Research | Knitting | Category 𝒦: Knitout program

Category 𝒦: Knitout program

Knitout is a language for knitting machines. The model has one carrier and two needle beds. A state gives the number of loops on each needle. Knitout programs form a category. An arrow is a program that the machine accepts. Composition runs one program after the other.

One-carrier knitout: the instructions, the machine state, and one step

Instructions (Op)

Instruction Effect
xfer n m All loops of n move to m. m is across from n.
rack r The back bed moves to offset r.
knit d n A new loop is made on n through its loops. The old loops leave the needle.
tuck d n A new loop goes on n. The old loops stay.
split d n m A new loop is made on n through its loops. The old loops move to m, across from n.
miss d n The carrier moves past n.
drop n The loops of n are dropped.
  • d: direction (R, L). A needle: bed (f, b) and an integer index.
  • One carrier. The carrier is always active. Its position is a physical carrier location. Location z is immediately left of physical needle z (Lin Def. 5.2).
  • Racks are integers.
  • Not included from Lin Def. 5.1: more than one carrier, in, out, nop, loop size, yarn length, the attachment map. Loop size and yarn length have no effect on validity or on the state.

State

  • Each loop has an identity. The identity is given when the loop is made. State.fresh counts up.
  • Each needle holds a list of identities. Head = top = the newest loop.
  • A transfer puts the moved loops on top of the loops of the receiving needle, in reversed order (xferStack).
  • A split leaves the new loop alone on the needle. The old loops go to the needle across, in the same way.

One step (step)

Refusal Instructions Condition From
notAligned xfer, split The two needles are not across from each other Lin Fig. 11
rackStep rack The offset does not change by exactly one Lin Fig. 11
carrierElsewhere knit, tuck, split, miss The carrier is not at arrivesFrom Lin Fig. 11
emptyNeedle knit, drop The needle has no loops Lin Fig. 11
emptyNeedle split The needle has no loops This model
backTuck tuck The needle is on the back bed This model
  • linRefusal: the refusals of Lin Fig. 11. extraRefusal: the refusals of this model.
  • Result: no loop is made directly on an empty back needle. (Lin: a knit needs a needle with loops.)
namespace Knitout
structure Needle where
  bed : Bed
  idx : Int
  deriving DecidableEq, Repr

inductive Op where
  | xfer (src dst : Needle)
  | rack (r : Int)
  | knit (d : Dir) (n : Needle)
  | tuck (d : Dir) (n : Needle)
  | split (d : Dir) (n dst : Needle)
  | miss (d : Dir) (n : Needle)
  | drop (n : Needle)
  deriving DecidableEq, Repr

structure State where
  rack : Int
  stk : Needle → List Nat
  carrier : Int
  fresh : Nat

structure LinState where
  rack : Int
  L : Needle → Nat
  Y : Int

def erase (s : State) : LinState := ⟨s.rack, fun n => (s.stk n).length, s.carrier⟩

def slot (r : Int) (n : Needle) : Int :=
  match n.bed with
  | .f => n.idx
  | .b => n.idx + r

def aligned (r : Int) (n m : Needle) : Bool := decide (n.bed ≠ m.bed ∧ slot r n = slot r m)

def arrivesFrom (d : Dir) (z : Int) : Int :=
  match d with
  | .R => z
  | .L => z + 1

def leavesTo (d : Dir) (z : Int) : Int :=
  match d with
  | .R => z + 1
  | .L => z

inductive Refusal where
  | notAligned
  | rackStep
  | carrierElsewhere
  | emptyNeedle
  | backTuck
  | splitNotOne
  deriving DecidableEq, Repr

def linRefusal (t : LinState) : Op → Option Refusal
  | .xfer a b => if aligned t.rack a b then none else some .notAligned
  | .rack r => if (r - t.rack).natAbs = 1 then none else some .rackStep
  | .knit d n =>
    if t.Y ≠ arrivesFrom d (slot t.rack n) then some .carrierElsewhere
    else if t.L n = 0 then some .emptyNeedle else none
  | .tuck d n => if t.Y ≠ arrivesFrom d (slot t.rack n) then some .carrierElsewhere else none
  | .split d n m =>
    if t.Y ≠ arrivesFrom d (slot t.rack n) then some .carrierElsewhere
    else if aligned t.rack n m then none else some .notAligned
  | .miss d n => if t.Y ≠ arrivesFrom d (slot t.rack n) then some .carrierElsewhere else none
  | .drop n => if t.L n = 0 then some .emptyNeedle else none

def extraRefusal (s : State) : Op → Option Refusal
  | .tuck _ n => if n.bed = .b then some .backTuck else none
  | .split _ n _ => if (s.stk n).length = 0 then some .emptyNeedle else none
  | _ => none

def refusal (s : State) (op : Op) : Option Refusal :=
  match linRefusal (erase s) op with
  | some r => some r
  | none => extraRefusal s op

def xferStack (moved below : List Nat) : List Nat := moved.reverse ++ below

def apply (s : State) : Op → State
  | .xfer a b =>
    { s with stk := Function.update (Function.update s.stk a []) b (xferStack (s.stk a) (s.stk b)) }
  | .rack r => { s with rack := r }
  | .knit d n =>
    { s with stk := Function.update s.stk n [s.fresh], fresh := s.fresh + 1,
             carrier := leavesTo d (slot s.rack n) }
  | .tuck d n =>
    { s with stk := Function.update s.stk n (s.fresh :: s.stk n), fresh := s.fresh + 1,
             carrier := leavesTo d (slot s.rack n) }
  | .split d n m =>
    { s with stk := Function.update (Function.update s.stk n [s.fresh]) m (xferStack (s.stk n) (s.stk m)),
             fresh := s.fresh + 1, carrier := leavesTo d (slot s.rack n) }
  | .miss d n => { s with carrier := leavesTo d (slot s.rack n) }
  | .drop n => { s with stk := Function.update s.stk n [] }

def step (s : State) (op : Op) : Except Refusal State :=
  match refusal s op with
  | none => .ok (apply s op)
  | some r => .error r

def runSteps (s : State) : List Op → Option State
  | [] => some s
  | op :: ops =>
    match step s op with
    | .ok s' => runSteps s' ops
    | .error _ => none
end Knitout

Lin's step, the split-free step, and the row of points

Lin's validity on counts, the split-free machine step1, and the row of points of a state. This file does not define drawings.

Declaration Content
linApply, linStep Lin Fig. 11 on a count state (LinState)
crateExtra1, crateRefusal1, step1, run The split-free machine: as step, but a split needs exactly one loop
validLin, validCrate, stepLin Validity in Lin's sense, validity for step1, one step with Lin's validity
empty z The empty machine with the carrier at location z
Pt, Pt2, points, points2, rowOf1 The row of a state (Lin Def. 5.5): one point per loop, or one per leg
leftToRight The loops of a needle, left to right
Distinct, Reachable Distinct loop identities; the states that Lin-valid instructions reach from empty z

The stack and the row

  • Each needle holds a list of loop identities. Head = top = the newest loop.
  • The row is read left to right, front before back (Lin Def. 5.5, Fig. 10).
  • Front needle: bottom first, top last. Back needle: top first, bottom last.
  • On both beds the top loop is nearest the gap between the beds.

The layout of legs

  • In points2, the two legs of each loop are adjacent. The loops of a needle come one after another.
  • Lin Fig. 12 draws the loops of a needle nested. This model does not use that layout.
  • In this layout a transfer draws no crossing. So a transfer keeps the left-to-right order of all points.
  • Result: the moved loops go on top of the receiving stack, in reversed order (xferStack).
  • points_xfer proves that a valid transfer changes no point of the row.
namespace Knitout
def linApply (t : LinState) : Op → LinState
  | .xfer a b => { t with L := Function.update (Function.update t.L a 0) b (t.L a + t.L b) }
  | .rack r => { t with rack := r }
  | .knit d n => { t with L := Function.update t.L n 1, Y := leavesTo d (slot t.rack n) }
  | .tuck d n => { t with L := Function.update t.L n (t.L n + 1), Y := leavesTo d (slot t.rack n) }
  | .split d n m =>
    { t with L := Function.update (Function.update t.L n 1) m (t.L n + t.L m),
             Y := leavesTo d (slot t.rack n) }
  | .miss d n => { t with Y := leavesTo d (slot t.rack n) }
  | .drop n => { t with L := Function.update t.L n 0 }

def linStep (t : LinState) (op : Op) : Option LinState :=
  match linRefusal t op with
  | none => some (linApply t op)
  | some _ => none

def crateExtra1 (s : State) : Op → Option Refusal
  | .tuck _ n => if n.bed = .b then some .backTuck else none
  | .split _ n _ => if (s.stk n).length = 1 then none else some .splitNotOne
  | _ => none

def crateRefusal1 (s : State) (op : Op) : Option Refusal :=
  match linRefusal (erase s) op with
  | some r => some r
  | none => crateExtra1 s op

def validLin (s : State) (op : Op) : Prop := linRefusal (erase s) op = none

def validCrate (s : State) (op : Op) : Prop := crateRefusal1 s op = none

instance (s : State) (op : Op) : Decidable (validLin s op) := by unfold validLin; infer_instance

instance (s : State) (op : Op) : Decidable (validCrate s op) := by unfold validCrate; infer_instance

def stepLin (s : State) (op : Op) : Option State :=
  match linRefusal (erase s) op with
  | none => some (apply s op)
  | some _ => none

def step1 (s : State) (op : Op) : Except Refusal State :=
  match crateRefusal1 s op with
  | none => .ok (apply s op)
  | some r => .error r

def run (s : State) : List Op → Except (Nat × Refusal) State
  | [] => .ok s
  | op :: ops =>
    match step1 s op with
    | .error e => .error (0, e)
    | .ok s' =>
      match run s' ops with
      | .error (k, e) => .error (k + 1, e)
      | .ok t => .ok t

def runTrace (s : State) : List Op → Option Nat × State
  | [] => (none, s)
  | op :: ops =>
    match step1 s op with
    | .error _ => (some 0, s)
    | .ok s' =>
      match runTrace s' ops with
      | (some k, t) => (some (k + 1), t)
      | (none, t) => (none, t)

def empty (z : Int) : State := ⟨0, fun _ => [], z, 0⟩

The row of a state (Lin Def. 5.5)

For each physical position i, left to right:

  1. The carrier, if it is at i (immediately left of physical needle i).
  2. The loops of the front needle f.i.
  3. The loops of the back needle across from it, b.(i − r).

points s lo w reads the w positions from lo.

inductive Pt where
  | carrier
  | loop (id : Nat)
  deriving DecidableEq, Repr

inductive Pt2 where
  | carrier
  | leg (id : Nat) (right : Bool)
  deriving DecidableEq, Repr

def leftToRight (s : State) (n : Needle) : List Nat :=
  match n.bed with
  | .f => (s.stk n).reverse
  | .b => s.stk n

def seg1 (s : State) (i : Int) : List Pt :=
  (if s.carrier = i then [Pt.carrier] else []) ++
  (leftToRight s ⟨.f, i⟩).map Pt.loop ++ (leftToRight s ⟨.b, i - s.rack⟩).map Pt.loop

def points (s : State) (lo : Int) (w : Nat) : List Pt :=
  (List.range w).flatMap fun k => seg1 s (lo + k)

def Pt.legs : Pt → List Pt2
  | .carrier => [.carrier]
  | .loop i => [.leg i false, .leg i true]

def points2 (s : State) (lo : Int) (w : Nat) : List Pt2 := (points s lo w).flatMap Pt.legs

def Pt.obj : Pt → CellObj
  | .carrier => .yarn
  | .loop _ => .loop

def rowOf1 (s : State) (lo : Int) (w : Nat) : CellRow := (points s lo w).map Pt.obj

Loop identities

def Distinct (s : State) : Prop :=
  (∀ n, (s.stk n).Nodup) ∧ (∀ n m x, x ∈ s.stk n → x ∈ s.stk m → n = m) ∧ (∀ n x, x ∈ s.stk n → x < s.fresh)

inductive Reachable : State → Prop
  | empty (z : Int) : Reachable (empty z)
  | step {s : State} (op : Op) : Reachable s → validLin s op → Reachable (apply s op)
end Knitout

Lemmas about the tracked knitout machine

Facts about apply, step1 and the stacks: validity, erasure, where loops land, distinct identities, order kept.

Theorem Statement
validCrate_validLin Each instruction that step1 accepts is valid in Lin's sense
step1_ok, stepLin_some step1 (or stepLin) succeeds exactly on valid instructions, with the state apply
erase_apply, erase_stepLin erase turns the step on stacks into Lin's step on counts (Fig. 11)
tuck_stk, xfer_dst, xfer_top, split_dst A new loop or a moved group goes on top. The stack below does not change. A moved group is reversed
no_reorder, suffix_or_gone Each new stack is one of the five StackChange cases. No instruction reorders a stack in place
knit_stk, knit_takes_all A knit leaves one new loop. It takes all loops of its needle
seg1_xfer, points_xfer A valid transfer changes no point of the row (Lin Fig. 12(g))
distinct_apply, Reachable.distinct Loop identities stay distinct
hanoi₁, hanoi₂, hanoi₃ Worked examples
shared_order_kept1 Two loops on one needle keep their left-to-right order through each program that run accepts
  • points_xfer fixes the stack order under a transfer: xferStack.
  • pairKept_step1 holds for each instruction valid in Lin's sense. So it also serves the machine step: shared_order_kept.

Validity

theorem validCrate_validLin {s : State} {op : Op} (h : validCrate s op) : validLin s op
Proof
:= by
  unfold validCrate crateRefusal1 at h
  unfold validLin
  cases hl : linRefusal (erase s) op with
  | none => rfl
  | some r => simp [hl] at h
theorem step1_ok {s s' : State} {op : Op} : step1 s op = .ok s' ↔ validCrate s op ∧ s' = apply s op
Proof
:= by
  unfold step1 validCrate
  cases crateRefusal1 s op with
  | none => simp [eq_comm]
  | some r => simp
theorem stepLin_some {s s' : State} {op : Op} : stepLin s op = some s' ↔ validLin s op ∧ s' = apply s op
Proof
:= by
  unfold stepLin validLin
  cases linRefusal (erase s) op with
  | none => simp [eq_comm]
  | some r => simp
theorem aligned_ne {r : Int} {a b : Needle} (h : aligned r a b = true) : a ≠ b
Proof
:= by
  rintro rfl
  simp [aligned] at h
theorem validLin_xfer {s : State} {a b : Needle} (h : validLin s (.xfer a b)) : aligned s.rack a b = true
Proof
:= by
  simp only [validLin, linRefusal] at h
  split_ifs at h with ha
  · exact ha
theorem validLin_split {s : State} {d : Dir} {n m : Needle} (h : validLin s (.split d n m)) :
    aligned s.rack n m = true
Proof
:= by
  simp only [validLin, linRefusal] at h
  split_ifs at h with h1 h2
  · exact h2
theorem validLin_knit {s : State} {d : Dir} {n : Needle} (h : validLin s (.knit d n)) : s.stk n ≠ []
Proof
:= by
  simp only [validLin, linRefusal] at h
  split_ifs at h with h1 h2
  simpa [erase] using h2
theorem validLin_drop {s : State} {n : Needle} (h : validLin s (.drop n)) : s.stk n ≠ []
Proof
:= by
  simp only [validLin, linRefusal] at h
  split_ifs at h with h1
  simpa [erase] using h1

The erasure commutes with the step

theorem erase_apply (s : State) (op : Op) : erase (apply s op) = linApply (erase s) op
Proof
:= by
  cases op with
  | xfer a b =>
    simp only [erase, apply, linApply, xferStack, LinState.mk.injEq, true_and, and_true]
    funext n; simp only [update_apply]; split_ifs <;> simp
  | rack r => rfl
  | knit d n =>
    simp only [erase, apply, linApply, LinState.mk.injEq, true_and, and_true]
    funext m; simp only [update_apply]; split_ifs <;> simp
  | tuck d n =>
    simp only [erase, apply, linApply, LinState.mk.injEq, true_and, and_true]
    funext m; simp only [update_apply]; split_ifs <;> simp_all
  | split d n m =>
    simp only [erase, apply, linApply, xferStack, LinState.mk.injEq, true_and, and_true]
    funext k; simp only [update_apply]; split_ifs <;> simp
  | miss d n => rfl
  | drop n =>
    simp only [erase, apply, linApply, LinState.mk.injEq, true_and, and_true]
    funext m; simp only [update_apply]; split_ifs <;> simp
theorem erase_stepLin (s : State) (op : Op) : (stepLin s op).map erase = linStep (erase s) op
Proof
:= by
  unfold stepLin linStep
  cases linRefusal (erase s) op with
  | none => simp [erase_apply]
  | some r => rfl

New loops and moved loops go on top

def top (l : List Nat) : Option Nat := l.head?
theorem tuck_stk (s : State) (d : Dir) (n : Needle) : (apply s (.tuck d n)).stk n = s.fresh :: s.stk n
Proof
:= by
  simp [apply]
theorem tuck_top (s : State) (d : Dir) (n : Needle) : top ((apply s (.tuck d n)).stk n) = some s.fresh
Proof
:= by
  simp [top, apply]
theorem tuck_other (s : State) (d : Dir) (n m : Needle) (h : m ≠ n) : (apply s (.tuck d n)).stk m = s.stk m
Proof
:= by
  simp [apply, h]
theorem xfer_dst (s : State) (a b : Needle) : (apply s (.xfer a b)).stk b = (s.stk a).reverse ++ s.stk b
Proof
:= by
  simp [apply, xferStack]
theorem xfer_src (s : State) (a b : Needle) (h : validLin s (.xfer a b)) : (apply s (.xfer a b)).stk a = []
Proof
:= by
  have hne := aligned_ne (validLin_xfer h)
  simp [apply, hne]
theorem xfer_other (s : State) (a b m : Needle) (ha : m ≠ a) (hb : m ≠ b) :
    (apply s (.xfer a b)).stk m = s.stk m
Proof
:= by
  simp [apply, ha, hb]
theorem xfer_top (s : State) (a b : Needle) (h : s.stk a ≠ []) :
    top ((apply s (.xfer a b)).stk b) = (s.stk a).getLast?
Proof
:= by
  rw [xfer_dst]
  cases hs : (s.stk a).reverse with
  | nil => simp_all
  | cons x xs =>
    have : (s.stk a).getLast? = some x := by
      rw [← List.head?_reverse, hs]; rfl
    simp [top, this]
theorem split_src (s : State) (d : Dir) (n m : Needle) (h : validLin s (.split d n m)) :
    (apply s (.split d n m)).stk n = [s.fresh]
Proof
:= by
  have hne := aligned_ne (validLin_split h)
  simp [apply, hne]
theorem split_dst (s : State) (d : Dir) (n m : Needle) :
    (apply s (.split d n m)).stk m = (s.stk n).reverse ++ s.stk m
Proof
:= by
  simp [apply, xferStack]
theorem drop_stk (s : State) (n : Needle) : (apply s (.drop n)).stk n = []
Proof
:= by
  simp [apply]
theorem knit_stk (s : State) (d : Dir) (n : Needle) : (apply s (.knit d n)).stk n = [s.fresh]
Proof
:= by
  simp [apply]

No instruction reorders a stack in place

inductive StackChange (s : State) (n : Needle) : List Nat → Prop
  | same : StackChange s n (s.stk n)
  | push : StackChange s n (s.fresh :: s.stk n)
  | cleared : StackChange s n []
  | renewed : StackChange s n [s.fresh]
  | received (m : Needle) : aligned s.rack m n = true → StackChange s n (xferStack (s.stk m) (s.stk n))
theorem no_reorder (s : State) (op : Op) (h : validLin s op) (n : Needle) :
    StackChange s n ((apply s op).stk n)
Proof
:= by
  cases op with
  | xfer a b =>
    have ha := validLin_xfer h
    have hne := aligned_ne ha
    by_cases hb : n = b
    · subst hb; simpa [apply] using StackChange.received a ha
    · by_cases hn : n = a
      · subst hn; simpa [apply, hb] using StackChange.cleared (s := s) (n := n)
      · simpa [apply, hb, hn] using StackChange.same (s := s) (n := n)
  | rack r => exact .same
  | knit d m =>
    by_cases hn : n = m
    · subst hn; simpa [apply] using StackChange.renewed (s := s) (n := n)
    · simpa [apply, hn] using StackChange.same (s := s) (n := n)
  | tuck d m =>
    by_cases hn : n = m
    · subst hn; simpa [apply] using StackChange.push (s := s) (n := n)
    · simpa [apply, hn] using StackChange.same (s := s) (n := n)
  | split d a b =>
    have ha := validLin_split h
    have hne := aligned_ne ha
    by_cases hb : n = b
    · subst hb; simpa [apply] using StackChange.received a ha
    · by_cases hn : n = a
      · subst hn; simpa [apply, hb] using StackChange.renewed (s := s) (n := n)
      · simpa [apply, hb, hn] using StackChange.same (s := s) (n := n)
  | miss d m => exact .same
  | drop m =>
    by_cases hn : n = m
    · subst hn; simpa [apply] using StackChange.cleared (s := s) (n := n)
    · simpa [apply, hn] using StackChange.same (s := s) (n := n)
theorem suffix_or_gone (s : State) (op : Op) (h : validLin s op) (n : Needle) :
    s.stk n <:+ (apply s op).stk n ∨ (apply s op).stk n ⊆ [s.fresh]
Proof
:= by
  have hc := no_reorder s op h n
  generalize (apply s op).stk n = l at hc ⊢
  rcases hc with _ | _ | _ | _ | ⟨m, _⟩
  · exact Or.inl (List.suffix_refl _)
  · exact Or.inl (List.suffix_cons _ _)
  · exact Or.inr (List.nil_subset _)
  · exact Or.inr (List.Subset.refl _)
  · exact Or.inl (List.suffix_append _ _)

Transfer is the identity on the row of points

theorem seg1_xfer (s : State) (a b : Needle) (h : aligned s.rack a b = true) (i : Int) :
    seg1 (apply s (.xfer a b)) i = seg1 s i
Proof
:= by
  obtain ⟨ba, xa⟩ := a
  obtain ⟨bb, xb⟩ := b
  cases ba <;> cases bb <;> simp [aligned, slot] at h
  · simp only [seg1, apply, leftToRight, xferStack, update_apply, Needle.mk.injEq]
    by_cases hi : i = xa
    · subst hi
      have : i - s.rack = xb := by omega
      simp [this]
      by_cases hc : s.carrier = i <;> simp [hc]
    · have : i - s.rack ≠ xb := by omega
      simp [hi, this]
      by_cases hc : s.carrier = i <;> simp [hc]
  · simp only [seg1, apply, leftToRight, xferStack, update_apply, Needle.mk.injEq]
    by_cases hi : i = xb
    · subst hi
      have : i - s.rack = xa := by omega
      simp [this]
      by_cases hc : s.carrier = i <;> simp [hc]
    · have : i - s.rack ≠ xa := by omega
      simp [hi, this]
      by_cases hc : s.carrier = i <;> simp [hc]
theorem points_xfer (s : State) (a b : Needle) (h : validLin s (.xfer a b)) (lo : Int) (w : Nat) :
    points (apply s (.xfer a b)) lo w = points s lo w
Proof
:= by
  simp only [points, seg1_xfer s a b (validLin_xfer h)]
theorem cellRowWidth_map_obj (l : List Pt) : cellRowWidth (l.map Pt.obj) = (l.flatMap Pt.legs).length
Proof
:= by
  induction l with
  | nil => rfl
  | cons p l ih =>
    cases p <;> simp [cellRowWidth, Pt.obj, Pt.legs, CellObj.width] at ih ⊢ <;> omega
theorem cellRowWidth_rowOf1 (s : State) (lo : Int) (w : Nat) : cellRowWidth (rowOf1 s lo w) = (points2 s lo w).length
Proof
:=
  cellRowWidth_map_obj _

Loop identities stay distinct

theorem mem_update {f : Needle → List Nat} {a n : Needle} {v : List Nat} {x : Nat} :
    x ∈ update f a v n ↔ (n = a ∧ x ∈ v) ∨ (n ≠ a ∧ x ∈ f n)
Proof
:= by
  by_cases h : n = a
  · subst h; simp
  · simp [h]
theorem distinct_apply {s : State} {op : Op} (hD : Distinct s) (h : validLin s op) : Distinct (apply s op)
Proof
:= by
  obtain ⟨hnd, hu, hlt⟩ := hD
  cases op with
  | rack r => exact ⟨hnd, hu, hlt⟩
  | miss d n => exact ⟨hnd, hu, hlt⟩
  | drop n =>
    refine ⟨fun m => ?_, fun m₁ m₂ x h₁ h₂ => ?_, fun m x hx => ?_⟩
    · by_cases hm : m = n
      · subst hm; simp [apply]
      · simpa [apply, hm] using hnd m
    · simp only [apply, mem_update, List.not_mem_nil, and_false, false_or] at h₁ h₂
      exact hu _ _ x h₁.2 h₂.2
    · simp only [apply, mem_update, List.not_mem_nil, and_false, false_or] at hx
      exact hlt _ _ hx.2
  | knit d n =>
    refine ⟨fun m => ?_, fun m₁ m₂ x h₁ h₂ => ?_, fun m x hx => ?_⟩
    · by_cases hm : m = n
      · subst hm; simp [apply]
      · simpa [apply, hm] using hnd m
    · simp only [apply, mem_update, List.mem_singleton] at h₁ h₂
      rcases h₁ with ⟨rfl, rfl⟩ | ⟨h₁n, h₁⟩ <;> rcases h₂ with ⟨rfl, h₂⟩ | ⟨h₂n, h₂⟩
      · rfl
      · exact absurd (hlt _ _ h₂) (by omega)
      · subst h₂; exact absurd (hlt _ _ h₁) (by omega)
      · exact hu _ _ x h₁ h₂
    · show x < s.fresh + 1
      simp only [apply, mem_update, List.mem_singleton] at hx
      rcases hx with ⟨_, rfl⟩ | ⟨_, hx⟩
      · omega
      · have := hlt _ _ hx; omega
  | tuck d n =>
    refine ⟨fun m => ?_, fun m₁ m₂ x h₁ h₂ => ?_, fun m x hx => ?_⟩
    · by_cases hm : m = n
      · subst hm
        simp only [apply, update_self, List.nodup_cons]
        exact ⟨fun hf => absurd (hlt _ _ hf) (by omega), hnd m⟩
      · simpa [apply, hm] using hnd m
    · simp only [apply, mem_update, List.mem_cons] at h₁ h₂
      rcases h₁ with ⟨rfl, h₁ | h₁⟩ | ⟨h₁n, h₁⟩ <;> rcases h₂ with ⟨rfl, h₂ | h₂⟩ | ⟨h₂n, h₂⟩
      all_goals first
        | rfl
        | (subst_vars; first | exact absurd (hlt _ _ h₁) (by omega) | exact absurd (hlt _ _ h₂) (by omega))
        | exact hu _ _ x h₁ h₂
    · show x < s.fresh + 1
      simp only [apply, mem_update, List.mem_cons] at hx
      rcases hx with ⟨_, rfl | hx⟩ | ⟨_, hx⟩
      · omega
      · have := hlt _ _ hx; omega
      · have := hlt _ _ hx; omega
  | xfer a b =>
    have hne := aligned_ne (validLin_xfer h)
    refine ⟨fun m => ?_, fun m₁ m₂ x h₁ h₂ => ?_, fun m x hx => ?_⟩
    · by_cases hb : m = b
      · subst hb
        simp only [apply, update_self, xferStack, List.nodup_append, List.nodup_reverse, List.mem_reverse]
        refine ⟨hnd a, hnd m, fun x hx y hy hxy => hne ?_⟩
        subst hxy; exact hu _ _ x hx hy
      · by_cases ha : m = a
        · subst ha; simp [apply, hb]
        · simpa [apply, ha, hb] using hnd m
    · simp only [apply, xferStack, mem_update, List.mem_append, List.mem_reverse, List.not_mem_nil,
        and_false, false_or] at h₁ h₂
      rcases h₁ with ⟨rfl, h₁ | h₁⟩ | ⟨h₁b, h₁a, h₁⟩ <;> rcases h₂ with ⟨rfl, h₂ | h₂⟩ | ⟨h₂b, h₂a, h₂⟩
      all_goals first
        | rfl
        | exact hu _ _ x h₁ h₂
        | exact absurd (hu _ _ x h₁ h₂) ‹_›
        | exact absurd (hu _ _ x h₁ h₂).symm ‹_›
    · simp only [apply, xferStack, mem_update, List.mem_append, List.mem_reverse, List.not_mem_nil,
        and_false, false_or] at hx
      rcases hx with ⟨_, hx | hx⟩ | ⟨_, _, hx⟩ <;> exact hlt _ _ hx
  | split d a b =>
    have hne := aligned_ne (validLin_split h)
    refine ⟨fun m => ?_, fun m₁ m₂ x h₁ h₂ => ?_, fun m x hx => ?_⟩
    · by_cases hb : m = b
      · subst hb
        simp only [apply, update_self, xferStack, List.nodup_append, List.nodup_reverse, List.mem_reverse]
        refine ⟨hnd a, hnd m, fun x hx y hy hxy => hne ?_⟩
        subst hxy; exact hu _ _ x hx hy
      · by_cases ha : m = a
        · subst ha; simp [apply, hb]
        · simpa [apply, ha, hb] using hnd m
    · simp only [apply, xferStack, mem_update, List.mem_append, List.mem_reverse, List.mem_singleton] at h₁ h₂
      rcases h₁ with ⟨rfl, h₁ | h₁⟩ | ⟨h₁b, ⟨rfl, rfl⟩ | ⟨h₁a, h₁⟩⟩ <;>
        rcases h₂ with ⟨rfl, h₂ | h₂⟩ | ⟨h₂b, ⟨rfl, h₂⟩ | ⟨h₂a, h₂⟩⟩
      all_goals first
        | rfl
        | (subst_vars; first | exact absurd (hlt _ _ h₁) (by omega) | exact absurd (hlt _ _ h₂) (by omega))
        | exact hu _ _ x h₁ h₂
        | exact absurd (hu _ _ x h₁ h₂) ‹_›
        | exact absurd (hu _ _ x h₁ h₂).symm ‹_›
    · show x < s.fresh + 1
      simp only [apply, mem_update, xferStack, List.mem_append, List.mem_reverse, List.mem_singleton] at hx
      rcases hx with ⟨_, hx | hx⟩ | ⟨_, ⟨_, rfl⟩ | ⟨_, hx⟩⟩
      · have := hlt _ _ hx; omega
      · have := hlt _ _ hx; omega
      · omega
      · have := hlt _ _ hx; omega
theorem distinct_empty (z : Int) : Distinct (empty z)
Proof
:= by
  refine ⟨fun _ => List.nodup_nil, fun _ _ x h _ => ?_, fun _ x h => ?_⟩ <;> simp [empty] at h
theorem Reachable.distinct {s : State} (h : Reachable s) : Distinct s
Proof
:= by
  induction h with
  | empty z => exact distinct_empty z
  | step op _ hv ih => exact distinct_apply ih hv
theorem knit_takes_all {s : State} (hD : Distinct s) (d : Dir) (n : Needle) :
    ∀ x ∈ s.stk n, ∀ m, x ∉ (apply s (.knit d n)).stk m
Proof
:= by
  intro x hx m hm
  obtain ⟨_, hu, hlt⟩ := hD
  simp only [apply, mem_update, List.mem_singleton] at hm
  rcases hm with ⟨_, rfl⟩ | ⟨hmn, hm⟩
  · have := hlt _ _ hx; omega
  · exact hmn (hu _ _ x hm hx)

Worked examples ("Tower of Hanoi")

  • From the empty machine, tuck loop 0 on f0 and loop 1 on f1.
  • Either loop can go on top of the other: move it to the back bed, rack, move it back (hanoi₁, hanoi₂).
  • A transfer of the stack of two loops to the back bed turns the stack over (hanoi₃). The loop that was below is then on top.
def f (i : Int) : Needle := ⟨.f, i⟩

def b (i : Int) : Needle := ⟨.b, i⟩

def tuckTwo : List Op := [.tuck .R (f 0), .tuck .R (f 1)]

def hanoi₁prog : List Op := tuckTwo ++ [.xfer (f 1) (b 1), .rack (-1), .xfer (b 1) (f 0)]

def hanoi₂prog : List Op := tuckTwo ++ [.xfer (f 0) (b 0), .rack 1, .xfer (b 0) (f 1)]

def hanoi₃prog : List Op := hanoi₁prog ++ [.xfer (f 0) (b 1)]
theorem hanoi₁ : (run (empty 0) hanoi₁prog).toOption.map (fun t => t.stk (f 0)) = some [1, 0]
Proof
:= by decide
theorem hanoi₂ : (run (empty 0) hanoi₂prog).toOption.map (fun t => t.stk (f 1)) = some [0, 1]
Proof
:= by decide
theorem hanoi₃ : (run (empty 0) hanoi₃prog).toOption.map (fun t => t.stk (b 1)) = some [0, 1]
Proof
:= by decide

Loops that share a needle keep their left-to-right order

  • Two loops x, y are on one needle, x left of y in the row ([x, y] <+ leftToRight s n).
  • After a valid program, both are on one needle with x left of y, or both are gone.
  • Transfers move whole stacks. So a stack on the front bed is never turned over in place.
  • A stack is turned over (as a list) only when it goes to the other bed.
def lrList : Bed → List Nat → List Nat
  | .f, l => l.reverse
  | .b, l => l
theorem leftToRight_eq (s : State) (n : Needle) : leftToRight s n = lrList n.bed (s.stk n)
Proof
:= by
  unfold leftToRight lrList; cases n.bed <;> rfl
theorem lr_recv {p : List Nat} {ba bb : Bed} (hne : ba ≠ bb) (la lb : List Nat) :
    (p <+ lrList ba la → p <+ lrList bb (la.reverse ++ lb)) ∧ (p <+ lrList bb lb → p <+ lrList bb (la.reverse ++ lb))
Proof
:= by
  cases ba <;> cases bb <;> simp only [ne_eq, not_true_eq_false, reduceCtorEq, not_false_eq_true] at hne <;>
    simp only [lrList, List.reverse_append, List.reverse_reverse]
  · exact ⟨fun h => h.trans (List.sublist_append_left _ _), fun h => h.trans (List.sublist_append_right _ _)⟩
  · exact ⟨fun h => h.trans (List.sublist_append_right _ _), fun h => h.trans (List.sublist_append_left _ _)⟩
theorem lr_push {p : List Nat} (bd : Bed) (z : Nat) (l : List Nat) (h : p <+ lrList bd l) : p <+ lrList bd (z :: l)
Proof
:= by
  cases bd
  · simp only [lrList, List.reverse_cons]; exact h.trans (List.sublist_append_left _ _)
  · exact h.trans (List.sublist_cons_self _ _)
theorem mem_of_lr {p : List Nat} {bd : Bed} {l : List Nat} {x : Nat} (h : p <+ lrList bd l) (hx : x ∈ p) : x ∈ l
Proof
:= by
  cases bd
  · simpa [lrList] using h.subset hx
  · exact h.subset hx
def PairKept (s : State) (x y : Nat) : Prop :=
  (∃ n, [x, y] <+ leftToRight s n) ∨ (x < s.fresh ∧ y < s.fresh ∧ ∀ n, x ∉ s.stk n ∧ y ∉ s.stk n)
theorem mem_apply {s : State} {op : Op} (hv : validLin s op) {n : Needle} {x : Nat}
    (hx : x ∈ (apply s op).stk n) : x = s.fresh ∨ ∃ m, x ∈ s.stk m
Proof
:= by
  have hc := no_reorder s op hv n
  generalize (apply s op).stk n = l at hc hx
  rcases hc with _ | _ | _ | _ | ⟨m, _⟩
  · exact Or.inr ⟨n, hx⟩
  · rcases List.mem_cons.1 hx with h | h
    · exact Or.inl h
    · exact Or.inr ⟨n, h⟩
  · simp at hx
  · exact Or.inl (List.mem_singleton.1 hx)
  · simp only [xferStack, List.mem_append, List.mem_reverse] at hx
    rcases hx with h | h
    · exact Or.inr ⟨m, h⟩
    · exact Or.inr ⟨n, h⟩
theorem gone_step1 {s : State} {op : Op} (hv : validLin s op) {x : Nat} (hx : x < s.fresh)
    (hg : ∀ n, x ∉ s.stk n) : x < (apply s op).fresh ∧ ∀ n, x ∉ (apply s op).stk n
Proof
:= by
  constructor
  · cases op <;> simp only [apply] <;> omega
  · intro n hn
    rcases mem_apply hv hn with h | ⟨m, h⟩
    · omega
    · exact hg m h
theorem pairKept_step1 {s : State} {op : Op} {x y : Nat} (hD : Distinct s) (hv : validLin s op)
    (h : PairKept s x y) : PairKept (apply s op) x y
Proof
:= by
  rcases h with ⟨n, hn⟩ | ⟨hx, hy, hg⟩
  swap
  · have gx := gone_step1 hv hx (fun n => (hg n).1)
    have gy := gone_step1 hv hy (fun n => (hg n).2)
    exact Or.inr ⟨gx.1, gy.1, fun n => ⟨gx.2 n, gy.2 n⟩⟩
  obtain ⟨_, hu, hlt⟩ := hD
  rw [leftToRight_eq] at hn
  have hxn : x ∈ s.stk n := mem_of_lr hn (by simp)
  have hyn : y ∈ s.stk n := mem_of_lr hn (by simp)
  have keep : ∀ s', s'.stk n = s.stk n → (∃ m, [x, y] <+ leftToRight s' m) :=
    fun s' e => ⟨n, by rw [leftToRight_eq, e]; exact hn⟩
  cases op with
  | rack r => exact Or.inl (keep _ rfl)
  | miss d t => exact Or.inl (keep _ rfl)
  | tuck d t =>
    by_cases ht : n = t
    · subst ht; exact Or.inl ⟨n, by rw [leftToRight_eq, tuck_stk]; exact lr_push _ _ _ hn⟩
    · exact Or.inl (keep _ (tuck_other s d t n ht))
  | knit d t =>
    by_cases ht : n = t
    · subst ht
      refine Or.inr ⟨by simp only [apply]; have := hlt _ _ hxn; omega,
        by simp only [apply]; have := hlt _ _ hyn; omega, fun m => ⟨fun hm => ?_, fun hm => ?_⟩⟩
      · simp only [apply, mem_update, List.mem_singleton] at hm
        rcases hm with ⟨_, rfl⟩ | ⟨hmn, hm⟩
        · have := hlt _ _ hxn; omega
        · exact hmn (hu _ _ x hm hxn)
      · simp only [apply, mem_update, List.mem_singleton] at hm
        rcases hm with ⟨_, rfl⟩ | ⟨hmn, hm⟩
        · have := hlt _ _ hyn; omega
        · exact hmn (hu _ _ y hm hyn)
    · exact Or.inl (keep _ (by simp [apply, ht]))
  | drop t =>
    by_cases ht : n = t
    · subst ht
      refine Or.inr ⟨by simp only [apply]; have := hlt _ _ hxn; omega,
        by simp only [apply]; have := hlt _ _ hyn; omega, fun m => ⟨fun hm => ?_, fun hm => ?_⟩⟩
      · simp only [apply, mem_update, List.not_mem_nil, and_false, false_or] at hm
        exact hm.1 (hu _ _ x hm.2 hxn)
      · simp only [apply, mem_update, List.not_mem_nil, and_false, false_or] at hm
        exact hm.1 (hu _ _ y hm.2 hyn)
    · exact Or.inl (keep _ (by simp [apply, ht]))
  | xfer a b =>
    have hal := validLin_xfer hv
    have hne := aligned_ne hal
    have hbed : a.bed ≠ b.bed := by simp [aligned] at hal; exact hal.1
    by_cases hb : n = b
    · subst hb
      exact Or.inl ⟨n, by rw [leftToRight_eq, xfer_dst]; exact (lr_recv hbed _ _).2 hn⟩
    · by_cases ha : n = a
      · subst ha
        exact Or.inl ⟨b, by rw [leftToRight_eq, xfer_dst]; exact (lr_recv hbed _ _).1 hn⟩
      · exact Or.inl (keep _ (xfer_other s a b n ha hb))
  | split d a b =>
    have hal := validLin_split hv
    have hne := aligned_ne hal
    have hbed : a.bed ≠ b.bed := by simp [aligned] at hal; exact hal.1
    by_cases hb : n = b
    · subst hb
      exact Or.inl ⟨n, by rw [leftToRight_eq, split_dst]; exact (lr_recv hbed _ _).2 hn⟩
    · by_cases ha : n = a
      · subst ha
        exact Or.inl ⟨b, by rw [leftToRight_eq, split_dst]; exact (lr_recv hbed _ _).1 hn⟩
      · exact Or.inl (keep _ (by simp [apply, ha, hb]))
theorem run_ok_cons {s t : State} {op : Op} {ops : List Op} (h : run s (op :: ops) = .ok t) :
    validCrate s op ∧ run (apply s op) ops = .ok t
Proof
:= by
  unfold run at h
  cases hs : step1 s op with
  | error e => simp [hs] at h
  | ok s' =>
    rw [step1_ok] at hs
    obtain ⟨hv, rfl⟩ := hs
    refine ⟨hv, ?_⟩
    rw [step1_ok.2 ⟨hv, rfl⟩] at h
    cases hr : run (apply s op) ops with
    | error e => simp [hr] at h
    | ok t' => simp only [hr] at h; rw [h]
theorem shared_order_kept1 {s t : State} {ops : List Op} {n : Needle} {x y : Nat} (hs : Reachable s)
    (hrun : run s ops = .ok t) (hxy : [x, y] <+ leftToRight s n) :
    (∃ m, [x, y] <+ leftToRight t m) ∨ (∀ m, x ∉ t.stk m ∧ y ∉ t.stk m)
Proof
:= by
  suffices key : ∀ (ops : List Op) (s : State), Reachable s → PairKept s x y → run s ops = .ok t → PairKept t x y by
    rcases key ops s hs (Or.inl ⟨n, hxy⟩) hrun with h | ⟨_, _, h⟩
    · exact Or.inl h
    · exact Or.inr h
  intro ops
  induction ops with
  | nil => intro s _ hp hr; simp only [run, Except.ok.injEq] at hr; exact hr ▸ hp
  | cons op ops ih =>
    intro s hs hp hr
    obtain ⟨hv, hr'⟩ := run_ok_cons hr
    have hl := validCrate_validLin hv
    exact ih _ (hs.step op hl) (pairKept_step1 hs.distinct hl hp) hr'

The count machine

The count machine runs knitout on Lin's state (LinState): the rack, the number of loops on each needle, the carrier location (Lin Def. 5.3). It agrees with the tracked machine (State, step) through erase.

Declaration Content
extraL The two refusals of this model on counts: a tuck on the back bed, a split of an empty needle
refusalL, stepL Lin Fig. 11 (linRefusal), then extraL, then linApply
runStepsL A program, one instruction at a time
stepL_erase step and stepL agree through erase
lift, erase_lift A tracked state with the counts of t. erase (lift t) = t
runStepsL_of_runSteps, runSteps_of_runStepsL A program runs on the count machine iff it runs on the tracked machine. The last states agree through erase
namespace FormalizeKnitting.Knitout
def extraL (t : LinState) : Op → Option Refusal
  | .tuck _ n => if n.bed = .b then some .backTuck else none
  | .split _ n _ => if t.L n = 0 then some .emptyNeedle else none
  | _ => none

def refusalL (t : LinState) (op : Op) : Option Refusal :=
  match linRefusal t op with
  | some r => some r
  | none => extraL t op

def stepL (t : LinState) (op : Op) : Except Refusal LinState :=
  match refusalL t op with
  | none => .ok (linApply t op)
  | some r => .error r

def runStepsL (t : LinState) : List Op → Option LinState
  | [] => some t
  | op :: ops =>
    match stepL t op with
    | .ok t' => runStepsL t' ops
    | .error _ => none
theorem extraRefusal_erase (s : State) (op : Op) : extraRefusal s op = extraL (erase s) op
Proof
:= by
  cases op <;> simp [extraRefusal, extraL, erase]
theorem refusal_erase (s : State) (op : Op) : refusal s op = refusalL (erase s) op
Proof
:= by
  unfold refusal refusalL; rw [extraRefusal_erase]; cases linRefusal (erase s) op <;> rfl
theorem stepL_erase (s : State) (op : Op) : (step s op).map erase = stepL (erase s) op
Proof
:= by
  unfold step stepL
  rw [refusal_erase]
  cases refusalL (erase s) op <;> simp [Except.map, erase_apply]
def lift (t : LinState) : State := ⟨t.rack, fun n => List.replicate (t.L n) 0, t.Y, 0⟩
theorem erase_lift (t : LinState) : erase (lift t) = t
Proof
:= by
  cases t; simp [erase, lift]
theorem runStepsL_of_runSteps : ∀ (s s' : State) (ops : List Op), runSteps s ops = some s' →
    runStepsL (erase s) ops = some (erase s')
Proof
  | s, s', [], h => by simp [runSteps, runStepsL] at h ⊢; rw [h]
  | s, s', op :: ops, h => by
    simp only [runSteps] at h
    have e := stepL_erase s op
    cases hs : step s op with
    | error r => simp [hs] at h
    | ok s1 =>
      rw [hs] at h e
      simp only [Except.map] at e
      simp only [runStepsL, ← e]
      exact runStepsL_of_runSteps s1 s' ops h
theorem runSteps_of_runStepsL : ∀ (s : State) (t' : LinState) (ops : List Op), runStepsL (erase s) ops = some t' →
    ∃ s', runSteps s ops = some s' ∧ erase s' = t'
Proof
  | s, t', [], h => by simp [runStepsL] at h; exact ⟨s, by simp [runSteps], h⟩
  | s, t', op :: ops, h => by
    simp only [runStepsL] at h
    have e := stepL_erase s op
    cases hs : step s op with
    | error r =>
      rw [hs] at e
      simp only [Except.map] at e
      rw [← e] at h; simp at h
    | ok s1 =>
      rw [hs] at e
      simp only [Except.map] at e
      rw [← e] at h
      obtain ⟨s', h1, h2⟩ := runSteps_of_runStepsL s1 t' ops h
      exact ⟨s', by simp [runSteps, hs, h1], h2⟩

The category of knitout programs, 𝒦

Part Definition
Objects KnitoutObj: a Lin state (LinState: rack, number of loops on each needle, carrier) with finitely many loops
Arrows s ⟶ s' KnitoutProgram s s': a list of instructions that the count machine accepts from s, ending on s'
Composition List concatenation: one program, then the other (runStepsL_append)
Identity on s The empty program
  • The count machine (stepL) is Lin Fig. 11 for one carrier, with two more refusals: a tuck on the back bed, a split of an empty needle.
  • Finitely many loops: some window holds the carrier and every loop (MR.Inv of the tracked state lift t).
  • Two arrows are equal only when they are the same list of instructions. There is no quotient.
  • Each state with finitely many loops is an object. Reachability from an empty machine is not required.
namespace FormalizeKnitting.Knitout
open CategoryTheory
theorem runStepsL_append (t : LinState) (p q : List Op) :
    runStepsL t (p ++ q) = (runStepsL t p).bind fun u => runStepsL u q
Proof
:= by
  induction p generalizing t with
  | nil => rfl
  | cons op ops ih =>
    simp only [List.cons_append, runStepsL]
    cases stepL t op with
    | ok t' => exact ih t'
    | error _ => rfl
structure KnitoutObj where
  t : LinState
  fin : ∃ win, MR.Inv win (lift t)
theorem KnitoutObj.ext' {a b : KnitoutObj} (h : a.t = b.t) : a = b
Proof
:= by
  cases a; cases b; simp only at h; subst h; rfl
def KnitoutProgram (a b : KnitoutObj) : Type := {ops : List Op // runStepsL a.t ops = some b.t}

def KnitoutProgram.comp {a b c : KnitoutObj} (p : KnitoutProgram a b) (q : KnitoutProgram b c) :
    KnitoutProgram a c :=
  ⟨p.1 ++ q.1, by rw [runStepsL_append, p.2]; exact q.2⟩

instance : SmallCategory KnitoutObj where
  Hom := KnitoutProgram
  id _ := ⟨[], rfl⟩
  comp p q := KnitoutProgram.comp p q
  id_comp _ := Subtype.ext (List.nil_append _)
  comp_id _ := Subtype.ext (List.append_nil _)
  assoc _ _ _ := Subtype.ext (List.append_assoc _ _ _)
theorem KnitoutObj.hom_val_comp {a b c : KnitoutObj} (p : a ⟶ b) (q : b ⟶ c) :
    (show KnitoutProgram a c from p ≫ q).1 =
      (show KnitoutProgram a b from p).1 ++ (show KnitoutProgram b c from q).1
Proof
:= rfl
theorem KnitoutObj.eqToHom_val {a b : KnitoutObj} (h : a = b) :
    (show KnitoutProgram 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. Def. 5.1 (syntax), Def. 5.2 (locations), Def. 5.3 (state), Fig. 11 (validity); Def. 5.1 (syntax), Def. 5.2 (locations), Def. 5.3 (state), Def. 5.5 (the row of points), Fig. 11 (validity), Fig. 12 (the drawings); Fig. 11, Fig. 12(g); Def. 5.3, Fig. 11.