Research | Knitting | Functor draw𝒮: Shorthand drawing

Functor draw𝒮: Shorthand drawing

draw𝒮 draws each shorthand program as a tangle. A row goes to its number of strands. A program goes to the drawing of its steps.

The shorthand with boxes

The box signature of the shorthand, and the drawing of a shorthand program with boxes.

Declaration Content
Label A tuck, a split through a stack of k loops (splitK), a knit through a stack of k loops
sig The signature. The insides are tuckInterior, splitInteriorK, knitInterior
runStepB, DB The drawing with boxes of one step and of a program
runStep_eq_runStepB, D_eq_DB The drawing D is DB with each box opened
  • A merge draws nothing. A drop is plain caps. Neither is a box.
  • The labels of denoteOp1 (PlainLabel) have a split through one loop only.
inductive Label where
  | tuck (d : Dir)
  | splitK (d : Dir) (bd : Bed) (k : Nat)
  | knit (d : Dir) (bd : Bed) (k : Nat)
  deriving DecidableEq, Repr

namespace Label
def ins : Label → Nat
  | tuck _ => 1
  | splitK _ _ k => 2 * k + 1
  | knit _ _ k => 2 * k + 1

def outs : Label → Nat
  | tuck _ => 3
  | splitK _ _ k => 2 * k + 3
  | knit .. => 3

def interior : Label → MWord
  | tuck d => tuckInterior d
  | splitK d bd k => splitInteriorK d bd k
  | knit d bd k => knitInterior d bd k
end Label

def sig : BoxSig Label where
  ins := Label.ins
  outs := Label.outs
  interior := Label.interior
  interior_wf l := by
    cases l with
    | tuck d => exact tuckInterior_wf d
    | splitK d bd k => exact splitInteriorK_wf d bd k
    | knit d bd k => exact knitInterior_wf d bd k

def runStepB (r : Row) : Step → Option (Row × BWord Label)
  | .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],
            plainB (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), [.box (.tuck d) (offset r (yarnIdx r))])
    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), [.box (.knit d bd k) (offset r (firstIdx r d))])
      | 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), [.box (.splitK d bd k) (offset r (firstIdx r d))])
      | none => none
    else none
  | .drop a =>
    if Obj.yarn ∈ r then
      match stackAt r a with
      | some k => some (r.eraseIdx a, plainB (List.replicate k (Gen.cap (offset r a))))
      | none => none
    else none

def DB : Row → List Step → Option (Row × BWord Label)
  | r, [] => some (r, [])
  | r, s :: ss =>
    match runStepB r s with
    | none => none
    | some (r', u) =>
      match DB r' ss with
      | none => none
      | some (r'', v) => some (r'', u ++ v)
theorem expand_plainB' {L : Type} (S : BoxSig L) (u : MWord) : expand S (plainB u) = u
Proof
:= by
  induction u with
  | nil => rfl
  | cons g gs ih =>
    simp only [plainB, List.map_cons, expand, List.flatMap_cons, BEv.inside, List.singleton_append] at ih ⊢
    rw [ih]
theorem runStep_eq_runStepB (r : Row) (s : Step) :
    runStep r s = (runStepB r s).map (fun p => (p.1, expand sig p.2))
Proof
:= by
  cases s with
  | x a front =>
    simp only [runStep, runStepB]
    split <;> simp [expand_plainB']
  | tuck d =>
    simp only [runStep, runStepB]
    split <;> simp [expand, BEv.inside, sig, Label.interior]
  | merge a =>
    simp only [runStep, runStepB]
    split
    · cases stackAt r a <;> cases stackAt r (a + 1) <;> simp [expand]
    · rfl
  | knit d bd =>
    simp only [runStep, runStepB]
    split
    · cases stackAt r (besideIdx r d) <;> simp [expand, BEv.inside, sig, Label.interior]
    · rfl
  | split d bd =>
    simp only [runStep, runStepB]
    split
    · cases stackAt r (besideIdx r d) <;> simp [expand, BEv.inside, sig, Label.interior]
    · rfl
  | drop a =>
    simp only [runStep, runStepB]
    split
    · cases stackAt r a <;> simp [expand_plainB']
    · rfl
theorem D_eq_DB : ∀ (r : Row) (w : List Step), D r w = (DB r w).map (fun p => (p.1, expand sig p.2))
Proof
  | r, [] => rfl
  | r, s :: ss => by
    simp only [D, DB, runStep_eq_runStepB]
    cases runStepB r s with
    | none => rfl
    | some p =>
      simp only [Option.map_some]
      rw [D_eq_DB p.1 ss]
      cases DB p.1 ss with
      | none => rfl
      | some q => simp [expand, List.flatMap_append]

Shorthand words are well formed

D_wf: if D r w = some (r', u), then u goes from rowWidth r strands to rowWidth r' strands. sim_correct uses D_wf to compose Iso along a program.

  • runStep_wf: the same fact for one step.
namespace FormalizeKnitting.Bridge.MR
open FormalizeKnitting.Knitout FormalizeKnitting.Knitout.MR
theorem cut (r : Row) (first len : Nat) :
    r = r.take first ++ (r.drop first).take len ++ r.drop (first + len)
Proof
:= by
  rw [List.append_assoc, ← List.drop_drop, List.take_append_drop, List.take_append_drop]
theorem splice_wf {r : Row} {first len : Nat} {new : Row} {u : MWord}
    (hu : topWidth (rowWidth ((r.drop first).take len)) u = some (rowWidth new)) :
    topWidth (rowWidth r) (shiftW (offset r first) u) = some (rowWidth (splice r first len new))
Proof
:= by
  have hr := congrArg rowWidth (cut r first len)
  rw [rowWidth_append, rowWidth_append] at hr
  rw [hr, offset, splice, rowWidth_append, rowWidth_append]
  exact topWidth_shift hu _ _
theorem window2 {r : Row} {a : Nat} {o₁ o₂ : Obj} (h₁ : r[a]? = some o₁) (h₂ : r[a + 1]? = some o₂) :
    (r.drop a).take 2 = [o₁, o₂]
Proof
:= by
  have e := cut2 h₁ h₂
  conv_lhs => rw [e]
  have hl : (r.take a).length = a := by
    have := (List.getElem?_eq_some_iff.mp h₁).1
    simp; omega
  rw [List.drop_append_of_le_length (by omega), List.drop_eq_nil_of_le (by omega)]; simp
theorem window1 {r : Row} {a : Nat} {o : Obj} (h : r[a]? = some o) : (r.drop a).take 1 = [o]
Proof
:= by
  have e := cut1 h
  conv_lhs => rw [e]
  have hl : (r.take a).length = a := by
    have := (List.getElem?_eq_some_iff.mp h).1
    simp; omega
  rw [List.drop_append_of_le_length (by omega), List.drop_eq_nil_of_le (by omega)]; simp
theorem caps_wf (o e : Nat) : ∀ k : Nat, topWidth (o + 2 * k + e) (List.replicate k (Gen.cap o)) = some (o + e)
Proof
  | 0 => by simp [topWidth]
  | k + 1 => by
    rw [List.replicate_succ, topWidth_cons_fits (by simp [Gen.fits]; omega)]
    have := caps_wf o e k
    simp only [Gen.widthAfter]
    rw [show o + 2 * (k + 1) + e - 2 = o + 2 * k + e by omega, this]
theorem rowWidth_pair (a b : Obj) : rowWidth [a, b] = a.width + b.width
Proof
:= by simp [rowWidth]
theorem stackAt_get {r : Row} {a k : Nat} (h : stackAt r a = some k) : r[a]? = some (.stack k)
Proof
:=
  (stackAt_some h).1
theorem runStep_wf {r : Row} {s : Step} {r' : Row} {u : MWord} (h : runStep r s = some (r', u)) :
    topWidth (rowWidth r) u = some (rowWidth 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, rfl⟩ := h
      have h₁ : r[a]? = some (r.getD a .yarn) := by
        rw [List.getD_eq_getElem?_getD, List.getElem?_eq_getElem (by omega)]; rfl
      have h₂ : r[a + 1]? = some (r.getD (a + 1) .yarn) := by
        rw [List.getD_eq_getElem?_getD, List.getElem?_eq_getElem (by omega)]; rfl
      have hw := window2 h₁ h₂
      have hr := congrArg rowWidth (cut r a 2)
      rw [rowWidth_append, rowWidth_append, hw, rowWidth_pair] at hr
      rw [swapBlocks_wf front (by rw [offset]; omega)]
      congr 1
      simp only [splice, rowWidth_append, rowWidth_pair]
      omega
    · 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, rfl⟩ := h
      refine splice_wf ?_
      rw [window1 (yarn_at hy)]
      cases d <;> exact tuckInterior_wf _
    · 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, rfl⟩ := h
        have hw := window2 (stackAt_get hk) (stackAt_get hl)
        have hr := congrArg rowWidth (cut r a 2)
        rw [rowWidth_append, rowWidth_append, hw] at hr
        simp only [topWidth, Option.some.injEq]
        rw [hr]; simp only [splice, rowWidth_append, rowWidth_cons]
        simp [rowWidth, Obj.width]; omega
      · 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, rfl⟩ := h
        refine splice_wf ?_
        have hy := yarn_at hc.1
        have hb := hc.2
        cases d with
        | R =>
          have hk' : stackAt r (yarnIdx r + 1) = some k := hk
          rw [show firstIdx r .R = yarnIdx r from rfl, window2 hy (stackAt_get hk')]
          have := knitInterior_wf .R bd k
          simpa [rowWidth, Obj.width, tuckRow, Nat.add_comm] using this
        | L =>
          simp only [hasBeside, decide_eq_true_eq] at hb
          have hy' : r[yarnIdx r - 1 + 1]? = some .yarn := by rw [Nat.sub_add_cancel hb]; exact hy
          have hk' : stackAt r (yarnIdx r - 1) = some k := hk
          rw [show firstIdx r .L = yarnIdx r - 1 from rfl, window2 (stackAt_get hk') hy']
          have := knitInterior_wf .L bd k
          simpa [rowWidth, Obj.width, tuckRow] using this
      · 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, rfl⟩ := h
        refine splice_wf ?_
        have hy := yarn_at hc.1
        have hb := hc.2
        have hw := splitInteriorK_wf d bd k
        have e : rowWidth (splitRow d bd k) = 2 * k + 3 := by
          cases d <;> cases bd <;> simp [splitRow, rowWidth, Obj.width] <;> omega
        rw [e]
        cases d with
        | R =>
          have hk' : stackAt r (yarnIdx r + 1) = some k := hk
          rw [show firstIdx r .R = yarnIdx r from rfl, window2 hy (stackAt_get hk')]
          simpa [rowWidth, Obj.width, Nat.add_comm] using hw
        | L =>
          simp only [hasBeside, decide_eq_true_eq] at hb
          have hy' : r[yarnIdx r - 1 + 1]? = some .yarn := by rw [Nat.sub_add_cancel hb]; exact hy
          have hk' : stackAt r (yarnIdx r - 1) = some k := hk
          rw [show firstIdx r .L = yarnIdx r - 1 from rfl, window2 (stackAt_get hk') hy']
          simpa [rowWidth, Obj.width] using hw
      · 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, rfl⟩ := h
        have hw := window1 (stackAt_get hk)
        have hr := congrArg rowWidth (cut r a 1)
        rw [rowWidth_append, rowWidth_append, hw] at hr
        have hal : a < r.length := (List.getElem?_eq_some_iff.mp (stackAt_get hk)).1
        have he : r.eraseIdx a = r.take a ++ r.drop (a + 1) := List.eraseIdx_eq_take_drop_succ r a
        rw [hr, offset, he, rowWidth_append]
        simpa [rowWidth, Obj.width] using caps_wf (rowWidth (r.take a)) (rowWidth (r.drop (a + 1))) k
      · exact absurd h (by simp)
    · exact absurd h (by simp)
theorem D_wf : ∀ {r : Row} {w : List Step} {r' : Row} {u : MWord}, D r w = some (r', u) →
    topWidth (rowWidth r) u = some (rowWidth r')
Proof
  | r, [], r', u, h => by
    simp only [D, Option.some.injEq, Prod.mk.injEq] at h
    obtain ⟨rfl, rfl⟩ := h; rfl
  | r, s :: ss, r', u, h => by
    simp only [D] at h
    split at h
    · simp at h
    · rename_i r₁ u₁ h₁
      split at h
      · simp at h
      · rename_i r₂ u₂ h₂
        simp only [Option.some.injEq, Prod.mk.injEq] at h
        obtain ⟨rfl, rfl⟩ := h
        exact topWidth_append_of (runStep_wf h₁) (D_wf h₂)

The drawing functor draw𝒮 : 𝒮 ⥤ 𝒯

Part Definition
Objects A row goes to its strand count (rowWidth).
Arrows A shorthand program goes to the class of its drawing by D (dw). wf_dw: the drawing is well formed.
Composition MR.D_app: the drawing of p ++ q is the drawing of p, then the drawing of q.
namespace FormalizeKnitting.Retract
open CategoryTheory FormalizeKnitting.Knitout FormalizeKnitting.Bridge
theorem TangleObj.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 dw (r : Row) (w : List Step) : MWord := ((D r w).map Prod.snd).getD []
theorem dw_eq {r r' : Row} {w : List Step} {M : MWord} (h : D r w = some (r', M)) : dw r w = M
Proof
:= by
  simp [dw, h]
theorem D_dw {a b : ShorthandObj} (p : ShorthandProgram a b) : D a.row p.1 = some (b.row, dw a.row p.1)
Proof
:= by
  obtain ⟨M, hM⟩ := p.2
  rw [dw_eq hM]; exact hM
theorem wf_dw {a b : ShorthandObj} (p : ShorthandProgram a b) : WF (rowWidth a.row) (rowWidth b.row) (dw a.row p.1)
Proof
:=
  MR.D_wf (D_dw p)
def drawS : ShorthandObj ⥤ TangleObj where
  obj a := ⟨rowWidth a.row⟩
  map {a b} p := TangleObj.hom (dw a.row (show ShorthandProgram a b from p).1) (wf_dw p)
  map_id a := TangleObj.hom_congr _ _ rfl
  map_comp {a b c} p q := by
    show TangleObj.hom _ _ = TangleObj.hom _ _ ≫ TangleObj.hom _ _
    rw [TangleObj.hom_comp]
    apply TangleObj.hom_congr
    exact dw_eq (MR.D_app (D_dw (show ShorthandProgram a b from p)) (D_dw (show ShorthandProgram b c from q)))
end FormalizeKnitting.Retract