Contents
- Reading a machine state as a row
- The reader
simOp: one knitout instruction to shorthand steps - One shorthand step on a cut row
- The stack row of a state
- From one instruction to a program
- Transfers and drops
- The instruction cases
- Misses
- Knits
- Tucks
- Splits
- Racking: one object past a block of objects
- Racking: the whole window
- The read functor R : 𝒦 ⥤ 𝒮
Reading a machine state as a row
Machine facts for the bridge from knitout to the shorthand: the window invariant and the drawing of a rack.
| Section | Key declarations |
|---|---|
| Counts at one position | nF, nB, nY |
| The window invariant | Inv, count_yarn_rowOf1 |
| Two drawing identities | expand_swapB, inside_box1 |
| Block braids | Rack.CF, Rack.iso_comm, Rack.swapBlocks_right_split, Rack.swapBlocks_left_split |
| A rack across the window | Rack.expand_rackBraid, Rack.drawAt, Rack.iso_app, Rack.nB_zero_of |
Counts at one needle position
nF s z: the loops on the front needle at physical positionz.nB s z: the loops on the back needle across fromz.nY s z: 1 if the carrier is atz, else 0.
namespace Knitout
namespace MR
def nF (s : State) (z : Int) : Nat := (s.stk ⟨.f, z⟩).length
def nB (s : State) (z : Int) : Nat := (s.stk ⟨.b, z - s.rack⟩).length
def nY (s : State) (z : Int) : Nat := if s.carrier = z then 1 else 0
end MR
end KnitoutThe window invariant and the two end statements
Inv win s: the state is inside the window.- The index of each back needle with loops is in the window (
BackIn). - The physical position of each needle with loops is in the window.
- The carrier is in the window.
- The index of each back needle with loops is in the window (
count_yarn_rowOf1: withInv, the rowrowOf1 s win.lo win.wholds exactly one yarn.
namespace Knitout
namespace MR
def Inv (win : Window) (s : State) : Prop :=
BackIn win s ∧ (∀ n, s.stk n ≠ [] → win.lo ≤ slot s.rack n ∧ slot s.rack n < win.lo + win.w) ∧
(win.lo ≤ s.carrier ∧ s.carrier < win.lo + win.w)One yarn in the row
theorem seg1_count_yarn (s : State) (i : Int) : ((seg1 s i).map Pt.obj).count .yarn = if s.carrier = i then 1 else 0Proof
:= by
unfold seg1
have h : ∀ l : List Nat, (l.map (Pt.obj ∘ Pt.loop)).count CellObj.yarn = 0 := by
intro l; simp [List.count_eq_zero, Pt.obj]
by_cases hc : s.carrier = i <;> simp [hc, List.count_append, h, Pt.obj] theorem rowOf1_succ (s : State) (lo : Int) (w : Nat) :
rowOf1 s lo (w + 1) = rowOf1 s lo w ++ (seg1 s (lo + w)).map Pt.objProof
:= by
simp [rowOf1, points, List.range_succ, List.flatMap_append] theorem count_yarn_rowOf1_aux (s : State) (lo : Int) :
∀ w : Nat, (rowOf1 s lo w).count .yarn = if lo ≤ s.carrier ∧ s.carrier < lo + w then 1 else 0Proof
| 0 => by simp [rowOf1, points]
| w + 1 => by
rw [rowOf1_succ, List.count_append, count_yarn_rowOf1_aux s lo w, seg1_count_yarn]
by_cases h1 : lo ≤ s.carrier ∧ s.carrier < lo + w
· rw [ite_eq_left h1, ite_eq_right (by omega), ite_eq_left (by push_cast; omega)]
· rw [ite_eq_right h1]
by_cases h2 : s.carrier = lo + w
· rw [ite_eq_left h2, ite_eq_left (by push_cast; omega)]
· rw [ite_eq_right h2, ite_eq_right (by push_cast; omega)] theorem count_yarn_rowOf1 {win : Window} {s : State} (h : Inv win s) : (rowOf1 s win.lo win.w).count .yarn = 1Proof
:= by
rw [count_yarn_rowOf1_aux, ite_eq_left h.2.2] The invariant is kept
theorem inside_bounds {win : Window} {z : Int} (h : win.inside z = true) : win.lo < z ∧ z < win.lo + win.w - 1Proof
:= by
simpa [Window.inside] using h theorem carrier_leaves {win : Window} {d : Dir} {z : Int} (h : win.inside z = true) :
win.lo ≤ leavesTo d z ∧ leavesTo d z < win.lo + win.wProof
:= by
have := inside_bounds h
cases d <;> simp [leavesTo] <;> omega Two drawing identities
expand_swapB: an openedswapBisswapBlocks.inside_box1: the inside of onePlainLabelbox. It is anrfllemma forsimp.
namespace Knitout
namespace MR
theorem expand_swapB (a l r : Nat) (b : Bool) : expand plainSig (swapB a l r b) = swapBlocks a l r bProof
:=
expand_plainB plainSig _ theorem inside_box1 (l : PlainLabel) (i : Nat) : (BEv.box l i : BEv PlainLabel).inside plainSig = shiftW i (PlainLabel.interior l)Proof
:=
rfl Racking: the block-braid lemmas
These are about plain drawings of crossings only.
CF b u: each event ofuis a crossing that fitsbstrands. ThentopWidth b u = some b.iso_comm: two crossing words commute if each pair of their crossings is far apart (indices differ by 2 or more).swapBlocks_right_split: an equality of words. The right block ofswapBlockssplits asR₁ + R₂: the firstR₁strands pass the left block, then the nextR₂.swapBlocks_left_split: anIso, by far commutation. The right block passes a left block ofa + cstrands. It can first pass the right part (cstrands), then the left part (astrands).
namespace Knitout
namespace MR
namespace Rack
def CF (b : Nat) (u : MWord) : Prop := ∀ g ∈ u, ∃ i s, g = Gen.cross i s ∧ i + 1 < btheorem CF.append {b : Nat} {u v : MWord} (hu : CF b u) (hv : CF b v) : CF b (u ++ v)Proof
:= by
intro g hg
rcases List.mem_append.mp hg with h | h
· exact hu g h
· exact hv g h theorem CF.left {b : Nat} {u v : MWord} (h : CF b (u ++ v)) : CF b uProof
:= fun g hg => h g (List.mem_append_left _ hg) theorem CF.right {b : Nat} {u v : MWord} (h : CF b (u ++ v)) : CF b vProof
:= fun g hg => h g (List.mem_append_right _ hg) theorem CF.nil (b : Nat) : CF b []Proof
:= fun _ h => absurd h (List.not_mem_nil) theorem topWidth_CF {b : Nat} : ∀ {u : MWord}, CF b u → topWidth b u = some bProof
| [], _ => rfl
| g :: u, h => by
obtain ⟨i, s, rfl, hi⟩ := h g (by simp)
have hf : (Gen.cross i s).fits b = true := by simp [Gen.fits, hi]
rw [topWidth_cons_fits hf]
exact topWidth_CF (fun g' hg' => h g' (by simp [hg'])) theorem Iso.embed {b : Nat} {p q u v : MWord} (h : Iso b u v) (hp : CF b p) (hq : CF b q) (hu : CF b u) :
Iso b (p ++ u ++ q) (p ++ v ++ q)Proof
:= by
have h1 : Iso b (u ++ q) (v ++ q) := Iso.suffix q h (by rw [topWidth_CF (hu.append hq)]; rfl)
have h2 := Iso.prefix (topWidth_CF hp) h1
simpa [List.append_assoc] using h2 Far commutation of crossing words
def FarX (g h : Gen) : Prop := ∃ i s j t, g = Gen.cross i s ∧ h = Gen.cross j t ∧ (i + 2 ≤ j ∨ j + 2 ≤ i)theorem iso_swap2 {b : Nat} {p q : MWord} {g h : Gen} (hf : FarX g h) (hw : CF b (p ++ [g, h] ++ q)) :
Iso b (p ++ [g, h] ++ q) (p ++ [h, g] ++ q)Proof
:= by
obtain ⟨i, s, j, t, rfl, rfl, hij⟩ := hf
have hw' := topWidth_CF hw
rcases hij with hij | hij
· have hok : (Mv.farR (Gen.cross i s) (Gen.cross j t)).ok = true := by
exact decide_eq_true (show i + 2 ≤ j by omega)
have hs := Step1.of_wf hok (p := p) (q := q) (by simpa [Mv.lhs] using hw')
simp only [Mv.lhs, Mv.rhs, Gen.lo, Gen.outs, Gen.ins, Gen.withLo] at hs
rw [show j - 2 + 2 = j by omega] at hs
exact Relation.EqvGen.rel _ _ hs
· have hok : (Mv.farL (Gen.cross i s) (Gen.cross j t)).ok = true := by
exact decide_eq_true (show j + 2 ≤ i by omega)
have hs := Step1.of_wf hok (p := p) (q := q) (by simpa [Mv.lhs] using hw')
simp only [Mv.lhs, Mv.rhs, Gen.lo, Gen.outs, Gen.ins, Gen.withLo] at hs
rw [show i + 2 - 2 = i by omega] at hs
exact Relation.EqvGen.rel _ _ hs theorem iso_pass_one {b : Nat} {g : Gen} : ∀ {Y p q : MWord}, (∀ h ∈ Y, FarX g h) → CF b (p ++ g :: Y ++ q) →
Iso b (p ++ [g] ++ Y ++ q) (p ++ Y ++ [g] ++ q)Proof
| [], p, q, _, _ => by simpa using Iso.refl b (p ++ g :: q)
| y :: Y, p, q, hY, hw => by
have h1 : Iso b (p ++ [g, y] ++ (Y ++ q)) (p ++ [y, g] ++ (Y ++ q)) :=
iso_swap2 (hY y (by simp)) (by simpa using hw)
have hw2 : CF b ((p ++ [y]) ++ g :: Y ++ q) := by
intro x hx; apply hw x; simp only [List.mem_append, List.mem_cons] at hx ⊢; tauto
have h2 := iso_pass_one (Y := Y) (p := p ++ [y]) (q := q) (fun h hh => hY h (by simp [hh])) hw2
have e1 : p ++ [g] ++ (y :: Y) ++ q = p ++ [g, y] ++ (Y ++ q) := by simp
have e2 : p ++ [y, g] ++ (Y ++ q) = p ++ [y] ++ [g] ++ Y ++ q := by simp
have e3 : p ++ [y] ++ Y ++ [g] ++ q = p ++ (y :: Y) ++ [g] ++ q := by simp
rw [e1]; refine h1.trans ?_; rw [e2]; rw [e3] at h2; exact h2 theorem iso_comm {b : Nat} {Y : MWord} : ∀ {X p q : MWord}, (∀ g ∈ X, ∀ h ∈ Y, FarX g h) →
CF b (p ++ X ++ Y ++ q) → Iso b (p ++ X ++ Y ++ q) (p ++ Y ++ X ++ q)Proof
| [], p, q, _, _ => by simpa using Iso.refl b (p ++ Y ++ q)
| x :: X, p, q, hXY, hw => by
have hw1 : CF b ((p ++ [x]) ++ X ++ Y ++ q) := by simpa using hw
have h1 := iso_comm (X := X) (p := p ++ [x]) (q := q) (fun g hg h hh => hXY g (by simp [hg]) h hh) hw1
have hw2 : CF b (p ++ x :: Y ++ (X ++ q)) := by
intro z hz; apply hw z; simp only [List.mem_append, List.mem_cons] at hz ⊢; tauto
have h2 := iso_pass_one (g := x) (Y := Y) (p := p) (q := X ++ q) (fun h hh => hXY x (by simp) h hh) hw2
have e1 : p ++ (x :: X) ++ Y ++ q = p ++ [x] ++ X ++ Y ++ q := by simp
have e2 : p ++ [x] ++ Y ++ X ++ q = p ++ [x] ++ Y ++ (X ++ q) := by simp
have e3 : p ++ Y ++ [x] ++ (X ++ q) = p ++ Y ++ (x :: X) ++ q := by simp
rw [e1]; refine h1.trans ?_; rw [e2]; rw [e3] at h2; exact h2 Blocks
def rowW (at_ L m : Nat) (f : Bool) : MWord := (List.range L).map fun st => Gen.cross (at_ + L - 1 - st + m) ftheorem swapBlocks_eq (at_ L R : Nat) (f : Bool) :
swapBlocks at_ L R f = (List.range R).flatMap fun m => rowW at_ L m fProof
:= rfl theorem mem_rowW {at_ L m : Nat} {f : Bool} {g : Gen} (h : g ∈ rowW at_ L m f) :
∃ st, st < L ∧ g = Gen.cross (at_ + L - 1 - st + m) fProof
:= by
simp only [rowW, List.mem_map, List.mem_range] at h
obtain ⟨st, hst, rfl⟩ := h
exact ⟨st, hst, rfl⟩ theorem mem_swapBlocks {at_ L R : Nat} {f : Bool} {g : Gen} (h : g ∈ swapBlocks at_ L R f) :
∃ m st, m < R ∧ st < L ∧ g = Gen.cross (at_ + L - 1 - st + m) fProof
:= by
rw [swapBlocks_eq] at h
simp only [List.mem_flatMap, List.mem_range] at h
obtain ⟨m, hm, hg⟩ := h
obtain ⟨st, hst, rfl⟩ := mem_rowW hg
exact ⟨m, st, hm, hst, rfl⟩ theorem CF_swapBlocks {b at_ L R : Nat} {f : Bool} (h : at_ + L + R ≤ b) : CF b (swapBlocks at_ L R f)Proof
:= by
intro g hg
obtain ⟨m, st, hm, hst, rfl⟩ := mem_swapBlocks hg
exact ⟨_, f, rfl, by omega⟩ theorem swapBlocks_right_split (at_ L R₁ R₂ : Nat) (f : Bool) :
swapBlocks at_ L (R₁ + R₂) f = swapBlocks at_ L R₁ f ++ swapBlocks (at_ + R₁) L R₂ fProof
:= by
simp only [swapBlocks_eq, List.range_add, List.flatMap_append, List.flatMap_map]
congr 1
apply List.flatMap_congr; intro m _
simp only [rowW]
apply List.map_congr_left; intro st hst
simp only [List.mem_range] at hst
congr 1; omega theorem rowW_split (at_ a c m : Nat) (f : Bool) : rowW at_ (a + c) m f = rowW (at_ + a) c m f ++ rowW at_ a m fProof
:= by
simp only [rowW]
rw [show a + c = c + a by omega, List.range_add, List.map_append, List.map_map]
congr 1
· apply List.map_congr_left; intro st hst
simp only [List.mem_range] at hst
congr 1; omega
· apply List.map_congr_left; intro st hst
simp only [List.mem_range] at hst
simp only [Function.comp]
congr 1; omega theorem swapBlocks_left_split {b : Nat} (at_ a c : Nat) (f : Bool) :
∀ R, at_ + a + c + R ≤ b →
Iso b (swapBlocks at_ (a + c) R f) (swapBlocks (at_ + a) c R f ++ swapBlocks at_ a R f)Proof
| 0, _ => by simpa [swapBlocks] using Iso.refl b []
| R + 1, hb => by
have ih := swapBlocks_left_split (b := b) at_ a c f R (by omega)
have eL : swapBlocks at_ (a + c) (R + 1) f =
swapBlocks at_ (a + c) R f ++ (rowW (at_ + a) c R f ++ rowW at_ a R f) := by
rw [swapBlocks_eq, List.range_succ, List.flatMap_append]
simp only [List.flatMap_cons, List.flatMap_nil, List.append_nil, rowW_split, swapBlocks_eq]
have eB : ∀ L at' : Nat, swapBlocks at' L (R + 1) f = swapBlocks at' L R f ++ rowW at' L R f := by
intro L at'; rw [swapBlocks_eq, List.range_succ, List.flatMap_append]
simp only [List.flatMap_cons, List.flatMap_nil, List.append_nil, swapBlocks_eq]
have cRB : CF b (rowW (at_ + a) c R f) := by
intro g hg; obtain ⟨st, hst, rfl⟩ := mem_rowW hg; exact ⟨_, f, rfl, by omega⟩
have cRA : CF b (rowW at_ a R f) := by
intro g hg; obtain ⟨st, hst, rfl⟩ := mem_rowW hg; exact ⟨_, f, rfl, by omega⟩
have cB : CF b (swapBlocks (at_ + a) c R f) := CF_swapBlocks (by omega)
have cA : CF b (swapBlocks at_ a R f) := CF_swapBlocks (by omega)
have cL : CF b (swapBlocks at_ (a + c) R f) := CF_swapBlocks (by omega)
have h1 : Iso b (swapBlocks at_ (a + c) R f ++ (rowW (at_ + a) c R f ++ rowW at_ a R f))
((swapBlocks (at_ + a) c R f ++ swapBlocks at_ a R f) ++ (rowW (at_ + a) c R f ++ rowW at_ a R f)) := by
simpa using Iso.embed ih (CF.nil b) (cRB.append cRA) cL
have hfar : ∀ g ∈ swapBlocks at_ a R f, ∀ h ∈ rowW (at_ + a) c R f, FarX g h := by
intro g hg h hh
obtain ⟨m, st, hm, hst, rfl⟩ := mem_swapBlocks hg
obtain ⟨st', hst', rfl⟩ := mem_rowW hh
exact ⟨_, f, _, f, rfl, rfl, Or.inl (by omega)⟩
have h2 := iso_comm (b := b) (X := swapBlocks at_ a R f) (Y := rowW (at_ + a) c R f)
(p := swapBlocks (at_ + a) c R f) (q := rowW at_ a R f) hfar (((cB.append cA).append cRB).append cRA)
rw [eL, eB, eB]
refine h1.trans ?_
simpa [List.append_assoc] using h2 example : swapBlocks 0 (2 + 2) 2 false ≠ swapBlocks 2 2 2 false ++ swapBlocks 0 2 2 falseProof
:= by decide example : swapBlocks 0 2 (1 + 2) true = swapBlocks 0 2 1 true ++ swapBlocks 1 2 2 trueProof
:= by decide A block of no strands
swapBlocks_zero_left: a left block of no strands gives the empty word.
namespace Knitout
namespace MR
namespace Rack
theorem swapBlocks_zero_left (at_ R : Nat) (f : Bool) : swapBlocks at_ 0 R f = []Proof
:= by
simp [swapBlocks] Racking across the whole window
expand_rackBraid: the drawing of a rack, opened, is the run ofdrawAtover each position of the window.iso_app: twoIsos of crossing words, one on top of the other.nB_zero_of: no back loops at a position where no back needle has loops.
namespace Knitout
namespace MR
namespace Rack
theorem iso_app {b : Nat} {M E M₂ S : MWord} (h₁ : Iso b M E) (h₂ : Iso b M₂ S) (hM : CF b M) (hM₂ : CF b M₂) :
Iso b (M ++ M₂) (E ++ S)Proof
:= by
have e : topWidth b E = some b := (Iso.topWidth_eq h₁) ▸ topWidth_CF hM
exact (Iso.suffix M₂ h₁ (by rw [topWidth_CF (hM.append hM₂)]; rfl)).trans (Iso.prefix e h₂) The word and the drawing, position by position
def drawAt (lo : Int) (s : State) (r : Int) (k : Nat) : MWord :=
if r < s.rack then
swapBlocks (cellRowWidth (rowOf1 s lo k)) (yarnAt s (lo + k) + frontAt s (lo + k)) (backAt s (lo + k)) true
else swapBlocks (cellRowWidth (rowOf1 s lo k) - 2 * nB s (lo + k - 1)) (2 * nB s (lo + k - 1))
(yarnAt s (lo + k) + frontAt s (lo + k)) falsetheorem flatMap_coe_aux {β : Type} (g : Int → List β) : ∀ l : List Nat,
List.flatMap g (List.flatMap (fun (a : Nat) => [(a : Int)]) l) = l.flatMap (fun k : Nat => g k)Proof
| [] => rfl
| a :: l => by simp only [List.flatMap_cons, List.flatMap_append, flatMap_coe_aux g l]; simp theorem flatMap_coe {β : Type} (g : Int → List β) (w : Nat) :
List.flatMap g (do let a ← List.range w; pure (a : Int)) = (List.range w).flatMap (fun k : Nat => g k)Proof
:= by
simp only [List.bind_eq_flatMap, List.pure_def]
exact flatMap_coe_aux g _ theorem expand_rackBraid (win : Window) (s : State) (r : Int) :
expand plainSig (denoteOp1 win s (.rack r)) = (List.range win.w).flatMap (drawAt win.lo s r)Proof
:= by
simp only [denoteOp1]
rw [rackBraid_eq, expand_plainB, flatMap_coe]
refine congrArg (fun g => List.flatMap g (List.range win.w)) (funext fun k => ?_)
simp only [drawAt, strandsBefore]
rw [show (win.lo + (k : Int) - win.lo).toNat = k by omega]
rfl theorem flatMap_range_succ {α : Type} (g : Nat → List α) (w : Nat) :
(List.range (w + 1)).flatMap g = (List.range w).flatMap g ++ g wProof
:= by
rw [List.range_succ, List.flatMap_append]; simp No back loops
theorem nB_zero_of {s : State} {z : Int} (h : s.stk ⟨.b, z - s.rack⟩ ≠ [] → False) : nB s z = 0Proof
:= by
by_contra hne
exact h (fun he => hne (by simp [nB, he])) The reader simOp: one knitout instruction to shorthand steps
simOp reads one knitout instruction as a shorthand word. sim reads a program. rowOf gives the shorthand row of
a state: each needle's loops are one stack object.
At each physical position, seg holds, from left to right:
- The yarn, if the carrier is there.
- The front needle's stack, if it is not empty.
- The back needle's stack across, if it is not empty.
| Instruction | Shorthand word (simOp) |
|---|---|
xfer a b |
Empty when a or b holds no loops. Otherwise one merge of the two stacks at that position. |
miss d n |
The yarn exchanges with each stack at that position (0, 1 or 2 exchanges). |
tuck d n, direction R |
The yarn passes the front stack, one tuck, a merge of the front stack with the new loop, the yarn passes the back stack. |
tuck d n, direction L |
The yarn passes the back stack, one tuck, the yarn passes the front stack, a merge of the front stack with the new loop. |
knit d n |
One knit. The yarn passes the other needle's stack: after the knit for f R and b L, before it for f L and b R. |
split d n m |
One split. The yarn passes the other needle's stack, before or after as for knit. Then a merge of the old stack with the stack across. |
drop n |
One drop. |
rack r |
rackSteps: at each position, the back stack that moves exchanges with each fixed object (the yarn, the front stack). |
- An exchange or a merge with an empty stack is left out (
xs,mswith count 0). simstops at the first instruction thatsteprefuses. From there, the word is empty.
namespace FormalizeKnitting.Bridge.MR
open FormalizeKnitting.Knitout FormalizeKnitting.Knitout.MR
def stkO (n : Nat) : Row := if n = 0 then [] else [.stack n]
def ob (n : Nat) : Nat := if n = 0 then 0 else 1
def seg (s : State) (i : Int) : Row :=
(if s.carrier = i then [Obj.yarn] else []) ++ stkO (nF s i) ++ stkO (nB s i)
def rowOf (s : State) (lo : Int) (w : Nat) : Row := (List.range w).flatMap fun k => seg s (lo + k)
def objsBefore (win : Window) (s : State) (i : Int) : Nat := (rowOf s win.lo (i - win.lo).toNat).length
def xs (a : Nat) (front : Bool) (c : Nat) : List Step := List.replicate c (.x a front)
def ms (a c : Nat) : List Step := List.replicate c (.merge a)
def rackSteps (win : Window) (s : State) (r : Int) : List Step :=
(List.range win.w).flatMap fun k =>
let i := win.lo + k
let base := objsBefore win s i
let p := nY s i + ob (nF s i)
if r < s.rack then
if nB s i = 0 then [] else (List.range p).map fun j => .x (base + p - 1 - j) true
else
if (s.stk ⟨.b, i - 1 - s.rack⟩).length = 0 then [] else (List.range p).map fun j => .x (base - 1 + j) falsesimOp: 44 lines of Lean
def simOp (win : Window) (s : State) : Op → List Step
| .xfer a b =>
let z := slot s.rack a
if (s.stk a).length = 0 ∨ (s.stk b).length = 0 then [] else [.merge (objsBefore win s z + nY s z)]
| .rack r => rackSteps win s r
| .miss d n =>
let z := slot s.rack n
let y := objsBefore win s (arrivesFrom d z)
let fo := ob (nF s z)
let bo := ob (nB s z)
match d with
| .R => xs y false fo ++ xs (y + fo) true bo
| .L => xs (y - 1) false bo ++ xs (y - 1 - bo) true fo
| .knit d n =>
let z := slot s.rack n
let y := objsBefore win s (arrivesFrom d z)
let fo := ob (nF s z)
let bo := ob (nB s z)
match d, n.bed with
| .R, .f => [.knit .R .f] ++ xs (y + 1) true bo
| .R, .b => xs y false fo ++ [.knit .R .b]
| .L, .f => xs (y - 1) false bo ++ [.knit .L .f]
| .L, .b => [.knit .L .b] ++ xs (y - 2) true fo
| .tuck d n =>
let z := slot s.rack n
let y := objsBefore win s (arrivesFrom d z)
let fo := ob (nF s z)
let bo := ob (nB s z)
match d with
| .R => xs y false fo ++ [.tuck .R] ++ ms y fo ++ xs (y + 1) true bo
| .L => xs (y - 1) false bo ++ [.tuck .L] ++ xs (y - 1 - bo) true fo ++ ms (y - bo) fo
| .split d n _ =>
let z := slot s.rack n
let y := objsBefore win s (arrivesFrom d z)
let fo := ob (nF s z)
let bo := ob (nB s z)
match d, n.bed with
| .R, .f => [.split .R .f] ++ xs (y + 2) true bo ++ ms (y + 1) bo
| .R, .b => xs y false fo ++ [.split .R .b] ++ ms y fo
| .L, .f => xs (y - 1) false bo ++ [.split .L .f] ++ ms y bo
| .L, .b => [.split .L .b] ++ xs (y - 2) true fo ++ ms (y - 1) fo
| .drop n =>
let z := slot s.rack n
[.drop (objsBefore win s z + nY s z + (if n.bed = .b then ob (nF s z) else 0))] def sim (win : Window) : State → List Op → List Step
| _, [] => []
| s, op :: ops =>
match step s op with
| .error _ => []
| .ok s' => simOp win s op ++ sim win s' ops
end FormalizeKnitting.Bridge.MR One shorthand step on a cut row
These lemmas compute runStep and D on a row cut as A ++ (the objects that the step works on) ++ Z.
| Lemma | Step |
|---|---|
rs_x |
An exchange of two neighbour objects |
rs_merge |
A merge of two neighbour stacks |
rs_tuck |
A tuck |
rs_knitR, rs_knitL |
A knit through a stack |
rs_splitR, rs_splitL |
A split through a stack |
rs_drop |
A drop |
D_passR, D_passL |
The yarn passes a stack that can be empty (stkO n) |
D_mergeO, D_mergeO' |
A merge with a stack that can be empty (ms … (ob n)) |
namespace FormalizeKnitting.Bridge.MR
open FormalizeKnitting.Knitout FormalizeKnitting.Knitout.MR
theorem rowWidth_append (A B : Row) : rowWidth (A ++ B) = rowWidth A + rowWidth BProof
:= by
simp [rowWidth] theorem rowWidth_cons (o : Obj) (A : Row) : rowWidth (o :: A) = o.width + rowWidth AProof
:= by
simp [rowWidth] theorem offset_len (A B : Row) : offset (A ++ B) A.length = rowWidth AProof
:= by
simp [offset] theorem splice_mid (A X Y Z : Row) : splice (A ++ X ++ Z) A.length X.length Y = A ++ Y ++ ZProof
:= by
simp [splice, List.drop_append] theorem yarnIdx_mid (A Z : Row) (hA : Obj.yarn ∉ A) : yarnIdx (A ++ .yarn :: Z) = A.lengthProof
:= by
simp [yarnIdx, List.idxOf_append, hA] theorem stackAt_at (A Z : Row) (k : Nat) (hk : 0 < k) : stackAt (A ++ .stack k :: Z) A.length = some kProof
:= by
simp [stackAt, hk] theorem stackAt_at1 (A Z : Row) (o : Obj) (k : Nat) (hk : 0 < k) :
stackAt (A ++ o :: .stack k :: Z) (A.length + 1) = some kProof
:= by
simp [stackAt, hk] theorem D_cons_some {r r₁ : Row} {s : Step} {w : List Step} {u : MWord} (h : runStep r s = some (r₁, u)) :
D r (s :: w) = (D r₁ w).map fun q => (q.1, u ++ q.2)Proof
:= by
simp only [D, h]; cases D r₁ w <;> rfl theorem D_single {r r₁ : Row} {s : Step} {u : MWord} (h : runStep r s = some (r₁, u)) :
D r [s] = some (r₁, u)Proof
:= by
rw [D_cons_some h]; simp [D] theorem D_app {r r₁ r₂ : Row} {w₁ w₂ : List Step} {u v : MWord} (h₁ : D r w₁ = some (r₁, u))
(h₂ : D r₁ w₂ = some (r₂, v)) : D r (w₁ ++ w₂) = some (r₂, u ++ v)Proof
:= by
rw [D_append, h₁]; simp [h₂] One step
theorem rs_x (A Z : Row) (a b : Obj) (f : Bool) (hy : Obj.yarn ∈ A ++ a :: b :: Z) :
runStep (A ++ a :: b :: Z) (.x A.length f) =
some (A ++ b :: a :: Z, swapBlocks (rowWidth A) a.width b.width f)Proof
:= by
have h1 : A.length + 1 < (A ++ a :: b :: Z).length := by simp
have e : splice (A ++ a :: b :: Z) A.length 2 [b, a] = A ++ b :: a :: Z := by simp [splice, List.drop_append]
simp only [runStep, ite_eq_left (And.intro hy h1)]
have g1 : (A ++ a :: b :: Z).getD A.length .yarn = a := by simp
have g2 : (A ++ a :: b :: Z).getD (A.length + 1) .yarn = b := by simp
rw [g1, g2, show A ++ a :: b :: Z = A ++ (a :: b :: Z) from rfl, offset_len, e] theorem rs_merge (A Z : Row) (k l : Nat) (hk : 0 < k) (hl : 0 < l) (hy : Obj.yarn ∈ A ++ Z) :
runStep (A ++ .stack k :: .stack l :: Z) (.merge A.length) = some (A ++ .stack (k + l) :: Z, [])Proof
:= by
have hy' : Obj.yarn ∈ A ++ .stack k :: .stack l :: Z := by
simp only [List.mem_append, List.mem_cons] at hy ⊢; tauto
have e := splice_mid A [.stack k, .stack l] [.stack (k + l)] Z
simp only [List.append_assoc, List.cons_append, List.nil_append, List.length_cons, List.length_nil] at e
simp only [runStep, ite_eq_left hy', stackAt_at _ _ _ hk, stackAt_at1 _ _ _ _ hl, e] theorem rs_tuck (A Z : Row) (hA : Obj.yarn ∉ A) (d : Dir) :
runStep (A ++ .yarn :: Z) (.tuck d) = some (A ++ tuckRow d ++ Z, shiftW (rowWidth A) (tuckInterior d))Proof
:= by
have hy : Obj.yarn ∈ A ++ .yarn :: Z := by simp
have e := splice_mid A [.yarn] (tuckRow d) Z
simp only [List.append_assoc, List.cons_append, List.nil_append, List.length_cons, List.length_nil] at e
simp only [runStep, ite_eq_left hy, yarnIdx_mid A Z hA, e, List.append_assoc]
rw [offset_len] theorem rs_drop (A Z : Row) (k : Nat) (hk : 0 < k) (hy : Obj.yarn ∈ A ++ Z) :
runStep (A ++ .stack k :: Z) (.drop A.length) = some (A ++ Z, List.replicate k (Gen.cap (rowWidth A)))Proof
:= by
have hy' : Obj.yarn ∈ A ++ .stack k :: Z := by
simp only [List.mem_append, List.mem_cons] at hy ⊢; tauto
simp only [runStep, ite_eq_left hy', stackAt_at _ _ _ hk, offset_len]
simp [List.eraseIdx_append_of_length_le] theorem yarnIdx_mid1 (A Z : Row) (hA : Obj.yarn ∉ A) (k : Nat) :
yarnIdx (A ++ .stack k :: .yarn :: Z) = A.length + 1Proof
:= by
have := yarnIdx_mid (A ++ [.stack k]) Z (by simpa using hA)
simpa using this theorem rs_knitR (A Z : Row) (hA : Obj.yarn ∉ A) (k : Nat) (hk : 0 < k) (bd : Bed) :
runStep (A ++ .yarn :: .stack k :: Z) (.knit .R bd) =
some (A ++ tuckRow .R ++ Z, shiftW (rowWidth A) (knitInterior .R bd k))Proof
:= by
have hy : Obj.yarn ∈ A ++ .yarn :: .stack k :: Z := by simp
have hi := yarnIdx_mid A (.stack k :: Z) hA
have hb : hasBeside (A ++ .yarn :: .stack k :: Z) .R = true := by simp [hasBeside, hi]
have hs : stackAt (A ++ .yarn :: .stack k :: Z) (besideIdx (A ++ .yarn :: .stack k :: Z) .R) = some k := by
simp only [besideIdx, hi]; exact stackAt_at1 _ _ _ _ hk
have e : splice (A ++ .yarn :: .stack k :: Z) A.length 2 (tuckRow .R) = A ++ tuckRow .R ++ Z := by
simp [splice, List.drop_append]
simp only [runStep, ite_eq_left (And.intro hy hb), hs, firstIdx, hi, e, offset_len] theorem rs_splitR (A Z : Row) (hA : Obj.yarn ∉ A) (k : Nat) (hk : 0 < k) (bd : Bed) :
runStep (A ++ .yarn :: .stack k :: Z) (.split .R bd) =
some (A ++ splitRow .R bd k ++ Z, shiftW (rowWidth A) (splitInteriorK .R bd k))Proof
:= by
have hy : Obj.yarn ∈ A ++ .yarn :: .stack k :: Z := by simp
have hi := yarnIdx_mid A (.stack k :: Z) hA
have hb : hasBeside (A ++ .yarn :: .stack k :: Z) .R = true := by simp [hasBeside, hi]
have hs : stackAt (A ++ .yarn :: .stack k :: Z) (besideIdx (A ++ .yarn :: .stack k :: Z) .R) = some k := by
simp only [besideIdx, hi]; exact stackAt_at1 _ _ _ _ hk
have e : splice (A ++ .yarn :: .stack k :: Z) A.length 2 (splitRow .R bd k) = A ++ splitRow .R bd k ++ Z := by
simp [splice, List.drop_append]
simp only [runStep, ite_eq_left (And.intro hy hb), hs, firstIdx, hi, e, offset_len] theorem rs_knitL (A Z : Row) (hA : Obj.yarn ∉ A) (k : Nat) (hk : 0 < k) (bd : Bed) :
runStep (A ++ .stack k :: .yarn :: Z) (.knit .L bd) =
some (A ++ tuckRow .L ++ Z, shiftW (rowWidth A) (knitInterior .L bd k))Proof
:= by
have hy : Obj.yarn ∈ A ++ .stack k :: .yarn :: Z := by simp
have hi := yarnIdx_mid1 A Z hA k
have hb : hasBeside (A ++ .stack k :: .yarn :: Z) .L = true := by simp [hasBeside, hi]
have hs : stackAt (A ++ .stack k :: .yarn :: Z) (besideIdx (A ++ .stack k :: .yarn :: Z) .L) = some k := by
simp only [besideIdx, hi, Nat.add_sub_cancel]; exact stackAt_at _ _ _ hk
have e : splice (A ++ .stack k :: .yarn :: Z) A.length 2 (tuckRow .L) = A ++ tuckRow .L ++ Z := by
simp [splice, List.drop_append]
simp only [runStep, ite_eq_left (And.intro hy hb), hs, firstIdx, hi, Nat.add_sub_cancel, e, offset_len] theorem rs_splitL (A Z : Row) (hA : Obj.yarn ∉ A) (k : Nat) (hk : 0 < k) (bd : Bed) :
runStep (A ++ .stack k :: .yarn :: Z) (.split .L bd) =
some (A ++ splitRow .L bd k ++ Z, shiftW (rowWidth A) (splitInteriorK .L bd k))Proof
:= by
have hy : Obj.yarn ∈ A ++ .stack k :: .yarn :: Z := by simp
have hi := yarnIdx_mid1 A Z hA k
have hb : hasBeside (A ++ .stack k :: .yarn :: Z) .L = true := by simp [hasBeside, hi]
have hs : stackAt (A ++ .stack k :: .yarn :: Z) (besideIdx (A ++ .stack k :: .yarn :: Z) .L) = some k := by
simp only [besideIdx, hi, Nat.add_sub_cancel]; exact stackAt_at _ _ _ hk
have e : splice (A ++ .stack k :: .yarn :: Z) A.length 2 (splitRow .L bd k) = A ++ splitRow .L bd k ++ Z := by
simp [splice, List.drop_append]
simp only [runStep, ite_eq_left (And.intro hy hb), hs, firstIdx, hi, Nat.add_sub_cancel, e, offset_len] Passing or merging with a possibly empty stack
theorem stkO_width (n : Nat) : rowWidth (stkO n) = 2 * nProof
:= by
unfold stkO; split <;> simp_all [rowWidth, Obj.width] theorem stkO_length (n : Nat) : (stkO n).length = ob nProof
:= by
unfold stkO ob; split <;> simp theorem yarn_notin_stkO (n : Nat) : Obj.yarn ∉ stkO nProof
:= by
unfold stkO; split <;> simp theorem D_passR (A Z : Row) (n : Nat) (f : Bool) :
D (A ++ .yarn :: (stkO n ++ Z)) (xs A.length f (ob n)) =
some (A ++ stkO n ++ .yarn :: Z, swapBlocks (rowWidth A) 1 (2 * n) f)Proof
:= by
unfold stkO ob xs
split
· subst_vars; simp [D, swapBlocks]
· rename_i hn
have h := rs_x A Z .yarn (.stack n) f (by simp)
rw [List.replicate_one, D_single (by simpa using h)]
simp [Obj.width] theorem D_passL (A Z : Row) (n : Nat) (f : Bool) :
D (A ++ stkO n ++ .yarn :: Z) (xs (A.length + ob n - 1) f (ob n)) =
some (A ++ .yarn :: (stkO n ++ Z), swapBlocks (rowWidth A) (2 * n) 1 f)Proof
:= by
unfold stkO ob xs
split
· subst_vars; simp [D, swapBlocks]
· rename_i hn
have h := rs_x A Z (.stack n) .yarn f (by simp)
rw [List.replicate_one, show A.length + 1 - 1 = A.length by omega]
rw [D_single (by simpa using h)]
simp [Obj.width] theorem D_mergeO (A Z : Row) (k n : Nat) (hk : 0 < k) (hy : Obj.yarn ∈ A ++ Z) :
D (A ++ .stack k :: (stkO n ++ Z)) (ms A.length (ob n)) = some (A ++ .stack (k + n) :: Z, [])Proof
:= by
unfold stkO ob ms
split
· subst_vars; simp [D]
· rename_i hn
rw [List.replicate_one, D_single (by simpa using rs_merge A Z k n hk (by omega) hy)] theorem D_mergeO' (A Z : Row) (k n : Nat) (hk : 0 < k) (hy : Obj.yarn ∈ A ++ Z) :
D (A ++ stkO n ++ .stack k :: Z) (ms A.length (ob n)) = some (A ++ .stack (n + k) :: Z, [])Proof
:= by
unfold stkO ob ms
split
· subst_vars; simp [D]
· rename_i hn
rw [List.replicate_one]
have h := rs_merge A Z n k (by omega) hk hy
rw [D_single (by simpa using h)] The stack row of a state
Facts about rowOf, the shorthand row of a state.
rowWidth_rowOf:rowOfhas the same number of strands asrowOf1. SostrandsBeforecounts the strands ofrowOf.count_yarn_rowOf:rowOfhas as many yarn objects asrowOf1.count_yarn_rowOf_inv: one yarn whenInvholds.local_rows: the rows before and after an instruction, cut around the positionszandz + 1. The parts left and right of the cut are the same when the instruction changes no other position.
namespace FormalizeKnitting.Bridge.MR
open FormalizeKnitting.Knitout FormalizeKnitting.Knitout.MR
theorem seg_width (s : State) (i : Int) : rowWidth (seg s i) = yarnAt s i + frontAt s i + backAt s iProof
:= by
unfold seg
rw [rowWidth_append, rowWidth_append, stkO_width, stkO_width]
simp only [yarnAt, frontAt, backAt, nF, nB]
split <;> simp [rowWidth, Obj.width] theorem seg_count (s : State) (i : Int) : (seg s i).count .yarn = if s.carrier = i then 1 else 0Proof
:= by
unfold seg
have h : ∀ n, (stkO n).count Obj.yarn = 0 := fun n => List.count_eq_zero.mpr (yarn_notin_stkO n)
rw [List.count_append, List.count_append, h, h]
split <;> simp theorem rowOf_succ (s : State) (lo : Int) (w : Nat) : rowOf s lo (w + 1) = rowOf s lo w ++ seg s (lo + w)Proof
:= by
simp [rowOf, List.range_succ, List.flatMap_append] theorem rowOf_add (s : State) (lo : Int) (a b : Nat) :
rowOf s lo (a + b) = rowOf s lo a ++ rowOf s (lo + a) bProof
:= by
induction b with
| zero => simp [rowOf]
| succ b ih =>
rw [← Nat.add_assoc, rowOf_succ, ih, rowOf_succ]
have : lo + ((a + b : Nat) : Int) = lo + a + b := by push_cast; omega
rw [this]; simp theorem rowOf_one (s : State) (i : Int) : rowOf s i 1 = seg s iProof
:= by
rw [show (1 : Nat) = 0 + 1 from rfl, rowOf_succ]; simp [rowOf] theorem rowOf_congr {s t : State} {lo : Int} {w : Nat} (h : ∀ k : Nat, k < w → seg s (lo + k) = seg t (lo + k)) :
rowOf s lo w = rowOf t lo wProof
:= by
induction w with
| zero => rfl
| succ w ih => rw [rowOf_succ, rowOf_succ, ih (fun k hk => h k (by omega)), h w (by omega)] theorem rowWidth_rowOf (s : State) (lo : Int) : ∀ w : Nat, rowWidth (rowOf s lo w) = cellRowWidth (rowOf1 s lo w)Proof
| 0 => by simp [rowOf, rowOf1, points, rowWidth, cellRowWidth]
| w + 1 => by
rw [rowOf_succ, rowOf1_succ, rowWidth_append, cellRowWidth_append, rowWidth_rowOf s lo w, seg_width]
have := segW_eq s (lo + w)
unfold segW at this
rw [this] theorem count_yarn_rowOf (s : State) (lo : Int) :
∀ w : Nat, (rowOf s lo w).count .yarn = (rowOf1 s lo w).count .yarnProof
| 0 => by simp [rowOf, rowOf1, points]
| w + 1 => by
rw [rowOf_succ, rowOf1_succ, List.count_append, List.count_append, count_yarn_rowOf s lo w, seg_count,
seg1_count_yarn] theorem strandsBefore_rowOf (win : Window) (s : State) (i : Int) :
strandsBefore win s i = rowWidth (rowOf s win.lo (i - win.lo).toNat)Proof
:= by
rw [rowWidth_rowOf]; rfl theorem windowWidth_rowOf (win : Window) (s : State) : windowWidth win s = rowWidth (rowOf s win.lo win.w)Proof
:= by
rw [rowWidth_rowOf]; rfl theorem count_yarn_rowOf_inv {win : Window} {s : State} (h : Inv win s) :
(rowOf s win.lo win.w).count .yarn = 1Proof
:= by
rw [count_yarn_rowOf]; exact count_yarn_rowOf1 h theorem seg_congr {s t : State} {i : Int} (hr : t.rack = s.rack) (hc : (t.carrier = i) ↔ (s.carrier = i))
(hf : t.stk ⟨.f, i⟩ = s.stk ⟨.f, i⟩) (hb : t.stk ⟨.b, i - s.rack⟩ = s.stk ⟨.b, i - s.rack⟩) :
seg t i = seg s iProof
:= by
unfold seg nF nB
rw [hr, hf, hb]
by_cases h : s.carrier = i <;> simp [h, hc] theorem seg_far {s t : State} {z : Int} (hr : t.rack = s.rack)
(hloc : ∀ n : Needle, slot s.rack n ≠ z → t.stk n = s.stk n)
(hc : ∀ i, i ≠ z → i ≠ z + 1 → (t.carrier = i ↔ s.carrier = i)) :
∀ i, i ≠ z → i ≠ z + 1 → seg t i = seg s iProof
:= by
intro i h1 h2
apply seg_congr hr (hc i h1 h2)
· exact hloc _ (by simp [slot]; exact h1)
· exact hloc _ (by simp [slot]; exact h1) theorem local_rows {s s' : State} {win : Window} {z : Int} (h1 : win.lo ≤ z) (h2 : z + 1 < win.lo + win.w)
(hfar : ∀ i, i ≠ z → i ≠ z + 1 → seg s' i = seg s i) :
∃ P Q, rowOf s win.lo win.w = P ++ seg s z ++ seg s (z + 1) ++ Q ∧
rowOf s' win.lo win.w = P ++ seg s' z ++ seg s' (z + 1) ++ Q ∧
objsBefore win s z = P.length ∧ strandsBefore win s z = rowWidth P ∧
objsBefore win s (z + 1) = P.length + (seg s z).length ∧
strandsBefore win s (z + 1) = rowWidth P + rowWidth (seg s z) ∧
(s.carrier = z ∨ s.carrier = z + 1 → Obj.yarn ∉ P ∧ Obj.yarn ∉ Q)Proof
:= by
obtain ⟨lo, w⟩ := win
simp only at h1 h2
set a := (z - lo).toNat with ha
have hz : lo + a = z := by omega
have hw : w = a + 1 + 1 + (w - a - 2) := by omega
have key : ∀ t : State, rowOf t lo w = rowOf t lo a ++ seg t z ++ seg t (z + 1) ++ rowOf t (z + 2) (w - a - 2) := by
intro t
conv_lhs => rw [hw]
rw [rowOf_add, rowOf_add, rowOf_add, hz, rowOf_one, rowOf_one]
have e1 : lo + ((a + 1 : Nat) : Int) = z + 1 := by omega
have e2 : lo + ((a + 1 + 1 : Nat) : Int) = z + 2 := by omega
rw [e1, e2]
have hP : rowOf s' lo a = rowOf s lo a := rowOf_congr (fun k hk => hfar _ (by omega) (by omega))
have hQ : rowOf s' (z + 2) (w - a - 2) = rowOf s (z + 2) (w - a - 2) :=
rowOf_congr (fun k hk => hfar _ (by omega) (by omega))
have hb1 : (z + 1 - lo).toNat = a + 1 := by omega
refine ⟨rowOf s lo a, rowOf s (z + 2) (w - a - 2), key s, by rw [key s', hP, hQ], rfl, ?_, ?_, ?_, ?_⟩
· rw [strandsBefore_rowOf]
· simp only [objsBefore]; rw [hb1, rowOf_succ, hz]; simp
· rw [strandsBefore_rowOf]; simp only; rw [hb1, rowOf_succ, hz, rowWidth_append]
· intro hc
have c1 := count_yarn_rowOf s lo a
have c2 := count_yarn_rowOf s (z + 2) (w - a - 2)
rw [count_yarn_rowOf1_aux] at c1 c2
constructor
· intro hm; have := List.count_pos_iff.mpr hm; rw [c1, ite_eq_right (by omega)] at this; omega
· intro hm; have := List.count_pos_iff.mpr hm; rw [c2, ite_eq_right (by omega)] at this; omega From one instruction to a program
sim_correct: a program is read correctly when each of its instructions is read correctly (SimOpCorrect).
| Declaration | Content |
|---|---|
inv_step |
Each instruction that step accepts keeps MR.Inv (the state is inside the window). |
SimOpCorrect op |
The statement for one instruction op. |
sim_correct |
The statement for a program, from SimOpCorrect for each instruction. Proof by induction, with D_wf. |
namespace FormalizeKnitting.Bridge.MR
open FormalizeKnitting.Knitout FormalizeKnitting.Knitout.MR
theorem step_ok {s s' : State} {op : Op} :
step s op = .ok s' ↔ refusal s op = none ∧ s' = apply s opProof
:= by
unfold step
split
· rename_i h; simp [h, eq_comm]
· rename_i r h; simp [h] theorem validLin_of_full {s : State} {op : Op} (h : refusal s op = none) : validLin s opProof
:= by
unfold refusal at h; unfold validLin
split at h
· simp at h
· assumption theorem inv_step {win : Window} {s s' : State} {op : Op} (hI : Inv win s) (hs : step s op = .ok s')
(hw : windowOk win s op = true) (hn : NamedIn win op) : Inv win s'Proof
:= by
obtain ⟨hv, rfl⟩ := step_ok.mp hs
obtain ⟨hB, hL, hC⟩ := hI
refine ⟨backIn_apply hB hn, ?_, ?_⟩
· intro q hq
cases op with
| xfer a b =>
simp only [windowOk, Bool.and_eq_true] at hw
simp only [apply, upd_apply] at hq ⊢
by_cases hb : q = b
· subst hb; exact (fun h => ⟨by omega, by omega⟩) (inside_bounds hw.2)
· by_cases ha : q = a
· simp [ha] at hq
exact absurd hq.1 (aligned_ne (validLin_xfer (validLin_of_full hv)))
· simp only [hb, ha, ite_false] at hq; exact hL q hq
| rack r =>
simp only [apply] at hq ⊢
obtain ⟨bd, j⟩ := q
cases bd with
| f => exact hL _ hq
| b =>
have := inside_bounds (windowOk_rack hw (hB j hq) hq)
simp only [slot]; omega
| knit d n | tuck d n | drop n =>
simp only [windowOk] at hw
simp only [apply, upd_apply] at hq ⊢
by_cases hq' : q = n
· subst hq'; exact (fun h => ⟨by omega, by omega⟩) (inside_bounds hw)
· simp only [hq', ite_false] at hq; exact hL q hq
| split d n m =>
simp only [windowOk, Bool.and_eq_true] at hw
simp only [apply, upd_apply] at hq ⊢
by_cases hm : q = m
· subst hm; exact (fun h => ⟨by omega, by omega⟩) (inside_bounds hw.2)
· by_cases hq' : q = n
· subst hq'; exact (fun h => ⟨by omega, by omega⟩) (inside_bounds hw.1)
· simp only [hm, hq', ite_false] at hq; exact hL q hq
| miss d n => exact hL q hq
· cases op with
| xfer a b => exact hC
| rack r => exact hC
| drop n => exact hC
| knit d n | tuck d n | miss d n =>
simp only [windowOk] at hw; exact carrier_leaves hw
| split d n m =>
simp only [windowOk, Bool.and_eq_true] at hw; exact carrier_leaves hw.1 def SimOpCorrect (op : Op) : Prop :=
∀ (win : Window) (s s' : State), step s op = .ok s' → windowOk win s op = true → Inv win s →
∃ M, D (rowOf s win.lo win.w) (simOp win s op) = some (rowOf s' win.lo win.w, M) ∧
Iso (windowWidth win s) M (expand sig (denoteOp win s op))theorem sim_correct (hop : ∀ op, SimOpCorrect op) (win : Window) :
∀ (s : State) (ops : List Op) (t : State) (B : BWord Label), Inv win s → (∀ op ∈ ops, NamedIn win op) →
denote win s ops = some (t, B) →
∃ M, D (rowOf s win.lo win.w) (sim win s ops) = some (rowOf t win.lo win.w, M) ∧
Iso (windowWidth win s) M (expand sig B)Proof
| s, [], t, B, _, _, h => by
simp only [denote, Option.some.injEq, Prod.mk.injEq] at h
obtain ⟨rfl, rfl⟩ := h
exact ⟨[], rfl, Iso.refl _ _⟩
| s, op :: ops, t, B, hI, hn, h => by
simp only [denote] at h
split at h
· simp at h
· rename_i s' hs
split at h
· rename_i hw
split at h
· simp at h
· rename_i t' u hd
simp only [Option.some.injEq, Prod.mk.injEq] at h
obtain ⟨rfl, rfl⟩ := h
have hI' := inv_step hI hs hw (hn op (by simp))
obtain ⟨M₁, hD₁, hiso₁⟩ := hop op win s s' hs hw hI
obtain ⟨M₂, hD₂, hiso₂⟩ := sim_correct hop win s' ops t' u hI' (fun o ho => hn o (by simp [ho])) hd
have hsim : sim win s (op :: ops) = simOp win s op ++ sim win s' ops := by simp [sim, hs]
have hD := D_app hD₁ hD₂
refine ⟨M₁ ++ M₂, by rw [hsim]; exact hD, ?_⟩
have hwf := D_wf hD
have hwf₁ := D_wf hD₁
rw [← windowWidth_rowOf, ← windowWidth_rowOf] at hwf₁
rw [← windowWidth_rowOf] at hwf
rw [expand_append]
have e1 : Iso (windowWidth win s) (M₁ ++ M₂) (expand sig (denoteOp win s op) ++ M₂) :=
Iso.suffix M₂ hiso₁ (by rw [hwf]; rfl)
have hE : topWidth (windowWidth win s) (expand sig (denoteOp win s op)) =
some (windowWidth win s') := by rw [← hiso₁.topWidth_eq, hwf₁]
exact e1.trans (Iso.prefix hE hiso₂)
· simp at h theorem inv_denote (win : Window) : ∀ (s : State) (ops : List Op) (t : State) (B : BWord Label), Inv win s →
(∀ op ∈ ops, NamedIn win op) → denote win s ops = some (t, B) → Inv win tProof
| s, [], t, B, h, _, hd => by
simp only [denote, Option.some.injEq, Prod.mk.injEq] at hd; rw [← hd.1]; exact h
| s, op :: ops, t, B, h, hn, hd => by
simp only [denote] at hd
split at hd
· simp at hd
· rename_i s' hs
split at hd
· rename_i hw
split at hd
· simp at hd
· rename_i t'' u hd'
simp only [Option.some.injEq, Prod.mk.injEq] at hd
rw [← hd.1]
exact inv_denote win s' ops t'' u (inv_step h hs hw (hn op (by simp)))
(fun o ho => hn o (by simp [ho])) hd'
· simp at hd Transfers and drops
SimOpCorrect for transfers and drops. For each, the drawing of the shorthand word is literally the drawing of the
instruction (Iso.refl).
| Theorem | Shorthand word |
|---|---|
simOp_xfer |
Empty, or one merge. A merge draws nothing, and a transfer draws nothing. |
simOp_drop |
One drop. It draws one cap for each loop of the stack (expand_caps). |
namespace FormalizeKnitting.Bridge.MR
open FormalizeKnitting.Knitout FormalizeKnitting.Knitout.MR
theorem expand_liftB (u : BWord PlainLabel) : expand sig (liftB u) = expand plainSig uProof
:= by
induction u with
| nil => rfl
| cons e u ih =>
simp only [liftB, List.map_cons, expand, List.flatMap_cons] at ih ⊢
rw [ih]
cases e with
| gen g => rfl
| box l i =>
cases l with
| tuck d => rfl
| knit d bd k => rfl
| split d bd => simp [BEv.inside, PlainLabel.toLabel, sig, plainSig, Label.interior, PlainLabel.interior, splitInteriorK_one] theorem stkO_add_zero (n : Nat) : stkO 0 ++ stkO n = stkO nProof
:= rfl theorem stkO_zero_add (n : Nat) : stkO n ++ stkO 0 = stkO nProof
:= by simp [stkO] theorem yarn_mem_of_count {r : Row} (h : r.count .yarn = 1) : Obj.yarn ∈ rProof
:= List.count_pos_iff.mp (by omega) theorem simOp_xfer (a b : Needle) : SimOpCorrect (.xfer a b)Proof
:= by
intro win s s' hs hw hI
obtain ⟨hv, rfl⟩ := step_ok.mp hs
have hal := validLin_xfer (validLin_of_full hv)
have hin : win.inside (slot s.rack a) = true := by simp only [windowOk, Bool.and_eq_true] at hw; exact hw.1
have hb := inside_bounds hin
set z := slot s.rack a with hz
simp only [aligned, decide_eq_true_eq] at hal
obtain ⟨hbed, hzb⟩ := hal
have hloc : ∀ m : Needle, slot s.rack m ≠ z → (apply s (.xfer a b)).stk m = s.stk m := by
intro m hm
simp only [apply, upd_apply]
rw [ite_eq_right (by rintro rfl; exact hm hzb.symm), ite_eq_right (by rintro rfl; exact hm rfl)]
obtain ⟨P, Q, hr, hr', ho, hst, -, -, -⟩ :=
local_rows (s := s) (s' := apply s (.xfer a b)) (win := win) (z := z) (by omega) (by omega)
(seg_far rfl hloc (fun i _ _ => Iff.rfl))
have h1 := count_yarn_rowOf_inv hI
have e1 : seg (apply s (.xfer a b)) (z + 1) = seg s (z + 1) :=
seg_congr rfl Iff.rfl (hloc _ (by simp [slot])) (hloc _ (by simp [slot]))
set Yz : Row := if s.carrier = z then [Obj.yarn] else []
have hYl : Yz.length = nY s z := by simp only [Yz, nY]; split <;> rfl
have hseg : seg s z = Yz ++ stkO (nF s z) ++ stkO (nB s z) := rfl
have hab : a ≠ b := by rintro rfl; exact hbed rfl
obtain ⟨F, B, hFa, hBb, hseg', hFB⟩ : ∃ F B, (s.stk a).length = F ∧ (s.stk b).length = B ∧
seg (apply s (.xfer a b)) z = Yz ++ stkO (F + B) ∧
((nF s z = F ∧ nB s z = B) ∨ (nF s z = B ∧ nB s z = F)) := by
obtain ⟨ba, ia⟩ := a
obtain ⟨bb, ib⟩ := b
cases ba <;> cases bb <;> simp only [ne_eq, not_true_eq_false] at hbed
· have hzib : z - s.rack = ib := by simp only [hz, slot] at hzb ⊢; omega
have nF' : nF (apply s (.xfer ⟨.f, ia⟩ ⟨.b, ib⟩)) z = 0 := by
show ((Function.update (Function.update s.stk ⟨.f, ia⟩ []) ⟨.b, ib⟩
(xferStack (s.stk ⟨.f, ia⟩) (s.stk ⟨.b, ib⟩))) ⟨.f, z⟩).length = 0
rw [Function.update_of_ne (by simp), show z = ia from rfl, Function.update_self]; rfl
have nB' : nB (apply s (.xfer ⟨.f, ia⟩ ⟨.b, ib⟩)) z = (s.stk ⟨.f, ia⟩).length + (s.stk ⟨.b, ib⟩).length := by
show ((Function.update (Function.update s.stk ⟨.f, ia⟩ []) ⟨.b, ib⟩
(xferStack (s.stk ⟨.f, ia⟩) (s.stk ⟨.b, ib⟩))) ⟨.b, z - s.rack⟩).length = _
rw [hzib, Function.update_self]; simp [xferStack]
refine ⟨_, _, rfl, rfl, ?_, Or.inl ⟨rfl, ?_⟩⟩
· show Yz ++ stkO (nF (apply s (.xfer ⟨.f, ia⟩ ⟨.b, ib⟩)) z) ++ stkO (nB (apply s (.xfer ⟨.f, ia⟩ ⟨.b, ib⟩)) z) = _
rw [nF', nB', List.append_assoc, stkO_add_zero]
· show (s.stk ⟨.b, z - s.rack⟩).length = _
rw [hzib]
· have hzib : z = ib := by simp only [hz, slot] at hzb ⊢; omega
have hzia : z - s.rack = ia := by simp only [hz, slot]; omega
have nB' : nB (apply s (.xfer ⟨.b, ia⟩ ⟨.f, ib⟩)) z = 0 := by
show ((Function.update (Function.update s.stk ⟨.b, ia⟩ []) ⟨.f, ib⟩
(xferStack (s.stk ⟨.b, ia⟩) (s.stk ⟨.f, ib⟩))) ⟨.b, z - s.rack⟩).length = 0
rw [Function.update_of_ne (by simp), hzia, Function.update_self]; rfl
have nF' : nF (apply s (.xfer ⟨.b, ia⟩ ⟨.f, ib⟩)) z = (s.stk ⟨.b, ia⟩).length + (s.stk ⟨.f, ib⟩).length := by
show ((Function.update (Function.update s.stk ⟨.b, ia⟩ []) ⟨.f, ib⟩
(xferStack (s.stk ⟨.b, ia⟩) (s.stk ⟨.f, ib⟩))) ⟨.f, z⟩).length = _
rw [hzib, Function.update_self]; simp [xferStack]
refine ⟨_, _, rfl, rfl, ?_, Or.inr ⟨?_, ?_⟩⟩
· show Yz ++ stkO (nF (apply s (.xfer ⟨.b, ia⟩ ⟨.f, ib⟩)) z) ++ stkO (nB (apply s (.xfer ⟨.b, ia⟩ ⟨.f, ib⟩)) z) = _
rw [nF', nB', List.append_assoc, stkO_zero_add]
· show (s.stk ⟨.f, z⟩).length = _
rw [hzib]
· show (s.stk ⟨.b, z - s.rack⟩).length = _
rw [hzia]
have hsimc : simOp win s (.xfer a b) = if F = 0 ∨ B = 0 then [] else [.merge (P.length + nY s z)] := by
simp only [simOp, ← hz, ho, hFa, hBb]
have hden : expand sig (denoteOp win s (.xfer a b)) = [] := rfl
rw [hden]
by_cases h0 : F = 0 ∨ B = 0
· rw [hsimc, ite_eq_left h0]
refine ⟨[], ?_, Iso.refl _ _⟩
have : seg s z = seg (apply s (.xfer a b)) z := by
rw [hseg, hseg']
rcases hFB with ⟨e, e'⟩ | ⟨e, e'⟩ <;> rw [e, e'] <;> rcases h0 with rfl | rfl <;>
simp [stkO_zero_add, stkO_add_zero]
rw [hr, hr', this, e1]; rfl
· rw [hsimc, ite_eq_right h0]
have hF : 0 < F := by omega
have hB : 0 < B := by omega
have hpair : stkO (nF s z) ++ stkO (nB s z) = [.stack (nF s z), .stack (nB s z)] ∧ nF s z + nB s z = F + B ∧
0 < nF s z ∧ 0 < nB s z := by
rcases hFB with ⟨e, e'⟩ | ⟨e, e'⟩ <;> rw [e, e'] <;> refine ⟨?_, by omega, by omega, by omega⟩ <;>
simp [stkO, show F ≠ 0 by omega, show B ≠ 0 by omega]
obtain ⟨hp, hsum, hF', hB'⟩ := hpair
have r0 : rowOf s win.lo win.w = (P ++ Yz) ++ .stack (nF s z) :: .stack (nB s z) :: (seg s (z + 1) ++ Q) := by
rw [hr, hseg, List.append_assoc Yz, hp]; simp
have r1 : rowOf (apply s (.xfer a b)) win.lo win.w = (P ++ Yz) ++ .stack (nF s z + nB s z) :: (seg s (z + 1) ++ Q) := by
rw [hr', hseg', e1, hsum]
have : stkO (F + B) = [.stack (F + B)] := by simp [stkO]; omega
rw [this]; simp
have hy : Obj.yarn ∈ (P ++ Yz) ++ (seg s (z + 1) ++ Q) := by
have := yarn_mem_of_count h1
rw [r0] at this
simp only [List.mem_append, List.mem_cons, reduceCtorEq, false_or] at this ⊢
tauto
have hl : (P ++ Yz).length = P.length + nY s z := by simp [hYl]
refine ⟨[], ?_, Iso.refl _ _⟩
rw [r0, r1, ← hl, D_single (rs_merge _ _ _ _ hF' hB' hy)] theorem expand_caps {win : Window} {s : State} {n : Needle} :
expand sig (denoteOp win s (.drop n)) = List.replicate (s.stk n).length (Gen.cap
(strandsBefore win s (slot s.rack n) + yarnAt s (slot s.rack n) +
(if n.bed = .b then frontAt s (slot s.rack n) else 0)))Proof
:= by
show expand sig (liftB (denoteOp1 win s (.drop n))) = _
rw [expand_liftB]
exact expand_plainB' plainSig _ theorem drop_len_pos {s : State} {n : Needle} (hv : refusal s (.drop n) = none) : 0 < (s.stk n).lengthProof
:= by
have := validLin_of_full hv
unfold validLin linRefusal at this
by_contra hc
have h0 : (erase s).L n = 0 := by simp only [erase]; omega
simp [h0] at this theorem simOp_drop (n : Needle) : SimOpCorrect (.drop n)Proof
:= by
intro win s s' hs hw hI
obtain ⟨hv, rfl⟩ := step_ok.mp hs
have hk := drop_len_pos hv
have hin : win.inside (slot s.rack n) = true := by simpa [windowOk] using hw
have hb := inside_bounds hin
set z := slot s.rack n with hz
have hloc : ∀ m : Needle, slot s.rack m ≠ z → (apply s (.drop n)).stk m = s.stk m :=
fun m hm => upd_ne (needle_ne_of_slot (a := n) rfl hm)
obtain ⟨P, Q, hr, hr', ho, hst, -, -, -⟩ :=
local_rows (s := s) (s' := apply s (.drop n)) (win := win) (z := z) (by omega) (by omega)
(seg_far rfl hloc (fun i _ _ => Iff.rfl))
have h1 := count_yarn_rowOf_inv hI
have e1 : seg (apply s (.drop n)) (z + 1) = seg s (z + 1) :=
seg_congr rfl Iff.rfl (hloc _ (by simp [slot])) (hloc _ (by simp [slot]))
set Yz : Row := if s.carrier = z then [Obj.yarn] else []
have hYl : Yz.length = nY s z := by simp only [Yz, nY]; split <;> rfl
have hYw : rowWidth Yz = yarnAt s z := by simp only [Yz, yarnAt]; split <;> rfl
have hden := expand_caps (win := win) (s := s) (n := n)
have hy := yarn_mem_of_count h1
obtain ⟨bd, j⟩ := n
cases bd with
| f =>
have hzj : z = j := rfl
have ek : (s.stk ⟨.f, j⟩).length = nF s z := by rw [hzj]; rfl
have nF' : nF (apply s (.drop ⟨.f, j⟩)) z = 0 := by
show ((Function.update s.stk ⟨.f, j⟩ []) ⟨.f, z⟩).length = 0
rw [hzj, Function.update_self]; rfl
have nB' : nB (apply s (.drop ⟨.f, j⟩)) z = nB s z := by
show ((Function.update s.stk ⟨.f, j⟩ []) ⟨.b, z - s.rack⟩).length = _
rw [Function.update_of_ne (by simp)]; rfl
have hsk : stkO (nF s z) = [.stack (nF s z)] := by simp [stkO]; omega
have r0 : rowOf s win.lo win.w = (P ++ Yz) ++ .stack (nF s z) :: (stkO (nB s z) ++ seg s (z + 1) ++ Q) := by
rw [hr, show seg s z = Yz ++ stkO (nF s z) ++ stkO (nB s z) from rfl, hsk]; simp
have r1 : rowOf (apply s (.drop ⟨.f, j⟩)) win.lo win.w = (P ++ Yz) ++ (stkO (nB s z) ++ seg s (z + 1) ++ Q) := by
rw [hr', e1, show seg (apply s (.drop ⟨.f, j⟩)) z = Yz ++ stkO (nF (apply s (.drop ⟨.f, j⟩)) z) ++
stkO (nB (apply s (.drop ⟨.f, j⟩)) z) from rfl, nF', nB']; simp [stkO]
have hy' : Obj.yarn ∈ (P ++ Yz) ++ (stkO (nB s z) ++ seg s (z + 1) ++ Q) := by
rw [r0] at hy; simp only [List.mem_append, List.mem_cons, reduceCtorEq, false_or] at hy ⊢; tauto
have hsim : simOp win s (.drop ⟨.f, j⟩) = [.drop (P ++ Yz).length] := by
simp only [simOp, ← hz, ho, List.length_append, hYl, reduceCtorEq, ite_false, Nat.add_zero]
refine ⟨_, ?_, Iso.refl _ _⟩
rw [hsim, r0, D_single (rs_drop _ _ _ (by rw [← ek]; exact hk) hy'), r1, hden]
simp only [← hz, hst, ek, reduceCtorEq, ite_false, Nat.add_zero, rowWidth_append, hYw]
| b =>
have hzj : z - s.rack = j := by simp only [hz, slot]; omega
have ek : (s.stk ⟨.b, j⟩).length = nB s z := by simp only [nB, hzj]
have nB' : nB (apply s (.drop ⟨.b, j⟩)) z = 0 := by
show ((Function.update s.stk ⟨.b, j⟩ []) ⟨.b, z - s.rack⟩).length = 0
rw [hzj, Function.update_self]; rfl
have nF' : nF (apply s (.drop ⟨.b, j⟩)) z = nF s z := by
show ((Function.update s.stk ⟨.b, j⟩ []) ⟨.f, z⟩).length = _
rw [Function.update_of_ne (by simp)]; rfl
have hsk : stkO (nB s z) = [.stack (nB s z)] := by simp [stkO]; omega
have r0 : rowOf s win.lo win.w = (P ++ Yz ++ stkO (nF s z)) ++ .stack (nB s z) :: (seg s (z + 1) ++ Q) := by
rw [hr, show seg s z = Yz ++ stkO (nF s z) ++ stkO (nB s z) from rfl, hsk]; simp
have r1 : rowOf (apply s (.drop ⟨.b, j⟩)) win.lo win.w = (P ++ Yz ++ stkO (nF s z)) ++ (seg s (z + 1) ++ Q) := by
rw [hr', e1, show seg (apply s (.drop ⟨.b, j⟩)) z = Yz ++ stkO (nF (apply s (.drop ⟨.b, j⟩)) z) ++
stkO (nB (apply s (.drop ⟨.b, j⟩)) z) from rfl, nF', nB']; simp [stkO]
have hy' : Obj.yarn ∈ (P ++ Yz ++ stkO (nF s z)) ++ (seg s (z + 1) ++ Q) := by
rw [r0] at hy; simp only [List.mem_append, List.mem_cons, reduceCtorEq, false_or] at hy ⊢; tauto
have hsim : simOp win s (.drop ⟨.b, j⟩) = [.drop (P ++ Yz ++ stkO (nF s z)).length] := by
simp only [simOp, ← hz, ho, List.length_append, hYl, stkO_length, ite_true]
refine ⟨_, ?_, Iso.refl _ _⟩
rw [hsim, r0, D_single (rs_drop _ _ _ (by rw [← ek]; exact hk) hy'), r1, hden]
simp only [← hz, hst, ek, ite_true, rowWidth_append, hYw, stkO_width, frontAt, nF] The instruction cases
MissCase, TuckCase, KnitCase, SplitCase and RackCase are SimOpCorrect for one kind of instruction each.
simOpCorrect_all gives SimOpCorrect for every instruction from these five cases. Each case has a proof in a
later file.
| Case | Proof | Drawing of the shorthand word |
|---|---|---|
| transfer | simOp_xfer |
literal |
| drop | simOp_drop |
literal |
MissCase |
missCase |
literal |
TuckCase |
tuckCase |
literal |
KnitCase |
knitCase |
literal |
SplitCase |
splitCase |
literal |
RackCase |
Rack.rackCase |
literal for a rack up; Iso by far commutation of crossings for a rack down |
- The
exampleat the end shows that a rack down is not literal. At one position, a back stack of one loop passes the carrier and a front stack of one loop. The one block swap of the drawing is not the word of the two exchanges. The two are equal up to far commutation (MR.Rack.swapBlocks_left_split).
namespace FormalizeKnitting.Bridge.MR
open FormalizeKnitting.Knitout FormalizeKnitting.Knitout.MR
def MissCase : Prop := ∀ d n, SimOpCorrect (.miss d n)
def TuckCase : Prop := ∀ d n, SimOpCorrect (.tuck d n)
def KnitCase : Prop := ∀ d n, SimOpCorrect (.knit d n)
def SplitCase : Prop := ∀ d n m, SimOpCorrect (.split d n m)
def RackCase : Prop := ∀ r, SimOpCorrect (.rack r)theorem simOpCorrect_all (hm : MissCase) (ht : TuckCase) (hk : KnitCase) (hs : SplitCase) (hr : RackCase) :
∀ op, SimOpCorrect opProof
| .xfer a b => simOp_xfer a b
| .drop n => simOp_drop n
| .miss d n => hm d n
| .tuck d n => ht d n
| .knit d n => hk d n
| .split d n m => hs d n m
| .rack r => hr r example : swapBlocks 0 (1 + 2) 2 true ≠ swapBlocks 1 2 2 true ++ swapBlocks 0 1 2 trueProof
:= by decide Misses
missCase proves MissCase. The drawing of the shorthand word of a miss is literally the drawing of the miss.
setupR,setupL: the stack rows before and after an instruction that moves the carrier across positionzand changes the needles only atz. Before:P, the yarn, the front stack, the back stack,R, in the order of the direction. After: the same with the yarn on the other side. The proofs for knits, tucks and splits also use them.carrier_of_lin: before a miss, tuck, knit or split, the carrier is atarrivesFrom d z.
namespace FormalizeKnitting.Bridge.MR
open FormalizeKnitting.Knitout FormalizeKnitting.Knitout.MR FormalizeKnitting.Bridge.MR
theorem ob_cases (n : Nat) : (ob n = 0 ∧ n = 0) ∨ (ob n = 1 ∧ 0 < n)Proof
:= by
unfold ob; split <;> omega theorem ob_pos {n : Nat} (h : 0 < n) : ob n = 1Proof
:= by simp [ob]; omega theorem stkO_pos {n : Nat} (h : 0 < n) : stkO n = [.stack n]Proof
:= by simp [stkO]; omega theorem xs_congr {a b c : Nat} (f : Bool) (h : c = 0 ∨ a = b) : xs a f c = xs b f cProof
:= by
rcases h with rfl | rfl <;> rfl theorem ms_congr {a b c : Nat} (h : c = 0 ∨ a = b) : ms a c = ms b cProof
:= by
rcases h with rfl | rfl <;> rfl theorem carrier_of_lin {s : State} {op : Op} {d : Dir} {n : Needle} (hop : op = .miss d n ∨ op = .tuck d n ∨
op = .knit d n ∨ ∃ m, op = .split d n m) (h : refusal s op = none) :
s.carrier = arrivesFrom d (slot s.rack n)Proof
:= by
have hv := validLin_of_full h
unfold validLin at hv
rcases hop with rfl | rfl | rfl | ⟨m, rfl⟩ <;> simp only [linRefusal, erase] at hv <;>
split_ifs at hv with h1 <;> simp_all theorem far_carrier {s s' : State} {d : Dir} {z : Int} (hc : s.carrier = arrivesFrom d z)
(hc' : s'.carrier = leavesTo d z) : ∀ i, i ≠ z → i ≠ z + 1 → (s'.carrier = i ↔ s.carrier = i)Proof
:= by
intro i h1 h2
rw [hc, hc']; cases d <;> simp only [leavesTo, arrivesFrom] <;> constructor <;> intro h <;> omega theorem stkO_ne_yarn (a b : Nat) (Z : Row) : Obj.yarn ∉ stkO a ++ stkO b ++ Z ↔ Obj.yarn ∉ ZProof
:= by
simp [yarn_notin_stkO] theorem setupR {win : Window} {s s' : State} {z : Int} (hb : win.lo < z ∧ z < win.lo + win.w - 1)
(hr : s'.rack = s.rack) (hloc : ∀ m : Needle, slot s.rack m ≠ z → s'.stk m = s.stk m)
(hc : s.carrier = z) (hc' : s'.carrier = z + 1) :
∃ P R, Obj.yarn ∉ P ∧ objsBefore win s z = P.length ∧ strandsBefore win s z = rowWidth P ∧
rowOf s win.lo win.w = P ++ Obj.yarn :: (stkO (nF s z) ++ (stkO (nB s z) ++ R)) ∧
rowOf s' win.lo win.w = P ++ (stkO (nF s' z) ++ (stkO (nB s' z) ++ Obj.yarn :: R))Proof
:= by
have hcar := far_carrier (d := .R) (s := s) (s' := s') (z := z) hc hc'
obtain ⟨P, Q, h0, h1, ho, hst, -, -, hy⟩ :=
local_rows (s := s) (s' := s') (win := win) (z := z) (by omega) (by omega) (seg_far hr hloc hcar)
have e1 : seg s' (z + 1) = Obj.yarn :: seg s (z + 1) := by
have a1 : s'.stk ⟨.f, z + 1⟩ = s.stk ⟨.f, z + 1⟩ := hloc _ (by simp [slot])
have a2 : s'.stk ⟨.b, z + 1 - s.rack⟩ = s.stk ⟨.b, z + 1 - s.rack⟩ := hloc _ (by simp [slot])
simp only [seg, nF, nB, hr, a1, a2, hc, hc', ite_true, ite_eq_right (show ¬ z = z + 1 by omega)]; simp
have e0 : seg s z = Obj.yarn :: (stkO (nF s z) ++ stkO (nB s z)) := by
simp only [seg, hc, ite_true]; simp
have e0' : seg s' z = stkO (nF s' z) ++ stkO (nB s' z) := by
simp only [seg, hc', ite_eq_right (show ¬ z + 1 = z by omega)]; simp
refine ⟨P, seg s (z + 1) ++ Q, (hy (Or.inl hc)).1, ho, hst, ?_, ?_⟩
· rw [h0, e0]; simp
· rw [h1, e0', e1]; simp theorem setupL {win : Window} {s s' : State} {z : Int} (hb : win.lo < z ∧ z < win.lo + win.w - 1)
(hr : s'.rack = s.rack) (hloc : ∀ m : Needle, slot s.rack m ≠ z → s'.stk m = s.stk m)
(hc : s.carrier = z + 1) (hc' : s'.carrier = z) :
∃ P R, Obj.yarn ∉ P ∧ objsBefore win s (z + 1) = P.length + ob (nF s z) + ob (nB s z) ∧
strandsBefore win s (z + 1) = rowWidth P + 2 * nF s z + 2 * nB s z ∧
rowOf s win.lo win.w = P ++ (stkO (nF s z) ++ (stkO (nB s z) ++ Obj.yarn :: R)) ∧
rowOf s' win.lo win.w = P ++ Obj.yarn :: (stkO (nF s' z) ++ (stkO (nB s' z) ++ R))Proof
:= by
have hcar := far_carrier (d := .L) (s := s) (s' := s') (z := z) hc hc'
obtain ⟨P, Q, h0, h1, -, -, ho1, hst1, hy⟩ :=
local_rows (s := s) (s' := s') (win := win) (z := z) (by omega) (by omega) (seg_far hr hloc hcar)
have e1 : seg s (z + 1) = Obj.yarn :: seg s' (z + 1) := by
have a1 : s'.stk ⟨.f, z + 1⟩ = s.stk ⟨.f, z + 1⟩ := hloc _ (by simp [slot])
have a2 : s'.stk ⟨.b, z + 1 - s.rack⟩ = s.stk ⟨.b, z + 1 - s.rack⟩ := hloc _ (by simp [slot])
simp only [seg, nF, nB, hr, a1, a2, hc, hc', ite_true, ite_eq_right (show ¬ z = z + 1 by omega)]; simp
have e0 : seg s z = stkO (nF s z) ++ stkO (nB s z) := by
simp only [seg, hc, ite_eq_right (show ¬ z + 1 = z by omega)]; simp
have e0' : seg s' z = Obj.yarn :: (stkO (nF s' z) ++ stkO (nB s' z)) := by
simp only [seg, hc', ite_true]; simp
refine ⟨P, seg s' (z + 1) ++ Q, (hy (Or.inr hc)).1, ?_, ?_, ?_, ?_⟩
· rw [ho1, e0]; simp [stkO_length, Nat.add_assoc]
· rw [hst1, e0, rowWidth_append, stkO_width, stkO_width, Nat.add_assoc]
· rw [h0, e0, e1]; simp
· rw [h1, e0']; simp Miss
theorem missCase : MissCaseProof
:= by
intro d n win s s' hs hw _
obtain ⟨hv, rfl⟩ := step_ok.mp hs
have hc := carrier_of_lin (Or.inl rfl) hv
have hb := inside_bounds (show win.inside (slot s.rack n) = true by simpa [windowOk] using hw)
set z := slot s.rack n with hz
have hloc : ∀ m : Needle, slot s.rack m ≠ z → (apply s (.miss d n)).stk m = s.stk m := fun _ _ => rfl
have hden : expand sig (denoteOp win s (.miss d n)) = expand plainSig (denoteOp1 win s (.miss d n)) :=
expand_liftB _
rw [hden]
have eF : nF (apply s (.miss d n)) z = nF s z := rfl
have eB : nB (apply s (.miss d n)) z = nB s z := rfl
cases d with
| R =>
obtain ⟨P, R, -, ho, hst, h0, h1⟩ := setupR (win := win) (s := s) (s' := apply s (.miss .R n)) (z := z) hb rfl hloc hc rfl
rw [eF, eB] at h1
have d1 := D_passR P (stkO (nB s z) ++ R) (nF s z) false
have d2 := D_passR (P ++ stkO (nF s z)) R (nB s z) true
simp only [List.append_assoc] at d1 d2
have hsim : simOp win s (.miss .R n) =
xs P.length false (ob (nF s z)) ++ xs (P ++ stkO (nF s z)).length true (ob (nB s z)) := by
simp only [simOp, arrivesFrom, ← hz, ho, List.length_append, stkO_length]
refine ⟨_, ?_, Iso.refl _ _⟩
rw [hsim, h0, D_app d1 d2, h1]
simp only [denoteOp1, arrivesFrom, ← hz, hst, expand_append, expand_swapB, rowWidth_append, stkO_width,
frontAt, backAt, nF, nB]
| L =>
obtain ⟨P, R, -, ho, hst, h0, h1⟩ := setupL (win := win) (s := s) (s' := apply s (.miss .L n)) (z := z) hb rfl hloc hc rfl
rw [eF, eB] at h1
have d1 := D_passL (P ++ stkO (nF s z)) R (nB s z) false
have d2 := D_passL P (stkO (nB s z) ++ R) (nF s z) true
simp only [List.append_assoc] at d1 d2
have hsim : simOp win s (.miss .L n) =
xs ((P ++ stkO (nF s z)).length + ob (nB s z) - 1) false (ob (nB s z)) ++
xs (P.length + ob (nF s z) - 1) true (ob (nF s z)) := by
simp only [simOp, arrivesFrom, ← hz, ho, List.length_append, stkO_length]
congr 1
apply xs_congr; rcases ob_cases (nB s z) with ⟨h, -⟩ | ⟨h, -⟩ <;> omega
refine ⟨_, ?_, Iso.refl _ _⟩
rw [hsim, h0, D_app d1 d2, h1]
simp only [denoteOp1, arrivesFrom, ← hz, hst, expand_append, expand_swapB, rowWidth_append, stkO_width,
frontAt, backAt, nF, nB, Option.some.injEq, Prod.mk.injEq, true_and]
congr 2 <;> omega Knits
knitCase proves KnitCase. The drawing of the shorthand word of a knit is literally denoteOp's drawing.
- The knit goes through all
k ≥ 1loops of the needle. Both beds, both directions. The needle across can be empty or loaded. - The word is one stack
knitand the yarn passing the stack across (simOp). knit_f: the front bed.knit_b: the back bed.
namespace FormalizeKnitting.Bridge.MR
open FormalizeKnitting.Knitout FormalizeKnitting.Knitout.MR FormalizeKnitting.Bridge.MR
theorem stkO_one : stkO 1 = [.stack 1]Proof
:= rfl theorem rowWidth_single (k : Nat) : rowWidth [Obj.stack k] = 2 * kProof
:= by simp [rowWidth, Obj.width] theorem rowWidth_yarn : rowWidth [Obj.yarn] = 1Proof
:= by simp [rowWidth, Obj.width] theorem knit_pos_full {s : State} {d : Dir} {n : Needle} (hv : refusal s (.knit d n) = none) :
0 < (s.stk n).lengthProof
:= by
have hne := validLin_knit (validLin_of_full hv)
cases h : s.stk n with
| nil => exact absurd h hne
| cons a l => simp theorem expand_box_knit (win : Window) (s : State) (d : Dir) (n : Needle) :
expand sig (denoteOp win s (.knit d n)) = expand plainSig (denoteOp1 win s (.knit d n))Proof
:=
expand_liftB _ theorem knit_f (d : Dir) (j : Int) : SimOpCorrect (.knit d ⟨.f, j⟩)Proof
:= by
intro win s s' hs hw _
obtain ⟨hv, rfl⟩ := step_ok.mp hs
have hc := carrier_of_lin (Or.inr (Or.inr (Or.inl rfl))) hv
have hk := knit_pos_full hv
have hb := inside_bounds (show win.inside (slot s.rack ⟨.f, j⟩) = true by simpa [windowOk] using hw)
set z := slot s.rack ⟨.f, j⟩ with hz
have hzj : z = j := rfl
have hloc : ∀ m : Needle, slot s.rack m ≠ z → (apply s (.knit d ⟨.f, j⟩)).stk m = s.stk m :=
fun m hm => upd_ne (needle_ne_of_slot (a := ⟨.f, j⟩) rfl hm)
have ek : (s.stk ⟨.f, j⟩).length = nF s z := rfl
rw [ek] at hk
have eF0 : nF (apply s (.knit d ⟨.f, j⟩)) z = 1 := by simp [nF, apply, hzj]
have eB0 : nB (apply s (.knit d ⟨.f, j⟩)) z = nB s z := by simp [nB, apply]
rw [expand_box_knit]
cases d with
| R =>
obtain ⟨P, R, hP, ho, hst, h0, h1⟩ :=
setupR (win := win) (s := s) (s' := apply s (.knit .R ⟨.f, j⟩)) (z := z) hb rfl hloc hc rfl
rw [eF0, eB0, stkO_one] at h1
rw [stkO_pos hk] at h0
have d1 := D_single (rs_knitR P (stkO (nB s z) ++ R) hP (nF s z) hk .f)
have d2 := D_passR (P ++ [.stack 1]) R (nB s z) true
simp only [List.append_assoc, List.cons_append, List.nil_append, tuckRow] at d1 d2 h1 h0
have hsim : simOp win s (.knit .R ⟨.f, j⟩) = [.knit .R .f] ++ xs (P ++ [Obj.stack 1]).length true (ob (nB s z)) := by
simp only [simOp, arrivesFrom, ← hz, ho, List.length_append, List.length_singleton]
refine ⟨_, ?_, Iso.refl _ _⟩
rw [hsim, h0, D_app d1 d2, h1]
simp only [denoteOp1, arrivesFrom, ← hz, hst, ek, expand_append, expand_swapB, expand_cons, expand_nil,
inside_box1, PlainLabel.interior, List.append_nil, rowWidth_append, rowWidth_single, backAt, nB]
| L =>
obtain ⟨P, R, hP, ho, hst, h0, h1⟩ :=
setupL (win := win) (s := s) (s' := apply s (.knit .L ⟨.f, j⟩)) (z := z) hb rfl hloc hc rfl
rw [eF0, eB0, stkO_one] at h1
rw [stkO_pos hk] at h0
have d1 := D_passL (P ++ [.stack (nF s z)]) R (nB s z) false
have d2 := D_single (rs_knitL P (stkO (nB s z) ++ R) hP (nF s z) hk .f)
simp only [List.append_assoc, List.cons_append, List.nil_append, tuckRow] at d1 d2 h1 h0
have hsim : simOp win s (.knit .L ⟨.f, j⟩) =
xs ((P ++ [Obj.stack (nF s z)]).length + ob (nB s z) - 1) false (ob (nB s z)) ++ [.knit .L .f] := by
simp only [simOp, arrivesFrom, ← hz, ho, List.length_append, List.length_singleton, ob_pos hk]
refine ⟨_, ?_, Iso.refl _ _⟩
rw [hsim, h0, D_app d1 d2, h1]
simp only [denoteOp1, arrivesFrom, ← hz, hst, ek, expand_append, expand_swapB, expand_cons, expand_nil,
inside_box1, PlainLabel.interior, List.append_nil, rowWidth_append, rowWidth_single, backAt, frontAt, nB, nF,
Option.some.injEq, Prod.mk.injEq, true_and]
congr 2 <;> omega theorem knit_b (d : Dir) (j : Int) : SimOpCorrect (.knit d ⟨.b, j⟩)Proof
:= by
intro win s s' hs hw _
obtain ⟨hv, rfl⟩ := step_ok.mp hs
have hc := carrier_of_lin (Or.inr (Or.inr (Or.inl rfl))) hv
have hk := knit_pos_full hv
have hb := inside_bounds (show win.inside (slot s.rack ⟨.b, j⟩) = true by simpa [windowOk] using hw)
set z := slot s.rack ⟨.b, j⟩ with hz
have hzj : z - s.rack = j := by simp only [hz, slot]; omega
have hloc : ∀ m : Needle, slot s.rack m ≠ z → (apply s (.knit d ⟨.b, j⟩)).stk m = s.stk m :=
fun m hm => upd_ne (needle_ne_of_slot (a := ⟨.b, j⟩) rfl hm)
have ek : (s.stk ⟨.b, j⟩).length = nB s z := by simp only [nB, hzj]
rw [ek] at hk
have eB0 : nB (apply s (.knit d ⟨.b, j⟩)) z = 1 := by
simp only [nB, show (apply s (.knit d ⟨.b, j⟩)).rack = s.rack from rfl, hzj]
simp [apply]
have eF0 : nF (apply s (.knit d ⟨.b, j⟩)) z = nF s z := by simp [nF, apply]
rw [expand_box_knit]
cases d with
| R =>
obtain ⟨P, R, hP, ho, hst, h0, h1⟩ :=
setupR (win := win) (s := s) (s' := apply s (.knit .R ⟨.b, j⟩)) (z := z) hb rfl hloc hc rfl
rw [eF0, eB0, stkO_one] at h1
rw [stkO_pos hk] at h0
have d1 := D_passR P ([.stack (nB s z)] ++ R) (nF s z) false
have hP' : Obj.yarn ∉ P ++ stkO (nF s z) := by simp [hP, yarn_notin_stkO]
have d2 := D_single (rs_knitR (P ++ stkO (nF s z)) R hP' (nB s z) hk .b)
simp only [List.append_assoc, List.cons_append, List.nil_append, tuckRow] at d1 d2 h1 h0
have hsim : simOp win s (.knit .R ⟨.b, j⟩) = xs P.length false (ob (nF s z)) ++ [.knit .R .b] := by
simp only [simOp, arrivesFrom, ← hz, ho]
refine ⟨_, ?_, Iso.refl _ _⟩
rw [hsim, h0, D_app d1 d2, h1]
simp only [denoteOp1, arrivesFrom, ← hz, hst, ek, expand_append, expand_swapB, expand_cons, expand_nil,
inside_box1, PlainLabel.interior, List.append_nil, rowWidth_append, stkO_width, frontAt, nF]
| L =>
obtain ⟨P, R, hP, ho, hst, h0, h1⟩ :=
setupL (win := win) (s := s) (s' := apply s (.knit .L ⟨.b, j⟩)) (z := z) hb rfl hloc hc rfl
rw [eF0, eB0, stkO_one] at h1
rw [stkO_pos hk] at h0
have hP' : Obj.yarn ∉ P ++ stkO (nF s z) := by simp [hP, yarn_notin_stkO]
have d1 := D_single (rs_knitL (P ++ stkO (nF s z)) R hP' (nB s z) hk .b)
have d2 := D_passL P ([.stack 1] ++ R) (nF s z) true
simp only [List.append_assoc, List.cons_append, List.nil_append, tuckRow] at d1 d2 h1 h0
have hsim : simOp win s (.knit .L ⟨.b, j⟩) =
[.knit .L .b] ++ xs (P.length + ob (nF s z) - 1) true (ob (nF s z)) := by
simp only [simOp, arrivesFrom, ← hz, ho, ob_pos hk]
congr 1
refine ⟨_, ?_, Iso.refl _ _⟩
rw [hsim, h0, D_app d1 d2, h1]
simp only [denoteOp1, arrivesFrom, ← hz, hst, ek, expand_append, expand_swapB, expand_cons, expand_nil,
inside_box1, PlainLabel.interior, List.append_nil, rowWidth_append, stkO_width, backAt, frontAt, nB, nF,
Option.some.injEq, Prod.mk.injEq, true_and]
congr 2 <;> omega theorem knitCase : KnitCaseProof
:= by
intro d n
obtain ⟨bd, j⟩ := n
cases bd with
| f => exact knit_f d j
| b => exact knit_b d j Tucks
tuckCase proves TuckCase. The drawing of the shorthand word of a tuck is literally denoteOp's drawing.
- A tuck is on the front bed only.
steprefuses a tuck on the back bed (extraRefusal,tuck_front_full). - Both directions. The needle can hold
k ≥ 0loops. The back needle across can be empty or loaded. - The word is a stack
tuck, amergeof the front stack with the new loop (loaded needle), and the yarn passing each stack (simOp). A merge draws nothing. tuck_f: the proof for the front bed.
namespace FormalizeKnitting.Bridge.MR
open FormalizeKnitting.Knitout FormalizeKnitting.Knitout.MR FormalizeKnitting.Bridge.MR
theorem tuck_front_full {s : State} {d : Dir} {n : Needle} (h : refusal s (.tuck d n) = none) :
n.bed = .fProof
:= by
unfold refusal at h
split at h
· simp at h
· simp only [extraRefusal] at h
cases hb : n.bed with
| f => rfl
| b => simp [hb] at h theorem tuck_f (d : Dir) (j : Int) : SimOpCorrect (.tuck d ⟨.f, j⟩)Proof
:= by
intro win s s' hs hw _
obtain ⟨hv, rfl⟩ := step_ok.mp hs
have hc := carrier_of_lin (Or.inr (Or.inl rfl)) hv
have hb := inside_bounds (show win.inside (slot s.rack ⟨.f, j⟩) = true by simpa [windowOk] using hw)
set z := slot s.rack ⟨.f, j⟩ with hz
have hzj : z = j := rfl
have hloc : ∀ m : Needle, slot s.rack m ≠ z → (apply s (.tuck d ⟨.f, j⟩)).stk m = s.stk m :=
fun m hm => upd_ne (needle_ne_of_slot (a := ⟨.f, j⟩) rfl hm)
have eF0 : nF (apply s (.tuck d ⟨.f, j⟩)) z = nF s z + 1 := by simp [nF, apply, hzj]
have eB0 : nB (apply s (.tuck d ⟨.f, j⟩)) z = nB s z := by simp [nB, apply]
have hden : expand sig (denoteOp win s (.tuck d ⟨.f, j⟩)) =
expand plainSig (denoteOp1 win s (.tuck d ⟨.f, j⟩)) := expand_liftB _
have eFa : frontAt s z = 2 * nF s z := rfl
have eBa : backAt s z = 2 * nB s z := rfl
have hP' : ∀ P : Row, Obj.yarn ∉ P → Obj.yarn ∉ P ++ stkO (nF s z) := fun P hP => by
simp [hP, yarn_notin_stkO]
rw [hden]
cases d with
| R =>
obtain ⟨P, R, hP, ho, hst, h0, h1⟩ :=
setupR (win := win) (s := s) (s' := apply s (.tuck .R ⟨.f, j⟩)) (z := z) hb rfl hloc hc rfl
rw [eF0, eB0, stkO_pos (Nat.succ_pos _)] at h1
have d1 := D_passR P (stkO (nB s z) ++ R) (nF s z) false
have d2 := D_single (rs_tuck (P ++ stkO (nF s z)) (stkO (nB s z) ++ R) (hP' P hP) .R)
have d3 := D_mergeO' P (Obj.yarn :: (stkO (nB s z) ++ R)) 1 (nF s z) Nat.one_pos (by simp)
have d4 := D_passR (P ++ [Obj.stack (nF s z + 1)]) R (nB s z) true
simp only [List.append_assoc, List.cons_append, List.nil_append, tuckRow] at d1 d2 d3 d4 h1
have hsim : simOp win s (.tuck .R ⟨.f, j⟩) = xs P.length false (ob (nF s z)) ++ [.tuck .R] ++
ms P.length (ob (nF s z)) ++ xs (P ++ [Obj.stack (nF s z + 1)]).length true (ob (nB s z)) := by
simp only [simOp, arrivesFrom, ← hz, ho, List.length_append, List.length_singleton]
refine ⟨_, ?_, Iso.refl _ _⟩
rw [hsim, h0, D_app (D_app (D_app d1 d2) d3) d4, h1]
simp only [denoteOp1, arrivesFrom, ← hz, hst, eFa, eBa, expand_append, expand_swapB, expand_cons, expand_nil,
inside_box1, PlainLabel.interior, List.append_nil, rowWidth_append, rowWidth_single, stkO_width,
List.append_assoc, Nat.mul_add, Nat.mul_one, Nat.add_assoc]
| L =>
obtain ⟨P, R, hP, ho, hst, h0, h1⟩ :=
setupL (win := win) (s := s) (s' := apply s (.tuck .L ⟨.f, j⟩)) (z := z) hb rfl hloc hc rfl
rw [eF0, eB0, stkO_pos (Nat.succ_pos _)] at h1
have d1 := D_passL (P ++ stkO (nF s z)) R (nB s z) false
have d2 := D_single (rs_tuck (P ++ stkO (nF s z)) (stkO (nB s z) ++ R) (hP' P hP) .L)
have d3 := D_passL P (Obj.stack 1 :: (stkO (nB s z) ++ R)) (nF s z) true
have d4 := D_mergeO' (P ++ [Obj.yarn]) (stkO (nB s z) ++ R) 1 (nF s z) Nat.one_pos (by simp)
simp only [List.append_assoc, List.cons_append, List.nil_append, tuckRow] at d1 d2 d3 d4 h1 h0
have hsim : simOp win s (.tuck .L ⟨.f, j⟩) =
xs ((P ++ stkO (nF s z)).length + ob (nB s z) - 1) false (ob (nB s z)) ++ [.tuck .L] ++
xs (P.length + ob (nF s z) - 1) true (ob (nF s z)) ++ ms (P ++ [Obj.yarn]).length (ob (nF s z)) := by
simp only [simOp, arrivesFrom, ← hz, ho, List.length_append, List.length_singleton, stkO_length]
have := ob_cases (nF s z)
have := ob_cases (nB s z)
congr 1
· congr 1
apply xs_congr; omega
· apply ms_congr; omega
refine ⟨_, ?_, Iso.refl _ _⟩
rw [hsim, h0, D_app (D_app (D_app d1 d2) d3) d4, h1]
simp only [denoteOp1, arrivesFrom, ← hz, hst, eFa, eBa, expand_append, expand_swapB, expand_cons, expand_nil,
inside_box1, PlainLabel.interior, List.append_nil, rowWidth_append, stkO_width,
List.append_assoc, Nat.add_sub_cancel] theorem tuckCase : TuckCaseProof
:= by
intro d n win s s' hs
have hf : n.bed = .f := tuck_front_full (step_ok.mp hs).1
obtain ⟨bd, j⟩ := n
simp only at hf
subst hf
exact tuck_f d j win s s' hs Splits
splitCase proves SplitCase. The drawing of the shorthand word of a split is literally denoteOp's drawing: the
box Label.splitK d bd k and the exchange of the yarn with the stack across.
- The needle split holds
k ≥ 1loops.steprefuses a split of an empty needle (extraRefusal,split_pos_full). - Both beds, both directions. The needle across can be empty or loaded.
- The word is one stack
split, the yarn passing the stack across, and amergeof the old stack with the stack across (loaded target) (simOp). split_f: the front bed.split_b: the back bed.
namespace FormalizeKnitting.Bridge.MR
open FormalizeKnitting.Knitout FormalizeKnitting.Knitout.MR FormalizeKnitting.Bridge.MR
theorem split_pos_full {s : State} {d : Dir} {n m : Needle} (h : refusal s (.split d n m) = none) :
0 < (s.stk n).lengthProof
:= by
unfold refusal at h
split at h
· simp at h
· simp only [extraRefusal] at h
split_ifs at h with h1
omega theorem split_aligned_full {s : State} {d : Dir} {n m : Needle} (h : refusal s (.split d n m) = none) :
n.bed ≠ m.bed ∧ slot s.rack n = slot s.rack mProof
:= by
have := validLin_split (validLin_of_full h)
simpa [aligned] using this theorem split_hloc {s : State} {d : Dir} {n m : Needle} {z : Int} (hn : slot s.rack n = z)
(hm : slot s.rack m = z) : ∀ q : Needle, slot s.rack q ≠ z → (apply s (.split d n m)).stk q = s.stk qProof
:= by
intro q hq
simp only [apply]
rw [upd_ne (needle_ne_of_slot hm hq), upd_ne (needle_ne_of_slot hn hq)] theorem inside_box (l : Label) (i : Nat) : (BEv.box l i : BEv Label).inside sig = shiftW i (Label.interior l)Proof
:=
rfl theorem rowWidth_nil : rowWidth [] = 0Proof
:= rfl theorem split_f (d : Dir) (j : Int) (m : Needle) : SimOpCorrect (.split d ⟨.f, j⟩ m)Proof
:= by
intro win s s' hs hw _
obtain ⟨hv, rfl⟩ := step_ok.mp hs
have hc := carrier_of_lin (Or.inr (Or.inr (Or.inr ⟨m, rfl⟩))) hv
have hk := split_pos_full hv
obtain ⟨hbd, hsl⟩ := split_aligned_full hv
have hb := inside_bounds (show win.inside (slot s.rack ⟨.f, j⟩) = true by
simp only [windowOk, Bool.and_eq_true] at hw; exact hw.1)
obtain ⟨bd', j'⟩ := m
cases bd' with
| f => exact absurd rfl hbd
| b =>
set z := slot s.rack ⟨.f, j⟩ with hz
have hzj : z = j := rfl
have hzj' : z - s.rack = j' := by simp only [slot] at hsl hz; omega
have hm : slot s.rack ⟨.b, j'⟩ = z := hsl.symm
have hloc := split_hloc (d := d) (n := ⟨.f, j⟩) rfl hm
have ek : (s.stk ⟨.f, j⟩).length = nF s z := rfl
rw [ek] at hk
have eF0 : nF (apply s (.split d ⟨.f, j⟩ ⟨.b, j'⟩)) z = 1 := by simp [nF, apply, hzj]
have eB0 : nB (apply s (.split d ⟨.f, j⟩ ⟨.b, j'⟩)) z = nF s z + nB s z := by
simp only [nB, nF, apply, upd_apply, hzj']
simp [xferStack, hzj]
have hpos : 0 < nF s z + nB s z := by omega
have eFa : frontAt s z = 2 * nF s z := rfl
have eBa : backAt s z = 2 * nB s z := rfl
cases d with
| R =>
obtain ⟨P, R, hP, ho, hst, h0, h1⟩ := setupR (win := win) (s := s)
(s' := apply s (.split .R ⟨.f, j⟩ ⟨.b, j'⟩)) (z := z) hb rfl hloc hc rfl
rw [eF0, eB0, stkO_one, stkO_pos hpos] at h1
rw [stkO_pos hk] at h0
have d1 := D_single (rs_splitR P (stkO (nB s z) ++ R) hP (nF s z) hk .f)
have d2 := D_passR (P ++ [Obj.stack 1, Obj.stack (nF s z)]) R (nB s z) true
have d3 := D_mergeO (P ++ [Obj.stack 1]) (Obj.yarn :: R) (nF s z) (nB s z) hk (by simp)
simp only [List.append_assoc, List.cons_append, List.nil_append, splitRow] at d1 d2 d3 h1 h0
have hsim : simOp win s (.split .R ⟨.f, j⟩ ⟨.b, j'⟩) = [.split .R .f] ++
xs (P ++ [Obj.stack 1, Obj.stack (nF s z)]).length true (ob (nB s z)) ++
ms (P ++ [Obj.stack 1]).length (ob (nB s z)) := by
simp only [simOp, arrivesFrom, ← hz, ho, List.length_append, List.length_cons, List.length_nil]
refine ⟨_, ?_, Iso.refl _ _⟩
rw [hsim, h0, D_app (D_app d1 d2) d3, h1]
simp only [denoteOp, arrivesFrom, ← hz, hst, ek, eFa, eBa, expand_append, expand_plainB', expand_cons,
expand_nil, inside_box, Label.interior, List.append_nil, rowWidth_append, rowWidth_cons, rowWidth_nil,
Obj.width, Nat.mul_one, Nat.add_zero, Nat.add_assoc]
| L =>
obtain ⟨P, R, hP, ho, hst, h0, h1⟩ := setupL (win := win) (s := s)
(s' := apply s (.split .L ⟨.f, j⟩ ⟨.b, j'⟩)) (z := z) hb rfl hloc hc rfl
rw [eF0, eB0, stkO_one, stkO_pos hpos] at h1
rw [stkO_pos hk] at h0
have d1 := D_passL (P ++ [Obj.stack (nF s z)]) R (nB s z) false
have d2 := D_single (rs_splitL P (stkO (nB s z) ++ R) hP (nF s z) hk .f)
have d3 := D_mergeO (P ++ [Obj.yarn, Obj.stack 1]) R (nF s z) (nB s z) hk (by simp)
simp only [List.append_assoc, List.cons_append, List.nil_append, splitRow] at d1 d2 d3 h1 h0
have hsim : simOp win s (.split .L ⟨.f, j⟩ ⟨.b, j'⟩) =
xs ((P ++ [Obj.stack (nF s z)]).length + ob (nB s z) - 1) false (ob (nB s z)) ++ [.split .L .f] ++
ms (P ++ [Obj.yarn, Obj.stack 1]).length (ob (nB s z)) := by
simp only [simOp, arrivesFrom, ← hz, ho, List.length_append, List.length_cons, List.length_nil, ob_pos hk]
have := ob_cases (nB s z)
congr 1
apply ms_congr; omega
refine ⟨_, ?_, Iso.refl _ _⟩
rw [hsim, h0, D_app (D_app d1 d2) d3, h1]
simp only [denoteOp, arrivesFrom, ← hz, hst, ek, eFa, eBa, expand_append, expand_plainB', expand_cons,
expand_nil, inside_box, Label.interior, List.append_nil, rowWidth_append, rowWidth_single,
Nat.add_sub_cancel] theorem split_b (d : Dir) (j : Int) (m : Needle) : SimOpCorrect (.split d ⟨.b, j⟩ m)Proof
:= by
intro win s s' hs hw _
obtain ⟨hv, rfl⟩ := step_ok.mp hs
have hc := carrier_of_lin (Or.inr (Or.inr (Or.inr ⟨m, rfl⟩))) hv
have hk := split_pos_full hv
obtain ⟨hbd, hsl⟩ := split_aligned_full hv
have hb := inside_bounds (show win.inside (slot s.rack ⟨.b, j⟩) = true by
simp only [windowOk, Bool.and_eq_true] at hw; exact hw.1)
obtain ⟨bd', j'⟩ := m
cases bd' with
| b => exact absurd rfl hbd
| f =>
set z := slot s.rack ⟨.b, j⟩ with hz
have hzj : z - s.rack = j := by simp only [hz, slot]; omega
have hzj' : z = j' := hsl
have hm : slot s.rack ⟨.f, j'⟩ = z := hsl.symm
have hloc := split_hloc (d := d) (n := ⟨.b, j⟩) rfl hm
have ek : (s.stk ⟨.b, j⟩).length = nB s z := by simp only [nB, hzj]
rw [ek] at hk
have eB0 : nB (apply s (.split d ⟨.b, j⟩ ⟨.f, j'⟩)) z = 1 := by
simp only [nB, show (apply s (.split d ⟨.b, j⟩ ⟨.f, j'⟩)).rack = s.rack from rfl, hzj]
simp [apply]
have eF0 : nF (apply s (.split d ⟨.b, j⟩ ⟨.f, j'⟩)) z = nF s z + nB s z := by
simp only [nF, nB, apply, upd_apply, ← hzj', hzj]
simp [xferStack]; omega
have hpos : 0 < nF s z + nB s z := by omega
have eFa : frontAt s z = 2 * nF s z := rfl
have eBa : backAt s z = 2 * nB s z := rfl
have hP' : ∀ P : Row, Obj.yarn ∉ P → Obj.yarn ∉ P ++ stkO (nF s z) := fun P hP => by
simp [hP, yarn_notin_stkO]
cases d with
| R =>
obtain ⟨P, R, hP, ho, hst, h0, h1⟩ := setupR (win := win) (s := s)
(s' := apply s (.split .R ⟨.b, j⟩ ⟨.f, j'⟩)) (z := z) hb rfl hloc hc rfl
rw [eF0, eB0, stkO_one, stkO_pos hpos] at h1
rw [stkO_pos hk] at h0
have d1 := D_passR P (Obj.stack (nB s z) :: R) (nF s z) false
have d2 := D_single (rs_splitR (P ++ stkO (nF s z)) R (hP' P hP) (nB s z) hk .b)
have d3 := D_mergeO' P (Obj.stack 1 :: Obj.yarn :: R) (nB s z) (nF s z) hk (by simp)
simp only [List.append_assoc, List.cons_append, List.nil_append, splitRow] at d1 d2 d3 h1 h0
have hsim : simOp win s (.split .R ⟨.b, j⟩ ⟨.f, j'⟩) = xs P.length false (ob (nF s z)) ++ [.split .R .b] ++
ms P.length (ob (nF s z)) := by
simp only [simOp, arrivesFrom, ← hz, ho]
refine ⟨_, ?_, Iso.refl _ _⟩
rw [hsim, h0, D_app (D_app d1 d2) d3, h1]
simp only [denoteOp, arrivesFrom, ← hz, hst, ek, eFa, expand_append, expand_plainB', expand_cons,
expand_nil, inside_box, Label.interior, List.append_nil, rowWidth_append, stkO_width]
| L =>
obtain ⟨P, R, hP, ho, hst, h0, h1⟩ := setupL (win := win) (s := s)
(s' := apply s (.split .L ⟨.b, j⟩ ⟨.f, j'⟩)) (z := z) hb rfl hloc hc rfl
rw [eF0, eB0, stkO_one, stkO_pos hpos] at h1
rw [stkO_pos hk] at h0
have d1 := D_single (rs_splitL (P ++ stkO (nF s z)) R (hP' P hP) (nB s z) hk .b)
have d2 := D_passL P (Obj.stack (nB s z) :: Obj.stack 1 :: R) (nF s z) true
have d3 := D_mergeO' (P ++ [Obj.yarn]) (Obj.stack 1 :: R) (nB s z) (nF s z) hk (by simp)
simp only [List.append_assoc, List.cons_append, List.nil_append, splitRow] at d1 d2 d3 h1 h0
have hsim : simOp win s (.split .L ⟨.b, j⟩ ⟨.f, j'⟩) = [.split .L .b] ++
xs (P.length + ob (nF s z) - 1) true (ob (nF s z)) ++ ms (P ++ [Obj.yarn]).length (ob (nF s z)) := by
simp only [simOp, arrivesFrom, ← hz, ho, List.length_append, List.length_singleton, ob_pos hk]
have := ob_cases (nF s z)
congr 1
apply ms_congr; omega
refine ⟨_, ?_, Iso.refl _ _⟩
rw [hsim, h0, D_app (D_app d1 d2) d3, h1]
simp only [denoteOp, arrivesFrom, ← hz, hst, ek, eFa, eBa, expand_append, expand_plainB', expand_cons,
expand_nil, inside_box, Label.interior, List.append_nil, rowWidth_append, stkO_width,
Nat.add_sub_cancel] theorem splitCase : SplitCaseProof
:= by
intro d n m
obtain ⟨bd, j⟩ := n
cases bd with
| f => exact split_f d j m
| b => exact split_b d j m Racking: one object past a block of objects
One object moves past a block of objects, one exchange at a time.
| Theorem | Move | Drawing |
|---|---|---|
D_passBlockL |
The object o moves left past the objects X (xLeft, the rightmost first). |
Iso to swapBlocks of X with o, by far commutation (CF) |
D_passBlockR |
The object o moves right past the objects X (xRight, the leftmost first). |
Literally swapBlocks of o with X (MR.Rack.swapBlocks_right_split) |
D_passO |
As D_passBlockL, for a stack stkO n that can be empty |
Empty when n = 0 |
D_passRO |
As D_passBlockR, for a stack stkO n that can be empty |
Empty when n = 0 |
namespace FormalizeKnitting.Bridge.MR.Rack
open FormalizeKnitting.Knitout FormalizeKnitting.Knitout.MR FormalizeKnitting.Knitout.MR.Rack FormalizeKnitting.Bridge.MR
def xLeft (a n : Nat) (f : Bool) : List Step := (List.range n).map fun j => .x (a - 1 - j) f
def xRight (a n : Nat) (f : Bool) : List Step := (List.range n).map fun j => .x (a + j) ftheorem xLeft_succ (a n : Nat) (f : Bool) : xLeft (a + 1) (n + 1) f = .x a f :: xLeft a n fProof
:= by
simp only [xLeft, List.range_succ_eq_map, List.map_cons, List.map_map]
congr 1
apply List.map_congr_left; intro j _
simp only [Function.comp]; congr 1; omega theorem xRight_succ (a n : Nat) (f : Bool) : xRight a (n + 1) f = .x a f :: xRight (a + 1) n fProof
:= by
simp only [xRight, List.range_succ_eq_map, List.map_cons, List.map_map]
congr 1
apply List.map_congr_left; intro j _
simp only [Function.comp]; congr 1; omega theorem rowWidth_single (o : Obj) : rowWidth [o] = o.widthProof
:= by simp [rowWidth] theorem D_passBlockL (f : Bool) (o : Obj) {b : Nat} (X : Row) : ∀ (A Z : Row), Obj.yarn ∈ A ++ X ++ [o] ++ Z →
rowWidth A + rowWidth X + o.width ≤ b →
∃ M, D (A ++ X ++ [o] ++ Z) (xLeft (A.length + X.length) X.length f) = some (A ++ [o] ++ X ++ Z, M) ∧
CF b M ∧ Iso b M (swapBlocks (rowWidth A) (rowWidth X) o.width f)Proof
:= by
induction X using List.reverseRecOn with
| nil =>
intro A Z _ _
refine ⟨[], by simp [xLeft, D], CF.nil b, ?_⟩
rw [show rowWidth [] = 0 from rfl, swapBlocks_zero_left]; exact Iso.refl _ _
| append_singleton X x ih =>
intro A Z hy hb
have hwX : rowWidth (X ++ [x]) = rowWidth X + x.width := by rw [rowWidth_append, rowWidth_single]
rw [hwX] at hb ⊢
have e0 : A ++ (X ++ [x]) ++ [o] ++ Z = (A ++ X) ++ x :: o :: Z := by simp
rw [e0] at hy ⊢
have hs := rs_x (A ++ X) Z x o f hy
have e1 : (A ++ X) ++ o :: x :: Z = A ++ X ++ [o] ++ ([x] ++ Z) := by simp
rw [e1] at hs
have hy' : Obj.yarn ∈ A ++ X ++ [o] ++ ([x] ++ Z) := by
simp only [List.mem_append, List.mem_cons] at hy ⊢; tauto
obtain ⟨M, hD, hCF, hI⟩ := ih A ([x] ++ Z) hy' (by omega)
have hlen : (A ++ X).length = A.length + X.length := by simp
have hx : xLeft (A.length + (X ++ [x]).length) (X ++ [x]).length f =
.x (A.length + X.length) f :: xLeft (A.length + X.length) X.length f := by
rw [List.length_append, List.length_singleton, ← Nat.add_assoc, xLeft_succ]
rw [hx]
rw [hlen] at hs
rw [D_cons_some hs, hD, rowWidth_append]
refine ⟨swapBlocks (rowWidth A + rowWidth X) x.width o.width f ++ M, by simp, ?_, ?_⟩
· exact (CF_swapBlocks (by omega)).append hCF
· have cS : CF b (swapBlocks (rowWidth A + rowWidth X) x.width o.width f) := CF_swapBlocks (by omega)
have i1 := Iso.embed (p := swapBlocks (rowWidth A + rowWidth X) x.width o.width f) (q := []) hI cS
(CF.nil b) hCF
have i2 := swapBlocks_left_split (b := b) (rowWidth A) (rowWidth X) x.width f o.width (by omega)
simp only [List.append_nil] at i1
exact i1.trans i2.symm theorem D_passBlockR (f : Bool) (o : Obj) (X : Row) : ∀ (A Z : Row), Obj.yarn ∈ A ++ o :: (X ++ Z) →
D (A ++ o :: (X ++ Z)) (xRight A.length X.length f) =
some (A ++ X ++ o :: Z, swapBlocks (rowWidth A) o.width (rowWidth X) f)Proof
:= by
induction X with
| nil => intro A Z _; simp [xRight, D, show rowWidth ([] : Row) = 0 from rfl, swapBlocks]
| cons x X ih =>
intro A Z hy
have hs := rs_x A (X ++ Z) o x f (by simpa using hy)
have e1 : A ++ x :: o :: (X ++ Z) = (A ++ [x]) ++ o :: (X ++ Z) := by simp
rw [e1] at hs
have hy' : Obj.yarn ∈ (A ++ [x]) ++ o :: (X ++ Z) := by
simp only [List.mem_append, List.mem_cons] at hy ⊢; tauto
have h2 := ih (A ++ [x]) Z hy'
rw [List.length_append, List.length_singleton] at h2
rw [List.length_cons, xRight_succ, show A ++ o :: (x :: X ++ Z) = A ++ o :: x :: (X ++ Z) from rfl,
D_cons_some hs, h2]
simp only [Option.map_some, rowWidth_append, rowWidth_cons]
rw [swapBlocks_right_split]
simp [show rowWidth ([] : Row) = 0 from rfl] A possibly empty stack moved past a block
theorem swapBlocks_zero_right (at_ L : Nat) (f : Bool) : swapBlocks at_ L 0 f = []Proof
:= by simp [swapBlocks] theorem stkO_pos {n : Nat} (hn : n ≠ 0) : stkO n = [.stack n]Proof
:= by simp [stkO, hn] theorem D_passO (f : Bool) (n : Nat) {b : Nat} (X A Z : Row) (hy : Obj.yarn ∈ A ++ X ++ stkO n ++ Z)
(hb : rowWidth A + rowWidth X + 2 * n ≤ b) :
∃ M, D (A ++ X ++ stkO n ++ Z) (if n = 0 then [] else xLeft (A.length + X.length) X.length f) =
some (A ++ stkO n ++ X ++ Z, M) ∧ CF b M ∧ Iso b M (swapBlocks (rowWidth A) (rowWidth X) (2 * n) f)Proof
:= by
by_cases hn : n = 0
· subst hn
refine ⟨[], by simp [stkO, D], CF.nil b, ?_⟩
rw [Nat.mul_zero, swapBlocks_zero_right]; exact Iso.refl _ _
· rw [stkO_pos hn] at hy ⊢
simp only [hn, ↓reduceIte]
exact D_passBlockL f (.stack n) X A Z hy hb theorem D_passRO (f : Bool) (n : Nat) (X A Z : Row) (hy : Obj.yarn ∈ A ++ stkO n ++ X ++ Z) :
D (A ++ stkO n ++ X ++ Z) (if n = 0 then [] else xRight (A.length + ob n - 1) X.length f) =
some (A ++ X ++ stkO n ++ Z, swapBlocks (rowWidth A) (2 * n) (rowWidth X) f)Proof
:= by
by_cases hn : n = 0
· subst hn
simp [stkO, D, swapBlocks_zero_left]
· rw [stkO_pos hn] at hy ⊢
simp only [hn, ↓reduceIte]
rw [show ob n = 1 by simp [ob, hn], Nat.add_sub_cancel]
have h := D_passBlockR f (.stack n) X A Z (by simpa using hy)
have e1 : A ++ [Obj.stack n] ++ X ++ Z = A ++ Obj.stack n :: (X ++ Z) := by simp
have e2 : A ++ X ++ [Obj.stack n] ++ Z = A ++ X ++ Obj.stack n :: Z := by simp
rw [e1, e2, h]; rfl Racking: the whole window
rackCase proves RackCase. The proof goes position by position across the window (down_run, up_run).
| Rack | At position i |
Drawing |
|---|---|---|
Down (down_run) |
The back stack at i moves left past the carrier and the front stack at i (D_passO). Then it is the last object of position i - 1. |
Iso to the block swap of drawAt, by far commutation |
Up (up_run) |
The back stack of position i - 1 moves right past the carrier and the front stack at i (D_passRO). Then it is the last object of position i. |
Literally the block swap of drawAt |
- An empty stack is no object (
stkO 0 = []). Then the word at that position is empty, and so is its drawing. rack_step_lin: a rack thatvalidLinaccepts changes the rack by 1.
namespace FormalizeKnitting.Bridge.MR.Rack
open FormalizeKnitting.Knitout FormalizeKnitting.Knitout.MR FormalizeKnitting.Knitout.MR.Rack FormalizeKnitting.Bridge.MR
def Fx (s : State) (i : Int) : Row := (if s.carrier = i then [Obj.yarn] else []) ++ stkO (nF s i)theorem seg_Fx (s : State) (i : Int) : seg s i = Fx s i ++ stkO (nB s i)Proof
:= rfl theorem Fx_length (s : State) (i : Int) : (Fx s i).length = nY s i + ob (nF s i)Proof
:= by
simp only [Fx, nY, List.length_append, stkO_length]; split <;> rfl theorem Fx_width (s : State) (i : Int) : rowWidth (Fx s i) = yarnAt s i + frontAt s iProof
:= by
simp only [Fx, rowWidth_append, stkO_width, yarnAt, frontAt, nF]; split <;> simp [rowWidth, Obj.width] theorem seg_length (s : State) (i : Int) : (seg s i).length = (Fx s i).length + ob (nB s i)Proof
:= by
rw [seg_Fx, List.length_append, stkO_length] theorem seg_width' (s : State) (i : Int) : rowWidth (seg s i) = rowWidth (Fx s i) + 2 * nB s iProof
:= by
rw [seg_Fx, rowWidth_append, stkO_width] theorem rowOf_rest (s : State) (lo : Int) {w W : Nat} (h : w + 1 ≤ W) :
rowOf s (lo + w) (W - w) = seg s (lo + w) ++ rowOf s (lo + ((w + 1 : Nat) : Int)) (W - (w + 1))Proof
:= by
rw [show W - w = 1 + (W - (w + 1)) by omega, rowOf_add, rowOf_one]
congr 2; push_cast; omega theorem rowOf_split (s : State) (lo : Int) {w W : Nat} (h : w ≤ W) :
rowOf s lo W = rowOf s lo w ++ rowOf s (lo + w) (W - w)Proof
:= by
rw [← rowOf_add]; congr 1; omega The word, position by position
def stepAt (lo : Int) (s : State) (r : Int) (k : Nat) : List Step :=
if r < s.rack then
if nB s (lo + k) = 0 then [] else
xLeft ((rowOf s lo k).length + (nY s (lo + k) + ob (nF s (lo + k)))) (nY s (lo + k) + ob (nF s (lo + k))) true
else
if nB s (lo + k - 1) = 0 then [] else
xRight ((rowOf s lo k).length - 1) (nY s (lo + k) + ob (nF s (lo + k))) falsetheorem rackSteps_eq (win : Window) (s : State) (r : Int) :
rackSteps win s r = (List.range win.w).flatMap (stepAt win.lo s r)Proof
:= by
unfold rackSteps
rw [flatMap_coe]
refine congrArg (fun g => List.flatMap g (List.range win.w)) (funext fun k => ?_)
simp only [objsBefore, stepAt, xLeft, xRight]
rw [show (win.lo + (k : Int) - win.lo).toNat = k by omega]
rfl theorem expand_rack (win : Window) (s : State) (r : Int) :
expand sig (denoteOp win s (.rack r)) = (List.range win.w).flatMap (drawAt win.lo s r)Proof
:= by
show expand sig (liftB (denoteOp1 win s (.rack r))) = _
rw [expand_liftB, expand_rackBraid] Rack down
theorem down_run (s : State) (r : Int) (hr : r = s.rack - 1) (lo : Int) (W : Nat)
(hy : Obj.yarn ∈ rowOf s lo W) (h0 : nB s lo = 0) :
∀ w, w ≤ W → ∃ P M, rowOf { s with rackProof
:= r } lo w = P ++ stkO (nB s (lo + w)) ∧
P.length = (rowOf s lo w).length ∧ rowWidth P = rowWidth (rowOf s lo w) ∧
D (rowOf s lo W) ((List.range w).flatMap (stepAt lo s r)) = some (P ++ rowOf s (lo + w) (W - w), M) ∧
Obj.yarn ∈ P ++ rowOf s (lo + w) (W - w) ∧
CF (rowWidth (rowOf s lo W)) M ∧
Iso (rowWidth (rowOf s lo W)) M ((List.range w).flatMap (drawAt lo s r))
| 0, _ => by
refine ⟨[], [], ?_, rfl, rfl, ?_, ?_, CF.nil _, Iso.refl _ _⟩
· show [] = [] ++ stkO (nB s (lo + ((0 : Nat) : Int)))
rw [show lo + ((0 : Nat) : Int) = lo by omega, h0]; rfl
· show D (rowOf s lo W) [] = some ([] ++ rowOf s (lo + ((0 : Nat) : Int)) (W - 0), [])
rw [show lo + ((0 : Nat) : Int) = lo by omega, Nat.sub_zero]; rfl
· rw [show lo + ((0 : Nat) : Int) = lo by omega, Nat.sub_zero]; simpa using hy
| w + 1, hw => by
obtain ⟨P, M, hP, hPl, hPw, hD, hY, hC, hI⟩ := down_run s r hr lo W hy h0 w (by omega)
have hlt : r < s.rack := by omega
set i : Int := lo + w with hi
set R := rowOf s (lo + ((w + 1 : Nat) : Int)) (W - (w + 1))
have erest := rowOf_rest s lo hw
rw [← hi, seg_Fx] at erest
rw [erest] at hD hY
have e0 : P ++ (Fx s i ++ stkO (nB s i) ++ R) = P ++ Fx s i ++ stkO (nB s i) ++ R := by simp
rw [e0] at hY hD
have hb : rowWidth P + rowWidth (Fx s i) + 2 * nB s i ≤ rowWidth (rowOf s lo W) := by
rw [rowOf_split s lo (show w ≤ W by omega), rowWidth_append, erest, ← hPw]
simp only [rowWidth_append, stkO_width]; omega
obtain ⟨M₂, hD₂, hC₂, hI₂⟩ := D_passO true (nB s i) (Fx s i) P R hY hb
have hstep : stepAt lo s r w =
if nB s i = 0 then [] else xLeft (P.length + (Fx s i).length) (Fx s i).length true := by
simp only [stepAt, hlt, ↓reduceIte, ← hi, hPl, Fx_length]
have hdraw : drawAt lo s r w = swapBlocks (rowWidth P) (rowWidth (Fx s i)) (2 * nB s i) true := by
simp only [drawAt, hlt, ↓reduceIte, ← hi, Fx_width, ← rowWidth_rowOf, ← hPw]; rfl
have hnext : nB { s with rack := r } i = nB s (lo + ((w + 1 : Nat) : Int)) := by
simp only [nB]; congr 3; push_cast; omega
refine ⟨P ++ stkO (nB s i) ++ Fx s i, M ++ M₂, ?_, ?_, ?_, ?_, ?_, hC.append hC₂, ?_⟩
· rw [rowOf_succ, hP, ← hi, seg_Fx, hnext]; simp only [List.append_assoc]; rfl
· simp only [rowOf_succ, List.length_append, ← hi, seg_length, hPl, stkO_length]; omega
· simp only [rowOf_succ, rowWidth_append, ← hi, seg_width', hPw, stkO_width]; omega
· rw [flatMap_range_succ, hstep, D_app hD hD₂]
· simp only [List.mem_append] at hY ⊢; tauto
· rw [flatMap_range_succ, hdraw]; exact iso_app hI hI₂ hC hC₂ Rack up
theorem up_run (s : State) (r : Int) (hr : r = s.rack + 1) (lo : Int) (W : Nat)
(hy : Obj.yarn ∈ rowOf s lo W) (h0 : nB s (lo - 1) = 0) :
∀ w, w ≤ W → ∃ P M, P = rowOf { s with rackProof
:= r } lo w ++ stkO (nB s (lo + w - 1)) ∧
P.length = (rowOf s lo w).length ∧ rowWidth P = rowWidth (rowOf s lo w) ∧
D (rowOf s lo W) ((List.range w).flatMap (stepAt lo s r)) = some (P ++ rowOf s (lo + w) (W - w), M) ∧
Obj.yarn ∈ P ++ rowOf s (lo + w) (W - w) ∧
CF (rowWidth (rowOf s lo W)) M ∧
Iso (rowWidth (rowOf s lo W)) M ((List.range w).flatMap (drawAt lo s r))
| 0, _ => by
refine ⟨[], [], ?_, rfl, rfl, ?_, ?_, CF.nil _, Iso.refl _ _⟩
· show [] = [] ++ stkO (nB s (lo + ((0 : Nat) : Int) - 1))
rw [show lo + ((0 : Nat) : Int) - 1 = lo - 1 by omega, h0]; rfl
· show D (rowOf s lo W) [] = some ([] ++ rowOf s (lo + ((0 : Nat) : Int)) (W - 0), [])
rw [show lo + ((0 : Nat) : Int) = lo by omega, Nat.sub_zero]; rfl
· rw [show lo + ((0 : Nat) : Int) = lo by omega, Nat.sub_zero]; simpa using hy
| w + 1, hw => by
obtain ⟨P, M, hP, hPl, hPw, hD, hY, hC, hI⟩ := up_run s r hr lo W hy h0 w (by omega)
have hlt : ¬ r < s.rack := by omega
set i : Int := lo + w with hi
set A := rowOf { s with rack := r } lo w
set R := rowOf s (lo + ((w + 1 : Nat) : Int)) (W - (w + 1))
have erest := rowOf_rest s lo hw
rw [← hi, seg_Fx] at erest
rw [erest, hP] at hD hY
have e0 : A ++ stkO (nB s (i - 1)) ++ (Fx s i ++ stkO (nB s i) ++ R) =
A ++ stkO (nB s (i - 1)) ++ Fx s i ++ (stkO (nB s i) ++ R) := by simp
rw [e0] at hY hD
have hPl' : A.length + ob (nB s (i - 1)) = (rowOf s lo w).length := by
rw [← hPl, hP, List.length_append, stkO_length]
have hPw' : rowWidth A + 2 * nB s (i - 1) = rowWidth (rowOf s lo w) := by
rw [← hPw, hP, rowWidth_append, stkO_width]
have hD₂ := D_passRO false (nB s (i - 1)) (Fx s i) A (stkO (nB s i) ++ R) hY
have hstep : stepAt lo s r w =
if nB s (i - 1) = 0 then [] else xRight (A.length + ob (nB s (i - 1)) - 1) (Fx s i).length false := by
simp only [stepAt, hlt, ↓reduceIte, ← hi, Fx_length, hPl']
have hdraw : drawAt lo s r w = swapBlocks (rowWidth A) (2 * nB s (i - 1)) (rowWidth (Fx s i)) false := by
simp only [drawAt, hlt, ↓reduceIte, ← hi, Fx_width, ← rowWidth_rowOf]
rw [← hPw']; congr 1; omega
have hnew : nB { s with rack := r } i = nB s (i - 1) := by
simp only [nB]; congr 3; omega
have hlast : lo + ((w + 1 : Nat) : Int) - 1 = i := by rw [hi]; push_cast; omega
have cS : CF (rowWidth (rowOf s lo W)) (swapBlocks (rowWidth A) (2 * nB s (i - 1)) (rowWidth (Fx s i)) false) := by
apply CF_swapBlocks
rw [rowOf_split s lo (show w ≤ W by omega), rowWidth_append, erest, ← hPw']
simp only [rowWidth_append, stkO_width]; omega
refine ⟨A ++ Fx s i ++ stkO (nB s (i - 1)) ++ stkO (nB s i), M ++ swapBlocks (rowWidth A) (2 * nB s (i - 1))
(rowWidth (Fx s i)) false, ?_, ?_, ?_, ?_, ?_, hC.append cS, ?_⟩
· rw [hlast, rowOf_succ, ← hi, seg_Fx, hnew]; simp only [List.append_assoc]; rfl
· simp only [rowOf_succ, List.length_append, ← hi, seg_length, stkO_length]; omega
· simp only [rowOf_succ, rowWidth_append, ← hi, seg_width', stkO_width]; omega
· rw [flatMap_range_succ, hstep, D_app hD hD₂]; simp
· simp only [List.mem_append] at hY ⊢; tauto
· rw [flatMap_range_succ, hdraw]; exact iso_app hI (Iso.refl _ _) hC cS The rack case
theorem rack_step_lin {s : State} {r : Int} (h : validLin s (.rack r)) : (r - s.rack).natAbs = 1Proof
:= by
simp only [validLin, linRefusal, erase] at h
split_ifs at h with h1 <;> simp_all theorem rackCase : RackCaseProof
:= by
intro r win s s' hs hw hI
obtain ⟨hv, rfl⟩ := step_ok.mp hs
have hr := rack_step_lin (validLin_of_full hv)
have hy := yarn_mem_of_count (count_yarn_rowOf_inv hI)
obtain ⟨hB, hL, -⟩ := hI
have hsim : simOp win s (.rack r) = rackSteps win s r := rfl
have happ : apply s (.rack r) = { s with rack := r } := rfl
rw [hsim, rackSteps_eq, expand_rack, happ, windowWidth_rowOf]
have hend : rowOf s (win.lo + win.w) (win.w - win.w) = [] := by rw [Nat.sub_self]; rfl
by_cases hlt : r < s.rack
· have hr' : r = s.rack - 1 := by omega
have h0 : nB s win.lo = 0 := nB_zero_of fun hl => by
have := inside_bounds (windowOk_rack hw (hB _ hl) hl); omega
have hW : nB s (win.lo + win.w) = 0 := nB_zero_of fun hl => by
have := hL _ hl; simp only [slot] at this; omega
obtain ⟨P, M, hP, -, -, hD, -, -, hI'⟩ := down_run s r hr' win.lo win.w hy h0 win.w le_rfl
rw [hW] at hP
rw [hend, List.append_nil] at hD
refine ⟨M, ?_, hI'⟩
rw [hD, hP]; simp [stkO]
· have hr' : r = s.rack + 1 := by omega
have h0 : nB s (win.lo - 1) = 0 := nB_zero_of fun hl => by
have := hL _ hl; simp only [slot] at this; omega
have hW : nB s (win.lo + win.w - 1) = 0 := nB_zero_of fun hl => by
have := inside_bounds (windowOk_rack hw (hB _ hl) hl); omega
obtain ⟨P, M, hP, -, -, hD, -, -, hI'⟩ := up_run s r hr' win.lo win.w hy h0 win.w le_rfl
rw [hW] at hP
rw [hend, List.append_nil] at hD
refine ⟨M, ?_, hI'⟩
rw [hD, hP]; simp [stkO] The read functor R : 𝒦 ⥤ 𝒮
R reads each knitout program as a shorthand program with sim. The reading draws the same tangle as the program
(read_good).
| Declaration | Content |
|---|---|
rowL |
The stack row of a count state. Any window that holds the state gives this row (rowL_eq, rowL_eq'). |
wK |
The strand count of a count state (rowWidth_rowL) |
read_good |
The reading goes from rowL a to rowL b. Its drawing is Iso to the drawing of the program. |
wf_good |
The drawing of a program goes from wK a strands to wK b strands. |
Rmap |
The reading of a program, in the window winHom. Rmap_val: the same reading in any good window. |
R |
The read functor: rowL on objects, Rmap on arrows |
The row of a count state
theorem noEmpty_rowOf (s : State) (lo : Int) (w : Nat) : NoEmpty (MR.rowOf s lo w)Proof
:= by
intro k hk
simp only [MR.rowOf, List.mem_flatMap, MR.seg, List.mem_append, MR.stkO] at hk
obtain ⟨_, _, hk⟩ := hk
rcases hk with (hk | hk) | hk
· split_ifs at hk <;> simp at hk
· split_ifs at hk with h0
· simp at hk
· simp at hk; omega
· split_ifs at hk with h0
· simp at hk
· simp at hk; omega noncomputable def rowL (a : KnitoutObj) : ShorthandObj :=
⟨MR.rowOf (lift a.t) (winOf a).lo (winOf a).w, MR.count_yarn_rowOf_inv (winOf_inv a), noEmpty_rowOf _ _ _⟩theorem rowL_eq' {a : KnitoutObj} {win : Window} {s : State} (h : MR.Inv win s) (hs : SameCounts s (lift a.t)) :
(rowL a).row = MR.rowOf s win.lo win.wProof
:= by
show MR.rowOf (lift a.t) (winOf a).lo (winOf a).w = _
rw [← rowOf_widen (winOf_inv a) (sub_hull_left (winOf a) win), ← rowOf_widen h (sub_hull_right (winOf a) win),
MR.rowOf_erase hs] theorem rowL_eq {a : KnitoutObj} {win : Window} (h : MR.Inv win (lift a.t)) :
(rowL a).row = Bridge.MR.rowOf (lift a.t) win.lo win.wProof
:=
rowL_eq' h rfl noncomputable def wK (a : KnitoutObj) : Nat := windowWidth (winOf a) (lift a.t)theorem wK_eq' {a : KnitoutObj} {win : Window} {s : State} (h : MR.Inv win s) (hs : SameCounts s (lift a.t)) :
wK a = windowWidth win sProof
:= by
show windowWidth (winOf a) (lift a.t) = _
rw [← windowWidth_widen (winOf_inv a) (sub_hull_left (winOf a) win),
← windowWidth_widen h (sub_hull_right (winOf a) win), windowWidth_erase hs] theorem rowWidth_rowL (a : KnitoutObj) : rowWidth (rowL a).row = wK aProof
:=
(MR.windowWidth_rowOf _ _).symm theorem read_good {a b : KnitoutObj} (f : KnitoutProgram a b) {win : Window} (hg : Good win (lift a.t) f.1) :
∃ M, D (rowL a).row (MR.sim win (lift a.t) f.1) = some ((rowL b).row, M) ∧
Iso (wK a) M (expand sig (bw win (lift a.t) f.1))Proof
:= by
obtain ⟨hI, hN, t, B, hd⟩ := hg
obtain ⟨M, hD, hiso⟩ := MR.sim_correct hop win _ _ t B hI hN hd
have hst := sameCounts_end f (runSteps_of_denote hd)
have hIt := MR.inv_denote win _ _ _ _ hI hN hd
rw [rowL_eq hI, rowL_eq' hIt hst, wK_eq' hI rfl, bw_eq hd]
exact ⟨M, hD, hiso⟩ theorem wf_good {a b : KnitoutObj} (f : KnitoutProgram a b) {win : Window} (hg : Good win (lift a.t) f.1) :
WF (wK a) (wK b) (expand sig (bw win (lift a.t) f.1))Proof
:= by
obtain ⟨M, hD, hiso⟩ := read_good f hg
have := MR.D_wf hD
rw [rowWidth_rowL, rowWidth_rowL, hiso.topWidth_eq] at this
exact this The read functor
noncomputable def Rmap {a b : KnitoutObj} (f : KnitoutProgram a b) : ShorthandProgram (rowL a) (rowL b) :=
⟨MR.sim (winHom f) (lift a.t) f.1, (read_good f (winHom_good f)).imp fun _ h => h.1⟩theorem Rmap_val {a b : KnitoutObj} (f : KnitoutProgram a b) {win : Window} (h : Good win (lift a.t) f.1) :
(Rmap f).1 = MR.sim win (lift a.t) f.1Proof
:=
sim_indep (winHom_good f) h noncomputable def R : KnitoutObj ⥤ ShorthandObj where
obj := rowL
map f := Rmap f
map_id a := Subtype.ext rfl
map_comp {a b c} f g := by
apply Subtype.ext
show (Rmap (KnitoutProgram.comp f g)).1 = (Rmap f).1 ++ (Rmap g).1
have hg := winHom_good (KnitoutProgram.comp f g)
obtain ⟨hp, s1, hs1, hq, -⟩ := good_split hg
have hc := sameCounts_end f hs1
rw [Rmap_val (KnitoutProgram.comp f g) hg, show (KnitoutProgram.comp f g).1 = f.1 ++ g.1 from rfl,
MR.sim_append _ _ _ _ _ hs1, Rmap_val f hp, Rmap_val g (good_erase hc hq), MR.sim_erase _ _ _ _ hc]theorem R_obj (a : KnitoutObj) : R.obj a = rowL aProof
:= rfl theorem R_map_val {a b : KnitoutObj} (f : a ⟶ b) {win : Window} (h : Good win (lift a.t) (show KnitoutProgram a b from f).1) :
(show ShorthandProgram (R.obj a) (R.obj b) from R.map f).1 = Bridge.MR.sim win (lift a.t) (show KnitoutProgram a b from f).1Proof
:=
Rmap_val f h