The theorems
| Lean name | Statement | Meaning |
|---|---|---|
retract | C ⋙ R = 𝟭 ShorthandObj | Compile a shorthand program, then read it. The result is the same program, step for step. |
R_drawS | R ⋙ drawS = drawK | A knitout program and its reading draw the same tangle. |
C_drawK | C ⋙ drawK = drawS | A shorthand program and its compilation draw the same tangle. This follows from the two theorems above. |
The three theorems use only the standard axioms propext, Classical.choice and
Quot.sound. No proof uses sorry.
Why work in 𝒮
-
𝒮 and 𝒦 draw the same tangles. Each knitout program draws the same tangle as its reading (
R_drawS). Each shorthand program draws the same tangle as its compilation (C_drawK). -
C loses no information. Two different shorthand programs compile to two different knitout programs, because R
gives back each one (
retract). - 𝒮 is smaller than 𝒦. A row holds one yarn and stacks of loops. There are six steps. There are no needle positions and no racking.
- So a question about the tangles that knitout can make is a question about 𝒮. A result proved in 𝒮 transfers to 𝒦 through C and R.
- For example, a hand stitch is possible on the machine exactly when some shorthand program makes its tangle. To show that a stitch is impossible on the machine, show that no shorthand program makes it.
What "retract over 𝒯" means
- A retract: two functors C : 𝒮 → 𝒦 and R : 𝒦 → 𝒮 with R ∘ C = id𝒮.
- Over 𝒯: both functors keep the drawing. The triangles in the diagram commute.
- 𝒮 and 𝒦 are not isomorphic. C ∘ R is not the identity of 𝒦. Different knitout states can read as the same row, for example the same loops on other needles.
The proof
The proof of R ∘ C = id𝒮 goes step by step. C compiles each step to a fragment of knitout. Each lemma below shows that R reads one kind of fragment back as its step. The definitions of C and R are on their own pages.
Contents
- Free moves read as nothing
- The moves of
arrangeread as nothing - A compiled program reads back as the program: the pieces
- The canonical layout, read
- The drop, tuck and knit fragments read as their steps
- The merge fragment reads as the merge
- The split fragment reads as the split
- The exchange fragment reads as the exchange
- Compile, then read: the same shorthand program
- 𝒮 is a retract of 𝒦 over 𝒯
Free moves read as nothing
The compiler moves stacks and the carrier with free moves: a transfer onto an empty needle, a rack that moves no
stack past an object, a miss past empty needles. sim reads each free move as the empty shorthand program.
| Theorem | Content |
|---|---|
sim_append |
The reading of p ++ q is the reading of p, then the reading of q from the end state of p. |
simOp_xfer_empty |
A transfer onto an empty needle reads as nothing. |
rackSteps_nil |
A rack reads as nothing when no moving back stack meets an object. |
sim_loopStep_right, sim_loopStep_left |
One whole-stack move into a free position reads as nothing. |
sim_miss_R, sim_miss_L |
A miss past an empty position reads as nothing. |
namespace FormalizeKnitting.Bridge.MR
open FormalizeKnitting.Knitout FormalizeKnitting.Knitout.MR
theorem sim_append (win : Window) :
∀ (s s' : State) (p q : List Op), runSteps s p = some s' → sim win s (p ++ q) = sim win s p ++ sim win s' qProof
| s, s', [], q, h => by
simp only [runSteps, Option.some.injEq] at h
subst h; simp [sim]
| s, s', op :: ops, q, h => by
simp only [runSteps] at h
split at h
· rename_i s1 hs
simp only [List.cons_append, sim, hs, List.append_assoc]
rw [sim_append win s1 s' ops q h]
· exact absurd h (by simp) theorem simOp_xfer_empty (win : Window) (s : State) (a b : Needle) (h : s.stk b = []) :
simOp win s (.xfer a b) = []Proof
:= by
simp [simOp, h] theorem rackSteps_nil (win : Window) (s : State) (r : Int)
(h : ∀ k < win.w,
(r < s.rack → nB s (win.lo + k) = 0 ∨ nY s (win.lo + k) + ob (nF s (win.lo + k)) = 0) ∧
(¬ r < s.rack → (s.stk ⟨.b, win.lo + k - 1 - s.rack⟩).length = 0 ∨
nY s (win.lo + k) + ob (nF s (win.lo + k)) = 0)) :
rackSteps win s r = []Proof
:= by
unfold rackSteps
rw [List.flatMap_eq_nil_iff]
intro k hk
simp at hk
obtain ⟨a, ha, rfl⟩ := hk
obtain ⟨h1, h2⟩ := h a ha
by_cases hr : r < s.rack
· simp only [hr, ite_true]
rcases h1 hr with e | e
· simp [e]
· split <;> simp [e]
· simp only [hr, ite_false]
rcases h2 hr with e | e
· simp [e]
· split <;> simp [e] theorem step_xfer_of_aligned (s : State) (a b : Needle) (h : aligned s.rack a b = true) :
step s (.xfer a b) = .ok (apply s (.xfer a b))Proof
:= by
simp [step, refusal, linRefusal, extraRefusal, erase, h] theorem step_rack_of_step (s : State) (r : Int) (h : (r - s.rack).natAbs = 1) :
step s (.rack r) = .ok (apply s (.rack r))Proof
:= by
simp [step, refusal, linRefusal, extraRefusal, erase, h] theorem sim_loopStep_right (win : Window) (st : State) (n : Int) (hr : st.rack = 0)
(hb : ∀ j, st.stk ⟨.b, j⟩ = []) (hc : st.carrier ≠ n + 1) (hf : st.stk ⟨.f, n + 1⟩ = []) :
sim win st (loopStep n true) = []Proof
:= by
have a1 : aligned st.rack (fN n) (bN n) = true := by simp [aligned, slot, fN, bN, hr]
set s1 := apply st (.xfer (fN n) (bN n)) with hs1
have r1 : (1 - s1.rack).natAbs = 1 := by simp [hs1, apply, hr]
set s2 := apply s1 (.rack 1) with hs2
have a2 : aligned s2.rack (bN n) (fN (n + 1)) = true := by simp [hs2, hs1, apply, aligned, slot, fN, bN]
set s3 := apply s2 (.xfer (bN n) (fN (n + 1))) with hs3
have r3 : (0 - s3.rack).natAbs = 1 := by simp [hs3, hs2, apply]
have x1 : simOp win st (.xfer (fN n) (bN n)) = [] := simOp_xfer_empty win st _ _ (hb n)
have x2 : simOp win s1 (.rack 1) = [] := by
apply rackSteps_nil
intro k _
refine ⟨fun h => by simp [hs1, apply, hr] at h, fun _ => ?_⟩
by_cases e : win.lo + k - 1 - s1.rack = n
· right
have ek : win.lo + (k : Int) = n + 1 := by simp [hs1, apply, hr] at e; omega
simp [nY, nF, ob, hs1, apply, ek, hc, fN, bN, hf]
· left
simp only [hs1, apply, upd_apply] at e ⊢
rw [ite_eq_right (by intro h; apply e; simp [bN] at h; simpa [hr] using h), ite_eq_right (by simp [fN])]
simp [hb]
have x3 : simOp win s2 (.xfer (bN n) (fN (n + 1))) = [] := by
apply simOp_xfer_empty
simp [hs2, hs1, apply, fN, bN, hf]
have x4 : simOp win s3 (.rack 0) = [] := by
apply rackSteps_nil
intro k _
refine ⟨fun _ => Or.inl ?_, fun h => absurd (by simp [hs3, hs2, apply]) h⟩
simp only [nB, hs3, hs2, hs1, apply, upd_apply]
split_ifs <;> simp_all [fN, bN]
simp only [loopStep, ite_true, sim, step_xfer_of_aligned st _ _ a1, ← hs1, step_rack_of_step s1 1 r1, ← hs2,
step_xfer_of_aligned s2 _ _ a2, ← hs3, step_rack_of_step s3 0 r3, x1, x2, x3, x4, List.nil_append] theorem sim_loopStep_left (win : Window) (st : State) (n : Int) (hr : st.rack = 0)
(hb : ∀ j, st.stk ⟨.b, j⟩ = []) (hc : st.carrier ≠ n) (hf : st.stk ⟨.f, n - 1⟩ = []) :
sim win st (loopStep n false) = []Proof
:= by
have a1 : aligned st.rack (fN n) (bN n) = true := by simp [aligned, slot, fN, bN, hr]
set s1 := apply st (.xfer (fN n) (bN n)) with hs1
have r1 : (-1 - s1.rack).natAbs = 1 := by simp [hs1, apply, hr]
set s2 := apply s1 (.rack (-1)) with hs2
have a2 : aligned s2.rack (bN n) (fN (n - 1)) = true := by
simp [hs2, hs1, apply, aligned, slot, fN, bN]; omega
set s3 := apply s2 (.xfer (bN n) (fN (n - 1))) with hs3
have r3 : (0 - s3.rack).natAbs = 1 := by simp [hs3, hs2, apply]
have x1 : simOp win st (.xfer (fN n) (bN n)) = [] := simOp_xfer_empty win st _ _ (hb n)
have x2 : simOp win s1 (.rack (-1)) = [] := by
apply rackSteps_nil
intro k _
refine ⟨fun _ => ?_, fun h => absurd (by simp [hs1, apply, hr]) h⟩
by_cases e : win.lo + (k : Int) = n
· right
simp [nY, nF, ob, hs1, apply, e, hc, fN, bN]
· left
simp only [nB, hs1, apply, upd_apply, hr, sub_zero]
rw [ite_eq_right (by simp [bN]; omega), ite_eq_right (by simp [fN])]
simp [hb]
have x3 : simOp win s2 (.xfer (bN n) (fN (n - 1))) = [] := by
apply simOp_xfer_empty
simp only [hs2, hs1, apply, upd_apply]
split_ifs <;> simp_all [fN, bN]
have x4 : simOp win s3 (.rack 0) = [] := by
apply rackSteps_nil
intro k _
refine ⟨fun h => absurd h (by simp [hs3, hs2, apply]), fun _ => Or.inl ?_⟩
simp only [hs3, hs2, hs1, apply, upd_apply]
split_ifs <;> simp_all [fN, bN]
simp only [loopStep, Bool.false_eq_true, ite_false, sim, step_xfer_of_aligned st _ _ a1, ← hs1,
step_rack_of_step s1 (-1) r1, ← hs2, step_xfer_of_aligned s2 _ _ a2, ← hs3, step_rack_of_step s3 0 r3, x1, x2,
x3, x4, List.nil_append] theorem sim_miss_R (win : Window) (st : State) (z : Int) (hr : st.rack = 0) (hc : st.carrier = z)
(hf : st.stk ⟨.f, z⟩ = []) (hb : st.stk ⟨.b, z⟩ = []) :
step st (.miss .R (fN z)) = .ok { st with carrierProof
:= z + 1 } ∧ simOp win st (.miss .R (fN z)) = [] := by
constructor
· simp [step, refusal, linRefusal, extraRefusal, erase, apply, arrivesFrom, leavesTo, slot, fN, hr, hc]
· simp [simOp, slot, fN, hr, nF, nB, ob, hf, hb, xs] theorem sim_miss_L (win : Window) (st : State) (z : Int) (hr : st.rack = 0) (hc : st.carrier = z + 1)
(hf : st.stk ⟨.f, z⟩ = []) (hb : st.stk ⟨.b, z⟩ = []) :
step st (.miss .L (fN z)) = .ok { st with carrierProof
:= z } ∧ simOp win st (.miss .L (fN z)) = [] := by
constructor
· simp [step, refusal, linRefusal, extraRefusal, erase, apply, arrivesFrom, leavesTo, slot, fN, hr, hc]
· simp [simOp, slot, fN, hr, nF, nB, ob, hf, hb, xs] The moves of arrange read as nothing
arrangeReadFree : ArrangeReadFree. sim reads the moves of arrange as the empty shorthand program.
ArrangeFree(proof:arrangeFree) states thatstepaccepts the moves ofarrange, that they draw nothing, and that they end laid out.- Each
…_readlemma has the hypotheses of a lemma of thearrangeFreeproof (for examplewalkStackR,inv1_step). Its conclusion:simof the same instructions is[]. - The intermediate states come from the lemmas of the
arrangeFreeproof. - Each one-needle move reads as nothing by
sim_loopStep_right,sim_loopStep_left,sim_miss_Rorsim_miss_L.
| Lemmas | Moves |
|---|---|
stackR_read, stackL_read, yarnR_read, yarnL_read |
One step of a stack or of the yarn |
walkStackR_read, walkStackL_read, walkYarnR_read, walkYarnL_read |
A walk of steps |
moveObject_read |
One whole move of an object |
move_read, inv1_step_read, inv2_step_read, fold1_read, fold2_read |
The folds of arrange |
namespace FormalizeKnitting.Bridge.RM
open FormalizeKnitting.Knitout FormalizeKnitting.Bridge.RM
open scoped List
def ArrangeReadFree : Prop :=
∀ (win : Window) (c c' : Comp) (target : List Int) (st : State), Laid c st → Fits win c →
arrange c target = some c' → c.ops <+: c'.ops → (∀ op ∈ c'.ops, NamedIn3 win op) →
MR.sim win st (c'.ops.drop c.ops.length) = []theorem sim_nil_append {win : Window} {st st1 : State} {p q : List Op} {M : BWord Label}
(hd : denote win st p = some (st1, M)) (hp : MR.sim win st p = []) (hq : MR.sim win st1 q = []) :
MR.sim win st (p ++ q) = []Proof
:= by
rw [MR.sim_append win st st1 p q (runSteps_of_denote hd), hp, hq]; rfl One step
theorem stackR_read (win : Window) (r : Row) (a : List Int) (st : State) (j s : Nat) (x : Int)
(hL : Laid ⟨r, a, []⟩ st) (hj : j < r.length) (hl : r.getD j .yarn = .stack s) (hx : a.getD j 0 = x)
(hnext : j + 1 < r.length → 2 * (x + 1) < cp (r.map shObj) a (j + 1)) :
MR.sim win st (loopStep x true) = []Proof
:= by
have hL' := hL
rw [laid_unfold] at hL'
obtain ⟨hy, hlen, hr, hb, hf, hc, hs0⟩ := hL'
have hs := sorted_sh hs0
have hlenR : (r.map shObj).length = r.length := by simp
have hpj : cp (r.map shObj) a j = 2 * x := by rw [cp_stack (by rw [hl]; simp), hx]
have hprev : 0 < j → cp (r.map shObj) a (j - 1) < 2 * (x + 1) := fun h => by
have := hs (j - 1) (by rw [hlenR]; omega); rw [show j - 1 + 1 = j by omega] at this; omega
have hfar := far_of_sorted hs (by rw [hlenR]; exact hj) hprev (by rw [hlenR]; exact hnext)
obtain ⟨hyl, hyy⟩ := yarnIdx_spec hy
have hyj : yarnIdx r ≠ j := by intro h; rw [h, hl] at hyy; cases hyy
have hcar : st.carrier ≠ x + 1 := by
rw [hc]; intro h
have hpy : cp (r.map shObj) a (yarnIdx r) = 2 * (x + 1) - 1 := by
rw [cp_sh, hyy, h]; rfl
rcases hfar _ (by rw [hlenR]; exact hyl) hyj with h' | h' <;> omega
have hcnt : loadAt r a (x + 1) = 0 := loadAt_zero r a (x + 1) (fun k hk hlk hak => by
have hkj : k ≠ j := by rintro rfl; rw [hx] at hak; omega
have hpk : cp (r.map shObj) a k = 2 * (x + 1) := by rw [cp_stack hlk, hak]
rcases hfar k (by rw [hlenR]; exact hk) hkj with h' | h' <;> omega)
have hf1 : st.stk ⟨.f, x + 1⟩ = [] := List.eq_nil_of_length_eq_zero (by rw [hf]; exact hcnt)
exact MR.sim_loopStep_right win st x hr hb hcar hf1 theorem stackL_read (win : Window) (r : Row) (a : List Int) (st : State) (j s : Nat) (x : Int)
(hL : Laid ⟨r, a, []⟩ st) (hj : j < r.length) (hl : r.getD j .yarn = .stack s) (hx : a.getD j 0 = x)
(hprev : 0 < j → cp (r.map shObj) a (j - 1) < 2 * (x - 1)) :
MR.sim win st (loopStep x false) = []Proof
:= by
have hL' := hL
rw [laid_unfold] at hL'
obtain ⟨hy, hlen, hr, hb, hf, hc, hs0⟩ := hL'
have hs := sorted_sh hs0
have hlenR : (r.map shObj).length = r.length := by simp
have hpj : cp (r.map shObj) a j = 2 * x := by rw [cp_stack (by rw [hl]; simp), hx]
have hnext : j + 1 < r.length → 2 * (x - 1) < cp (r.map shObj) a (j + 1) := fun h => by
have := hs j (by rw [hlenR]; exact h); omega
have hfar := far_of_sorted hs (by rw [hlenR]; exact hj) hprev (by rw [hlenR]; exact hnext)
obtain ⟨hyl, hyy⟩ := yarnIdx_spec hy
have hyj : yarnIdx r ≠ j := by intro h; rw [h, hl] at hyy; cases hyy
have hcar : st.carrier ≠ x := by
rw [hc]; intro h
have hpy : cp (r.map shObj) a (yarnIdx r) = 2 * x - 1 := by
rw [cp_sh, hyy, h]; rfl
rcases hfar _ (by rw [hlenR]; exact hyl) hyj with h' | h' <;> omega
have hcnt : loadAt r a (x - 1) = 0 := loadAt_zero r a (x - 1) (fun k hk hlk hak => by
have hkj : k ≠ j := by rintro rfl; rw [hx] at hak; omega
have hpk : cp (r.map shObj) a k = 2 * (x - 1) := by rw [cp_stack hlk, hak]
rcases hfar k (by rw [hlenR]; exact hk) hkj with h' | h' <;> omega)
have hf1 : st.stk ⟨.f, x - 1⟩ = [] := List.eq_nil_of_length_eq_zero (by rw [hf]; exact hcnt)
exact MR.sim_loopStep_left win st x hr hb hcar hf1 theorem yarnR_read (win : Window) (r : Row) (a : List Int) (st : State) (j : Nat) (x : Int)
(hL : Laid ⟨r, a, []⟩ st) (hj : j < r.length) (hl : r.getD j .yarn = .yarn) (hx : a.getD j 0 = x)
(hnext : j + 1 < r.length → 2 * (x + 1) - 1 < cp (r.map shObj) a (j + 1)) :
MR.sim win st [.miss .R (fN x)] = []Proof
:= by
have hL' := hL
rw [laid_unfold] at hL'
obtain ⟨hy, hlen, hr, hb, hf, hc, hs0⟩ := hL'
have hs := sorted_sh hs0
have hlenR : (r.map shObj).length = r.length := by simp
have hpj : cp (r.map shObj) a j = 2 * x - 1 := by rw [cp_sh, hl, hx]; rfl
have hprev : 0 < j → cp (r.map shObj) a (j - 1) < 2 * (x + 1) - 1 := fun h => by
have := hs (j - 1) (by rw [hlenR]; omega); rw [show j - 1 + 1 = j by omega] at this; omega
have hfar := far_of_sorted hs (by rw [hlenR]; exact hj) hprev (by rw [hlenR]; exact hnext)
have hcar : st.carrier = x := by rw [hc, yarnIdx_of hy hj hl, hx]
have hcnt : loadAt r a x = 0 := loadAt_zero r a x (fun k hk hlk hak => by
have hkj : k ≠ j := by rintro rfl; exact hlk hl
have hpk : cp (r.map shObj) a k = 2 * x := by rw [cp_stack hlk, hak]
rcases hfar k (by rw [hlenR]; exact hk) hkj with h' | h' <;> omega)
have hf1 : st.stk ⟨.f, x⟩ = [] := List.eq_nil_of_length_eq_zero (by rw [hf]; exact hcnt)
obtain ⟨h1, h2⟩ := MR.sim_miss_R win st x hr hcar hf1 (hb x)
simp only [MR.sim, h1, h2, List.append_nil] theorem yarnL_read (win : Window) (r : Row) (a : List Int) (st : State) (j : Nat) (x : Int)
(hL : Laid ⟨r, a, []⟩ st) (hj : j < r.length) (hl : r.getD j .yarn = .yarn) (hx : a.getD j 0 = x)
(hprev : 0 < j → cp (r.map shObj) a (j - 1) < 2 * (x - 1) - 1) :
MR.sim win st [.miss .L (fN (x - 1))] = []Proof
:= by
have hL' := hL
rw [laid_unfold] at hL'
obtain ⟨hy, hlen, hr, hb, hf, hc, hs0⟩ := hL'
have hs := sorted_sh hs0
have hlenR : (r.map shObj).length = r.length := by simp
have hpj : cp (r.map shObj) a j = 2 * x - 1 := by rw [cp_sh, hl, hx]; rfl
have hnext : j + 1 < r.length → 2 * (x - 1) - 1 < cp (r.map shObj) a (j + 1) := fun h => by
have := hs j (by rw [hlenR]; exact h); omega
have hfar := far_of_sorted hs (by rw [hlenR]; exact hj) hprev (by rw [hlenR]; exact hnext)
have hcar : st.carrier = x := by rw [hc, yarnIdx_of hy hj hl, hx]
have hcnt : loadAt r a (x - 1) = 0 := loadAt_zero r a (x - 1) (fun k hk hlk hak => by
have hkj : k ≠ j := by rintro rfl; exact hlk hl
have hpk : cp (r.map shObj) a k = 2 * (x - 1) := by rw [cp_stack hlk, hak]
rcases hfar k (by rw [hlenR]; exact hk) hkj with h' | h' <;> omega)
have hf1 : st.stk ⟨.f, x - 1⟩ = [] := List.eq_nil_of_length_eq_zero (by rw [hf]; exact hcnt)
obtain ⟨h1, h2⟩ := MR.sim_miss_L win st (x - 1) hr (by rw [hcar]; omega) hf1 (hb (x - 1))
simp only [MR.sim, h1, h2, List.append_nil] Walks
theorem walkStackR_read (win : Window) (r : Row) (a : List Int) (st : State) (j s : Nat) (x : Int)
(hL : Laid ⟨r, a, []⟩ st) (hj : j < r.length) (hl : r.getD j .yarn = .stack s) (hx : a.getD j 0 = x) :
∀ m : Nat, (j + 1 < r.length → 2 * (x + m) < cp (r.map shObj) a (j + 1)) →
(∀ k < m, win.inside (x + k) = true ∧ win.inside (x + k + 1) = true) →
MR.sim win st ((List.range m).flatMap fun k : Nat => loopStep (x + (k : Int)) true) = []Proof
| 0, _, _ => by simp [MR.sim]
| m + 1, hn, hin => by
have hn' : j + 1 < r.length → 2 * (x + m) < cp (r.map shObj) a (j + 1) :=
fun h => by have := hn h; push_cast at this; omega
have hin' : ∀ k < m, win.inside (x + k) = true ∧ win.inside (x + k + 1) = true := fun k hk => hin k (by omega)
obtain ⟨st1, hd1, hL1⟩ := walkStackR win r a st j s x hL hj hl hx m hn' hin'
have h0 := walkStackR_read win r a st j s x hL hj hl hx m hn' hin'
have hj' : j < a.length := by have := laid_len hL; omega
have h1 := stackR_read win r (a.set j (x + m)) st1 j s (x + m) hL1 hj hl
(by rw [getD_set_eq a j j _ 0 hj', ite_eq_left rfl])
(fun h => by rw [cp_set_ne' hj' (show j + 1 ≠ j by omega)]; have := hn h; push_cast at this; omega)
simp only [List.range_succ, List.flatMap_append, List.flatMap_cons, List.flatMap_nil, List.append_nil]
simpa using sim_nil_append hd1 h0 h1 theorem walkStackL_read (win : Window) (r : Row) (a : List Int) (st : State) (j s : Nat) (x : Int)
(hL : Laid ⟨r, a, []⟩ st) (hj : j < r.length) (hl : r.getD j .yarn = .stack s) (hx : a.getD j 0 = x) :
∀ m : Nat, (0 < j → cp (r.map shObj) a (j - 1) < 2 * (x - m)) →
(∀ k < m, win.inside (x - k) = true ∧ win.inside (x - k - 1) = true) →
MR.sim win st ((List.range m).flatMap fun k : Nat => loopStep (x - (k : Int)) false) = []Proof
| 0, _, _ => by simp [MR.sim]
| m + 1, hp, hin => by
have hp' : 0 < j → cp (r.map shObj) a (j - 1) < 2 * (x - m) :=
fun h => by have := hp h; push_cast at this; omega
have hin' : ∀ k < m, win.inside (x - k) = true ∧ win.inside (x - k - 1) = true := fun k hk => hin k (by omega)
obtain ⟨st1, hd1, hL1⟩ := walkStackL win r a st j s x hL hj hl hx m hp' hin'
have h0 := walkStackL_read win r a st j s x hL hj hl hx m hp' hin'
have hj' : j < a.length := by have := laid_len hL; omega
have h1 := stackL_read win r (a.set j (x - m)) st1 j s (x - m) hL1 hj hl
(by rw [getD_set_eq a j j _ 0 hj', ite_eq_left rfl])
(fun h => by rw [cp_set_ne' hj' (show j - 1 ≠ j by omega)]; have := hp h; push_cast at this; omega)
simp only [List.range_succ, List.flatMap_append, List.flatMap_cons, List.flatMap_nil, List.append_nil]
simpa using sim_nil_append hd1 h0 h1 theorem walkYarnR_read (win : Window) (r : Row) (a : List Int) (st : State) (j : Nat) (x : Int)
(hL : Laid ⟨r, a, []⟩ st) (hj : j < r.length) (hl : r.getD j .yarn = .yarn) (hx : a.getD j 0 = x) :
∀ m : Nat, (j + 1 < r.length → 2 * (x + m) - 1 < cp (r.map shObj) a (j + 1)) →
(∀ k < m, win.inside (x + k) = true) →
MR.sim win st ((List.range m).map fun k : Nat => Op.miss .R (fN (x + (k : Int)))) = []Proof
| 0, _, _ => by simp [MR.sim]
| m + 1, hn, hin => by
have hn' : j + 1 < r.length → 2 * (x + m) - 1 < cp (r.map shObj) a (j + 1) :=
fun h => by have := hn h; push_cast at this; omega
have hin' : ∀ k < m, win.inside (x + k) = true := fun k hk => hin k (by omega)
obtain ⟨st1, hd1, hL1⟩ := walkYarnR win r a st j x hL hj hl hx m hn' hin'
have h0 := walkYarnR_read win r a st j x hL hj hl hx m hn' hin'
have hj' : j < a.length := by have := laid_len hL; omega
have h1 := yarnR_read win r (a.set j (x + m)) st1 j (x + m) hL1 hj hl
(by rw [getD_set_eq a j j _ 0 hj', ite_eq_left rfl])
(fun h => by rw [cp_set_ne' hj' (show j + 1 ≠ j by omega)]; have := hn h; push_cast at this; omega)
simp only [List.range_succ, List.map_append, List.map_cons, List.map_nil]
simpa using sim_nil_append hd1 h0 h1 theorem walkYarnL_read (win : Window) (r : Row) (a : List Int) (st : State) (j : Nat) (x : Int)
(hL : Laid ⟨r, a, []⟩ st) (hj : j < r.length) (hl : r.getD j .yarn = .yarn) (hx : a.getD j 0 = x) :
∀ m : Nat, (0 < j → cp (r.map shObj) a (j - 1) < 2 * (x - m) - 1) →
(∀ k < m, win.inside (x - 1 - k) = true) →
MR.sim win st ((List.range m).map fun k : Nat => Op.miss .L (fN (x - 1 - (k : Int)))) = []Proof
| 0, _, _ => by simp [MR.sim]
| m + 1, hp, hin => by
have hp' : 0 < j → cp (r.map shObj) a (j - 1) < 2 * (x - m) - 1 :=
fun h => by have := hp h; push_cast at this; omega
have hin' : ∀ k < m, win.inside (x - 1 - k) = true := fun k hk => hin k (by omega)
obtain ⟨st1, hd1, hL1⟩ := walkYarnL win r a st j x hL hj hl hx m hp' hin'
have h0 := walkYarnL_read win r a st j x hL hj hl hx m hp' hin'
have hj' : j < a.length := by have := laid_len hL; omega
have e1 : x - m - 1 = x - 1 - m := by omega
have h1 := yarnL_read win r (a.set j (x - m)) st1 j (x - m) hL1 hj hl
(by rw [getD_set_eq a j j _ 0 hj', ite_eq_left rfl])
(fun h => by rw [cp_set_ne' hj' (show j - 1 ≠ j by omega)]; have := hp h; push_cast at this; omega)
rw [e1] at h1
simp only [List.range_succ, List.map_append, List.map_cons, List.map_nil]
simpa using sim_nil_append hd1 h0 h1 theorem moveObject_read (win : Window) (c : Comp) (st : State) (j : Nat) (dest : Int)
(hL : Laid c st) (hj : j < c.row.length)
(hprev : 0 < j → cp (c.row.map shObj) c.at_ (j - 1) < cpos ((c.row.map shObj).getD j .yarn) dest)
(hnext : j + 1 < c.row.length → cpos ((c.row.map shObj).getD j .yarn) dest < cp (c.row.map shObj) c.at_ (j + 1))
(hN : ∀ op ∈ (moveObject c j dest).ops, NamedIn3 win op) :
MR.sim win st ((moveObject c j dest).ops.drop c.ops.length) = []Proof
:= by
obtain ⟨r, a, o⟩ := c
have hL0 : Laid ⟨r, a, []⟩ st := hL
have hN' := fun op h => hN op (by rw [moveObject_ops]; exact List.mem_append_right _ h)
rw [moveObject_ops, List.drop_left]
simp only at hj hprev hnext hN' ⊢
generalize hx : a.getD j 0 = x at hN' ⊢
cases ho : r.getD j .yarn with
| yarn =>
rw [getD_sh_yarn ho, cpos_yarn] at hprev hnext
rw [ho, ite_eq_left rfl] at hN'
rw [ite_eq_left rfl]
by_cases hlt : x < dest
· rw [ite_eq_left hlt] at hN' ⊢
obtain ⟨m, hm⟩ : ∃ m : Nat, (dest - x).toNat = m := ⟨_, rfl⟩
rw [hm] at hN' ⊢
exact walkYarnR_read win r a st j x hL0 hj ho hx m (fun h => by have := hnext h; omega)
(fun k hk => inside_of_named (hN' _ (List.mem_map.mpr ⟨k, List.mem_range.mpr hk, rfl⟩))
(by simp [Op.named, fN]))
· rw [ite_eq_right hlt] at hN' ⊢
obtain ⟨m, hm⟩ : ∃ m : Nat, (x - dest).toNat = m := ⟨_, rfl⟩
rw [hm] at hN' ⊢
exact walkYarnL_read win r a st j x hL0 hj ho hx m (fun h => by have := hprev h; omega)
(fun k hk => inside_of_named (hN' _ (List.mem_map.mpr ⟨k, List.mem_range.mpr hk, rfl⟩))
(by simp [Op.named, fN]))
| stack s =>
rw [getD_sh_stack ho, cpos_loop] at hprev hnext
rw [ho, ite_eq_right (by simp)] at hN'
rw [ite_eq_right (by simp)]
by_cases hlt : x < dest
· rw [ite_eq_left hlt] at hN' ⊢
obtain ⟨m, hm⟩ : ∃ m : Nat, (dest - x).toNat = m := ⟨_, rfl⟩
rw [hm] at hN' ⊢
have hmem : ∀ k < m, Op.xfer (bN (x + k)) (fN (x + k + 1)) ∈
(List.range m).flatMap (fun k : Nat => loopStep (x + (k : Int)) true) := fun k hk =>
List.mem_flatMap.mpr ⟨k, List.mem_range.mpr hk, by simp [loopStep]⟩
exact walkStackR_read win r a st j s x hL0 hj ho hx m (fun h => by have := hnext h; omega)
(fun k hk => ⟨inside_of_named (hN' _ (hmem k hk)) (by simp [Op.named, fN, bN]),
inside_of_named (hN' _ (hmem k hk)) (by simp [Op.named, fN, bN])⟩)
· rw [ite_eq_right hlt] at hN' ⊢
obtain ⟨m, hm⟩ : ∃ m : Nat, (x - dest).toNat = m := ⟨_, rfl⟩
rw [hm] at hN' ⊢
have hmem : ∀ k < m, Op.xfer (bN (x - k)) (fN (x - k - 1)) ∈
(List.range m).flatMap (fun k : Nat => loopStep (x - (k : Int)) false) := fun k hk =>
List.mem_flatMap.mpr ⟨k, List.mem_range.mpr hk, by simp [loopStep]⟩
exact walkStackL_read win r a st j s x hL0 hj ho hx m (fun h => by have := hprev h; omega)
(fun k hk => ⟨inside_of_named (hN' _ (hmem k hk)) (by simp [Op.named, fN, bN]),
inside_of_named (hN' _ (hmem k hk)) (by simp [Op.named, fN, bN])⟩) The folds
theorem move_read (win : Window) (r : Row) (A : List Int) (O : List Op) (st1 : State) (m : Nat) (d : Int)
(hL : Laid ⟨r, A, O⟩ st1) (hm : m < r.length)
(hprev : 0 < m → cp (r.map shObj) A (m - 1) < cpos ((r.map shObj).getD m .yarn) d)
(hnext : m + 1 < r.length → cpos ((r.map shObj).getD m .yarn) d < cp (r.map shObj) A (m + 1))
(hN : ∀ op ∈ (moveObject1 ⟨r.map shObj, A, O⟩ m d).ops, NamedIn3 win op) :
MR.sim win st1 ((moveObject1 ⟨r.map shObj, A, O⟩ m d).ops.drop O.length) = []Proof
:= by
have e : moveObject1 ⟨r.map shObj, A, O⟩ m d = sh (moveObject ⟨r, A, O⟩ m d) := moveObject_sh ⟨r, A, O⟩ m d
rw [e] at hN ⊢
exact moveObject_read win ⟨r, A, O⟩ st1 m d hL hm hprev hnext hN theorem inv1_step_read {win : Window} {c : Comp} {t : List Int} {st : State} (hL : Laid c st)
(hsT : ∀ j, j + 1 < (sh c).row.length → cp (sh c).row t j < cp (sh c).row t (j + 1)) {c' : Comp1} {m : Nat}
(hm : m < (sh c).row.length) (hI : Inv1 win c t st c' (m + 1))
(hR : MR.sim win st (c'.ops.drop c.ops.length) = [])
(hN : ∀ op ∈ (ar1 (sh c) t c' m).ops, NamedIn3 win op) :
MR.sim win st ((ar1 (sh c) t c' m).ops.drop c.ops.length) = []Proof
:= by
obtain ⟨hrow, hpre, hch, hF, st1, hd1, hL1⟩ := hI
have hS := orig_sorted hL
suffices H : MR.sim win st1 ((ar1 (sh c) t c' m).ops.drop c'.ops.length) = [] by
have hpre' : c.ops <+: c'.ops := hpre
rw [drop_trans hpre' (ar1_prefix _ t c' m)]
exact sim_nil_append hd1 hR H
have hN' := hN
unfold ar1 at hN' ⊢
split_ifs with hP
· have hprev : 0 < m → cp c'.row c'.at_ (m - 1) < cpos (c'.row.getD m .yarn) (t.getD m 0) := fun h => by
have e : cp (sh c).row c'.at_ (m - 1) = cp (sh c).row (sh c).at_ (m - 1) := by
simp only [cp]; rw [hch (m - 1) (by omega), ite_eq_right (fun h' => by omega)]
rw [hrow, e]
have := hS (m - 1) (by omega); rw [show m - 1 + 1 = m by omega] at this
exact lt_trans this hP
have hnext : m + 1 < c'.row.length → cpos (c'.row.getD m .yarn) (t.getD m 0) < cp c'.row c'.at_ (m + 1) := by
intro h
rw [hrow] at h ⊢
have ht := hsT m h
have hv : cp (sh c).row c'.at_ (m + 1) = cpos ((sh c).row.getD (m + 1) .yarn) (c'.at_.getD (m + 1) 0) := rfl
rw [hv, hch (m + 1) h]
by_cases hP1 : cp (sh c).row (sh c).at_ (m + 1) < cp (sh c).row t (m + 1)
· rw [ite_eq_left ⟨le_refl _, hP1⟩]; exact ht
· rw [ite_eq_right (fun h' => hP1 h'.2)]
exact lt_of_lt_of_le ht (not_lt.mp hP1)
obtain ⟨R, A, O⟩ := c'
simp only at hrow
subst hrow
exact move_read win c.row A O st1 m (t.getD m 0) hL1 (by simpa [sh] using hm) hprev
(fun h => hnext (by simpa [sh] using h)) (by rwa [ite_eq_left hP] at hN')
· simp [MR.sim] theorem inv2_step_read {win : Window} {c : Comp} {t : List Int} {st : State} (hL : Laid c st)
(hsT : ∀ j, j + 1 < (sh c).row.length → cp (sh c).row t j < cp (sh c).row t (j + 1)) {c' : Comp1} {m : Nat}
(hm : m < (sh c).row.length) (hI : Inv2 win c t st c' m)
(hR : MR.sim win st (c'.ops.drop c.ops.length) = [])
(hN : ∀ op ∈ (ar2 (sh c) t c' m).ops, NamedIn3 win op) :
MR.sim win st ((ar2 (sh c) t c' m).ops.drop c.ops.length) = []Proof
:= by
obtain ⟨hrow, hpre, hch, hF, st1, hd1, hL1⟩ := hI
have hS := orig_sorted hL
suffices H : MR.sim win st1 ((ar2 (sh c) t c' m).ops.drop c'.ops.length) = [] by
have hpre' : c.ops <+: c'.ops := hpre
rw [drop_trans hpre' (ar2_prefix _ t c' m)]
exact sim_nil_append hd1 hR H
have hN' := hN
unfold ar2 at hN' ⊢
split_ifs with hP
· have hprev : 0 < m → cp c'.row c'.at_ (m - 1) < cpos (c'.row.getD m .yarn) (t.getD m 0) := fun h => by
rw [hrow]
have ht := hsT (m - 1) (by omega); rw [show m - 1 + 1 = m by omega] at ht
have hv : cp (sh c).row c'.at_ (m - 1) = cpos ((sh c).row.getD (m - 1) .yarn) (c'.at_.getD (m - 1) 0) := rfl
rw [hv, hch (m - 1) (by omega)]
split_ifs with hQ
· exact ht
· have e : cp (sh c).row (sh c).at_ (m - 1) = cp (sh c).row t (m - 1) := by
rcases lt_trichotomy (cp (sh c).row (sh c).at_ (m - 1)) (cp (sh c).row t (m - 1)) with h1 | h1 | h1
· exact absurd (Or.inl h1) hQ
· exact h1
· exact absurd (Or.inr ⟨by omega, h1⟩) hQ
show cp (sh c).row (sh c).at_ (m - 1) < _
rw [e]; exact ht
have hnext : m + 1 < c'.row.length → cpos (c'.row.getD m .yarn) (t.getD m 0) < cp c'.row c'.at_ (m + 1) := by
intro h
rw [hrow] at h ⊢
have hv : cp (sh c).row c'.at_ (m + 1) = cpos ((sh c).row.getD (m + 1) .yarn) (c'.at_.getD (m + 1) 0) := rfl
rw [hv, hch (m + 1) h]
by_cases hP1 : cp (sh c).row (sh c).at_ (m + 1) < cp (sh c).row t (m + 1)
· rw [ite_eq_left (Or.inl hP1)]; exact hsT m h
· rw [ite_eq_right (fun h' => h'.elim hP1 (fun h'' => by omega))]
exact lt_trans hP (hS m h)
obtain ⟨R, A, O⟩ := c'
simp only at hrow
subst hrow
exact move_read win c.row A O st1 m (t.getD m 0) hL1 (by simpa [sh] using hm) hprev
(fun h => hnext (by simpa [sh] using h)) (by rwa [ite_eq_left hP] at hN')
· simp [MR.sim] theorem fold1_read {win : Window} {c : Comp} {t : List Int} {st : State} (hL : Laid c st)
(hsT : ∀ j, j + 1 < (sh c).row.length → cp (sh c).row t j < cp (sh c).row t (j + 1)) :
∀ m, m ≤ (sh c).row.length → ∀ c', Inv1 win c t st c' m → MR.sim win st (c'.ops.drop c.ops.length) = [] →
(∀ op ∈ ((List.range m).reverse.foldl (ar1 (sh c) t) c').ops, NamedIn3 win op) →
MR.sim win st (((List.range m).reverse.foldl (ar1 (sh c) t) c').ops.drop c.ops.length) = []Proof
| 0, _, c', _, hR, _ => by simpa using hR
| m + 1, hm, c', h, hR, hN => by
have e : (List.range (m + 1)).reverse = m :: (List.range m).reverse := by simp [List.range_succ]
rw [e, List.foldl_cons] at hN ⊢
have hpre := (foldl_row_prefix (ar1 (sh c) t) (fun c' j => ⟨ar1_row _ t c' j, ar1_prefix _ t c' j⟩)
(List.range m).reverse (ar1 (sh c) t c' m)).2
have hN1 : ∀ op ∈ (ar1 (sh c) t c' m).ops, NamedIn3 win op := fun op ho => hN op (hpre.subset ho)
exact fold1_read hL hsT m (by omega) _ (inv1_step hL hsT (by omega) h hN1)
(inv1_step_read hL hsT (by omega) h hR hN1) hN theorem fold2_read {win : Window} {c : Comp} {t : List Int} {st : State} (hL : Laid c st)
(hsT : ∀ j, j + 1 < (sh c).row.length → cp (sh c).row t j < cp (sh c).row t (j + 1)) :
∀ k s, s + k = (sh c).row.length → ∀ c', Inv2 win c t st c' s → MR.sim win st (c'.ops.drop c.ops.length) = [] →
(∀ op ∈ ((List.range' s k).foldl (ar2 (sh c) t) c').ops, NamedIn3 win op) →
MR.sim win st (((List.range' s k).foldl (ar2 (sh c) t) c').ops.drop c.ops.length) = []Proof
| 0, s, hs, c', _, hR, _ => by simpa using hR
| k + 1, s, hs, c', h, hR, hN => by
rw [List.range'_succ, List.foldl_cons] at hN ⊢
have hpre := (foldl_row_prefix (ar2 (sh c) t) (fun c' j => ⟨ar2_row _ t c' j, ar2_prefix _ t c' j⟩)
(List.range' (s + 1) k) (ar2 (sh c) t c' s)).2
have hN1 : ∀ op ∈ (ar2 (sh c) t c' s).ops, NamedIn3 win op := fun op ho => hN op (hpre.subset ho)
exact fold2_read hL hsT k (s + 1) (by omega) _ (inv2_step hL hsT (by omega) h hN1)
(inv2_step_read hL hsT (by omega) h hR hN1) hN theorem arrangeReadFree : ArrangeReadFreeProof
:= by
intro win c c' t st hL hF ha _ hN
have ha' : arrange1 (sh c) t = some (sh c') := by rw [arrange_sh, ha]; rfl
rw [arrange1_def] at ha'
split_ifs at ha' with hcond
obtain ⟨_, hall⟩ := hcond
have hsT := sorted_of_all hall
simp only [Option.some.injEq] at ha'
set c1 := (List.range (sh c).row.length).reverse.foldl (ar1 (sh c) t) (sh c) with hc1
have hN2 : ∀ op ∈ ((List.range (sh c).row.length).foldl (ar2 (sh c) t) c1).ops, NamedIn3 win op := by
rw [ha']; exact hN
have hpre2 := (foldl_row_prefix (ar2 (sh c) t) (fun c' j => ⟨ar2_row _ t c' j, ar2_prefix _ t c' j⟩)
(List.range (sh c).row.length) c1).2
have h0 : Inv1 win c t st (sh c) (sh c).row.length :=
⟨rfl, List.prefix_refl _, fun i hi => by rw [ite_eq_right (fun h => by omega)], hF, st, by simp [denote, sh], hL⟩
have hR0 : MR.sim win st ((sh c).ops.drop c.ops.length) = [] := by simp [MR.sim, sh]
have hN1 : ∀ op ∈ c1.ops, NamedIn3 win op := fun op ho => hN2 op (hpre2.subset ho)
have hR1 := fold1_read hL hsT (sh c).row.length (le_refl _) (sh c) h0 hR0 hN1
obtain ⟨hr1, hp1, hch1, hF1, st1, hd1, hL1⟩ := fold1_free hL hsT (sh c).row.length (le_refl _) (sh c) h0 hN1
have h2 : Inv2 win c t st c1 0 := by
refine ⟨hr1, hp1, fun i hi => ?_, hF1, st1, hd1, hL1⟩
rw [hch1 i hi]
by_cases hP : cp (sh c).row (sh c).at_ i < cp (sh c).row t i
· rw [ite_eq_left ⟨Nat.zero_le _, hP⟩, ite_eq_left (Or.inl hP)]
· rw [ite_eq_right (fun h => hP h.2), ite_eq_right (fun h => h.elim hP (fun h' => by omega))]
rw [List.range_eq_range'] at hN2
have hR2 := fold2_read hL hsT (sh c).row.length 0 (by omega) c1 h2 hR1 hN2
rw [← List.range_eq_range', ha'] at hR2
exact hR2 A compiled program reads back as the program: the pieces
| Declaration | Content |
|---|---|
FragmentReadFor S P |
From the canonical layout, the compiled fragment of a step that satisfies P reads as that one step. |
ArrangeRead |
The moves of arrange read as nothing. |
noEmpty_runStepB |
A step never makes an empty stack. |
sim_compileSteps |
Read back, a compiled program is the program. Hypotheses: the compiler pieces and the two read pieces. |
namespace FormalizeKnitting.Bridge.RM
open FormalizeKnitting.Knitout FormalizeKnitting.Bridge.MR
theorem noEmpty_splice {r : Row} (h : NoEmpty r) (f len : Nat) (N : Row) (hN : ∀ k, Obj.stack k ∈ N → 0 < k) :
NoEmpty (splice r f len N)Proof
:= by
intro k hk
simp only [splice, List.mem_append] at hk
rcases hk with (hk | hk) | hk
· exact h k (List.mem_of_mem_take hk)
· exact hN k hk
· exact h k (List.mem_of_mem_drop hk) theorem stackAt_pos {r : Row} {a k : Nat} (h : stackAt r a = some k) : 0 < kProof
:= by
unfold stackAt at h
split at h
· split at h
· simp at h; omega
· simp at h
· simp at h theorem getD_mem_of_lt {r : Row} {a : Nat} (h : a < r.length) : r.getD a .yarn ∈ rProof
:= by
rw [List.getD_eq_getElem?_getD, List.getElem?_eq_getElem h]; exact List.getElem_mem h theorem noEmpty_runStepB {r r' : Row} {s : Step} {M : BWord Label} (hr : NoEmpty r)
(h : runStepB r s = some (r', M)) : NoEmpty r'Proof
:= by
cases s with
| x a front =>
simp only [runStepB] at h
split at h
· rename_i hc
simp only [Option.some.injEq, Prod.mk.injEq] at h; obtain ⟨rfl, -⟩ := h
refine noEmpty_splice hr _ _ _ fun k hk => ?_
simp only [List.mem_cons, List.not_mem_nil, or_false] at hk
rcases hk with hk | hk
· exact hr k (hk ▸ getD_mem_of_lt hc.2)
· exact hr k (hk ▸ getD_mem_of_lt (by omega))
· simp at h
| tuck d =>
simp only [runStepB] at h
split at h
· simp only [Option.some.injEq, Prod.mk.injEq] at h; obtain ⟨rfl, -⟩ := h
refine noEmpty_splice hr _ _ _ fun k hk => ?_
cases d <;> simp [tuckRow] at hk <;> omega
· simp at h
| merge a =>
simp only [runStepB] at h
split at h
· split at h
· rename_i k l hk hl
simp only [Option.some.injEq, Prod.mk.injEq] at h; obtain ⟨rfl, -⟩ := h
refine noEmpty_splice hr _ _ _ fun m hm => ?_
simp at hm; have := stackAt_pos hk; omega
· simp at h
· simp at h
| knit d bd =>
simp only [runStepB] at h
split at h
· split at h
· simp only [Option.some.injEq, Prod.mk.injEq] at h; obtain ⟨rfl, -⟩ := h
refine noEmpty_splice hr _ _ _ fun k hk => ?_
cases d <;> simp [tuckRow] at hk <;> omega
· simp at h
· simp at h
| split d bd =>
simp only [runStepB] at h
split at h
· split at h
· rename_i k hk
simp only [Option.some.injEq, Prod.mk.injEq] at h; obtain ⟨rfl, -⟩ := h
refine noEmpty_splice hr _ _ _ fun m hm => ?_
have := stackAt_pos hk
cases d <;> cases bd <;> simp [splitRow] at hm <;> omega
· simp at h
· simp at h
| drop a =>
simp only [runStepB] at h
split at h
· split at h
· simp only [Option.some.injEq, Prod.mk.injEq] at h; obtain ⟨rfl, -⟩ := h
exact fun k hk => hr k (List.mem_of_mem_eraseIdx hk)
· simp at h
· simp at h def FragmentReadFor (S : Nat) (P : Step → Prop) : Prop :=
∀ (win : Window) (c c1 : Comp) (st : State) (s : Step) (r' : Row) (M : BWord Label), P s →
NoEmpty c.row → Laid c st → c.at_ = canonical S c.row.length → Fits win c → runStepB c.row s = some (r', M) →
compileStep c s = some c1 → c.ops <+: c1.ops → (∀ op ∈ c1.ops, NamedIn3 win op) →
sim win st (c1.ops.drop c.ops.length) = [s]
def ArrangeRead : Prop :=
∀ (win : Window) (c c' : Comp) (target : List Int) (st : State), Laid c st → Fits win c →
arrange c target = some c' → c.ops <+: c'.ops → (∀ op ∈ c'.ops, NamedIn3 win op) →
sim win st (c'.ops.drop c.ops.length) = []theorem drop_prefix_append {α : Type} {a b : List α} (h : a <+: b) (q : List α) (hq : b <+: q) :
q.drop a.length = b.drop a.length ++ q.drop b.lengthProof
:= by
obtain ⟨t, rfl⟩ := h
obtain ⟨u, rfl⟩ := hq
simp [List.append_assoc] theorem sim_compileSteps {S : Nat} (hA : ArrangeFree) (hT : ArrangeTotal S) (hS : StepFragment S)
(hC : CompileTotal S) (hAR : ArrangeRead) (hSR : FragmentReadFor S (fun _ => True)) :
∀ (w : List Step) (c : Comp) (r' : Row) (M : BWord Label), c.row.count .yarn = 1 → NoEmpty c.row →
c.at_ = canonical S c.row.length → DB c.row w = some (r', M) →
∀ c2, compileSteps S c w = some c2 →
∀ win st, Laid c st → Fits win c → (∀ op ∈ c2.ops, NamedIn3 win op) →
sim win st (c2.ops.drop c.ops.length) = wProof
| [], c, r', M, _, _, _, _, c2, hc2, win, st, _, _, _ => by
simp only [compileSteps, Option.some.injEq] at hc2
subst hc2; simp [sim]
| s :: ss, c, r', M, hy, hne, hcan, hDB, c2, hc2, win, st, hL, hF, hN => by
simp only [DB] at hDB
cases hrs : runStepB c.row s with
| none => simp [hrs] at hDB
| some p =>
obtain ⟨r1, M1⟩ := p
simp only [hrs] at hDB
cases hrest : DB r1 ss with
| none => simp [hrest] at hDB
| some q =>
obtain ⟨r2, M2⟩ := q
obtain ⟨c1, hc1, hr1, hy1, hp1, hl1⟩ := hC c s r1 M1 hy hcan hrs
obtain ⟨⟨c1', ha1, hr1', hat1, hp1'⟩, _⟩ := hT c1 (by rw [hr1]; exact hy1) hl1
have hcs : compileSteps S c1' ss = some c2 := by
simp only [compileSteps, hc1, ha1] at hc2; exact hc2
obtain ⟨c2', hc2', _, _, hp2, _, hcond⟩ :=
compileSteps_spec hA hT hS hC ss c1' r2 M2 (by rw [hr1', hr1]; exact hy1)
(by rw [hat1, hr1']) (by rw [hr1', hr1]; exact hrest)
rw [hcs, Option.some.injEq] at hc2'
subst hc2'
have hN1' : ∀ op ∈ c1'.ops, NamedIn3 win op := fun op ho => hN op (hp2.subset ho)
have hN1 : ∀ op ∈ c1.ops, NamedIn3 win op := fun op ho => hN1' op (hp1'.subset ho)
obtain ⟨hF1, st1, hd1, hL1⟩ := hS win c c1 st s r1 M1 trivial hL hcan hF hrs hc1 hp1 hN1
obtain ⟨hF1', st1', hd1', hL1'⟩ := hA win c1 c1' _ st1 hL1 hF1 ha1 hp1' hN1'
have e1 := hSR win c c1 st s r1 M1 trivial hne hL hcan hF hrs hc1 hp1 hN1
have e2 := hAR win c1 c1' _ st1 hL1 hF1 ha1 hp1' hN1'
have e3 := sim_compileSteps hA hT hS hC hAR hSR ss c1' r2 M2 (by rw [hr1', hr1]; exact hy1)
(by rw [hr1', hr1]; exact noEmpty_runStepB hne hrs)
(by rw [hat1, hr1']) (by rw [hr1', hr1]; exact hrest) c2 hcs win st1' hL1' hF1' hN
rw [drop_prefix_append hp1 _ (hp1'.trans hp2), drop_prefix_append hp1' _ hp2,
sim_append win st st1 _ _ (runSteps_of_denote hd1), sim_append win st1 st1' _ _ (runSteps_of_denote hd1'),
e1, e2, e3]
rfl The canonical layout, read
In the canonical layout with spacing 2 (canonical 2), object j of the row is at position 3 + 2j. No other
position holds an object.
| Theorem | Content |
|---|---|
seg_canon_at |
The reader sees object j at position 3 + 2j. |
seg_canon_off |
The reader sees nothing at the other positions. |
rowOf_canon_upto |
From a left edge at most 3 to position 3 + 2m, the stack row is the first m objects. |
objsBefore_canon |
m objects are left of position 3 + 2m. |
objsBefore_canon_between |
The objects left of a position between object m - 1 and object m |
namespace FormalizeKnitting.Bridge.RM
open FormalizeKnitting.Knitout FormalizeKnitting.Knitout.MR FormalizeKnitting.Bridge.MR Frag
section Canon
variable {c : Comp} {st : State}
theorem seg_canon_at (hL : Laid c st) (hc : c.at_ = canonical 2 c.row.length) (hne : NoEmpty c.row) {j : Nat}
(hj : j < c.row.length) : seg st (3 + 2 * (j : Int)) = [c.row.getD j .yarn]Proof
:= by
have hb : nB st (3 + 2 * (j : Int)) = 0 := by simp [nB, hL.2.2.2.1]
have hf := laid_front_at hL hc j
simp only [seg, hb, stkO, ite_true, List.append_nil]
rw [laid_carrier hL hc]
by_cases hy : j = yarnIdx c.row
· subst hy
rw [getD_yarnIdx _ hL.1] at hf ⊢
simp only [nF, hf, stackSize, ite_true, List.append_nil]
· rw [ite_eq_right (by omega)]
cases h : c.row.getD j .yarn with
| yarn => exact absurd ((getD_eq_yarn_iff hL.1 hj).mp h) hy
| stack k =>
have hk : 0 < k := hne k (h ▸ getD_mem_of_lt hj)
rw [h] at hf
simp only [nF, hf, stackSize, List.nil_append]
rw [ite_eq_right (by omega)] theorem seg_canon_off (hL : Laid c st) (hc : c.at_ = canonical 2 c.row.length) (i : Int)
(hi : ∀ j : Nat, j < c.row.length → i ≠ 3 + 2 * (j : Int)) : seg st i = []Proof
:= by
have hb : nB st i = 0 := by simp [nB, hL.2.2.2.1]
have hy : st.carrier ≠ i := by
rw [laid_carrier hL hc]; exact fun h => hi _ (yarnIdx_lt hL.1) h.symm
have hf : nF st i = 0 := by
unfold nF
by_cases h : ∃ j : Nat, i = 3 + 2 * (j : Int)
· obtain ⟨j, rfl⟩ := h
have hj : c.row.length ≤ j := by
by_contra h'; exact hi j (by omega) rfl
rw [laid_front_at hL hc, stackSize_of_ge hj]
· simp only [not_exists] at h; rw [laid_front_off hL hc i h]; rfl
simp [seg, hb, hf, hy, stkO] theorem rowOf_canon_upto (hL : Laid c st) (hc : c.at_ = canonical 2 c.row.length) (hne : NoEmpty c.row)
(lo : Int) (hlo : lo ≤ 3) : ∀ m : Nat, m ≤ c.row.length →
rowOf st lo (3 + 2 * (m : Int) - lo).toNat = c.row.take mProof
| 0, _ => by
rw [List.take_zero]
unfold rowOf
rw [List.flatMap_eq_nil_iff]
intro k hk
simp at hk
obtain ⟨a, ha, rfl⟩ := hk
exact seg_canon_off hL hc _ (fun j _ => by omega)
| m + 1, hm => by
have ih := rowOf_canon_upto hL hc hne lo hlo m (by omega)
rw [show (3 + 2 * ((m + 1 : Nat) : Int) - lo).toNat = (3 + 2 * (m : Int) - lo).toNat + 1 + 1 by push_cast; omega,
rowOf_succ, rowOf_succ, ih]
have e0 : lo + (((3 + 2 * (m : Int) - lo).toNat : Nat) : Int) = 3 + 2 * (m : Int) := by omega
have e1 : lo + (((3 + 2 * (m : Int) - lo).toNat + 1 : Nat) : Int) = 3 + 2 * (m : Int) + 1 := by push_cast; omega
rw [e0, e1, seg_canon_at hL hc hne (by omega), seg_canon_off hL hc _ (fun j _ => by omega), List.append_nil,
List.take_add_one, List.getElem?_eq_getElem (show m < c.row.length by omega)]
simp [List.getD_eq_getElem?_getD, List.getElem?_eq_getElem (show m < c.row.length by omega)] theorem objsBefore_canon (hL : Laid c st) (hc : c.at_ = canonical 2 c.row.length) (hne : NoEmpty c.row)
(win : Window) (hlo : win.lo ≤ 3) (m : Nat) (hm : m ≤ c.row.length) :
objsBefore win st (3 + 2 * (m : Int)) = mProof
:= by
unfold objsBefore
rw [rowOf_canon_upto hL hc hne win.lo hlo m hm, List.length_take]; omega theorem objsBefore_canon_between (hL : Laid c st) (hc : c.at_ = canonical 2 c.row.length) (hne : NoEmpty c.row)
(win : Window) (hlo : win.lo ≤ 3) (m : Nat) (hm : m < c.row.length) :
objsBefore win st (3 + 2 * (m : Int) + 1) = m + 1Proof
:= by
unfold objsBefore
rw [show (3 + 2 * (m : Int) + 1 - win.lo).toNat = (3 + 2 * (m : Int) - win.lo).toNat + 1 by omega, rowOf_succ,
rowOf_canon_upto hL hc hne win.lo hlo m (by omega),
show win.lo + ((3 + 2 * (m : Int) - win.lo).toNat : Int) = 3 + 2 * (m : Int) by omega,
seg_canon_at hL hc hne hm]
simp [List.length_take]; omega The drop, tuck and knit fragments read as their steps
From the canonical layout, the compiler emits a short fragment for each shorthand step. Read back, the fragment is the step.
| Theorem | Statement |
|---|---|
drop_read |
FragmentReadFor 2 isDrop |
tuck_read |
FragmentReadFor 2 isTuck |
knit_read |
FragmentReadFor 2 isKnit |
rowOf_moved_upto: the stack row when only the carrier moved from its canonical position.
namespace FormalizeKnitting.Bridge.RM
open FormalizeKnitting.Knitout FormalizeKnitting.Knitout.MR FormalizeKnitting.Bridge.MR Frag
theorem sim_single (win : Window) (s s' : State) (op : Op) (h : step s op = .ok s') :
sim win s [op] = simOp win s opProof
:= by
simp [sim, h] theorem drop_read : FragmentReadFor 2 isDropProof
:= by
intro win c c1 st s r' M hs hne hL hc hF hrs hcs hpre hN
cases s with
| x _ _ => exact hs.elim
| tuck _ => exact hs.elim
| merge _ => exact hs.elim
| knit _ _ => exact hs.elim
| split _ _ => exact hs.elim
| drop a =>
have hy := hL.1
have hym := yarn_mem hy
set y := yarnIdx c.row with hydef
cases hk : stackAt c.row a with
| none => simp [runStepB, hym, hk] at hrs
| some k =>
obtain ⟨ha, hga, hk0⟩ := Frag.stackAt_some hk
have hpa : c.at_.getD a 0 = 3 + 2 * (a : Int) := by rw [hc]; exact canonical_getD_two ha
simp only [compileStep, hym, ite_true, hk, Option.some.injEq, hpa] at hcs
subst hcs
set p : Int := 3 + 2 * (a : Int) with hpdef
have hay : a ≠ y := fun h => by rw [h, getD_yarnIdx _ hy] at hga; exact absurd hga (by simp)
have hlo := fits_lo hF hc (by omega)
have hcar : st.carrier = 3 + 2 * (y : Int) := laid_carrier hL hc
have hlen : (st.stk ⟨.f, p⟩).length = k := by rw [laid_front_at hL hc, hga]; rfl
have hstep : step st (.drop (fN p)) = .ok (apply st (.drop (fN p))) := by
have hne' : (st.stk ⟨.f, p⟩).length ≠ 0 := by omega
simp [step, refusal, linRefusal, extraRefusal, erase, fN, hne']
rw [List.drop_left, sim_single win st _ _ hstep]
have hob : objsBefore win st p = a := objsBefore_canon hL hc hne win (by omega) a (by omega)
have hny : nY st p = 0 := by simp only [nY, hcar]; rw [ite_eq_right (by omega)]
simp [simOp, slot, fN, hob, hny] theorem tuck_read : FragmentReadFor 2 isTuckProof
:= by
intro win c c1 st s r' M hs hne hL hc hF hrs hcs hpre hN
cases s with
| x _ _ => exact hs.elim
| merge _ => exact hs.elim
| knit _ _ => exact hs.elim
| split _ _ => exact hs.elim
| drop _ => exact hs.elim
| tuck d =>
have hy := hL.1
have hym := yarn_mem hy
have hyl := yarnIdx_lt hy
set y := yarnIdx c.row with hydef
have hpy : c.at_.getD y 0 = 3 + 2 * (y : Int) := by rw [hc]; exact canonical_getD_two hyl
have hlo := fits_lo hF hc (by omega)
have hcar : st.carrier = 3 + 2 * (y : Int) := laid_carrier hL hc
have hob : objsBefore win st (3 + 2 * (y : Int)) = y := objsBefore_canon hL hc hne win (by omega) y (by omega)
have hfy : st.stk ⟨.f, 3 + 2 * (y : Int)⟩ = [] := laid_front_yarn hL hc
have hb : ∀ j, st.stk ⟨.b, j⟩ = [] := hL.2.2.2.1
have hr : st.rack = 0 := hL.2.2.1
cases d with
| R =>
simp only [compileStep, hym, ite_true, ← hydef, hpy, Option.some.injEq] at hcs
subst hcs
have hstep : step st (.tuck .R (fN (3 + 2 * (y : Int)))) = .ok (apply st (.tuck .R (fN (3 + 2 * (y : Int))))) := by
simp [step, refusal, linRefusal, extraRefusal, erase, fN, slot, arrivesFrom, hr, hcar]
rw [List.drop_left, sim_single win st _ _ hstep]
simp [simOp, slot, fN, hr, nF, nB, hfy, hb, ob, xs, ms]
| L =>
simp only [compileStep, hym, ite_true, ← hydef, hpy, Option.some.injEq] at hcs
subst hcs
have hoff : st.stk ⟨.f, 3 + 2 * (y : Int) - 1⟩ = [] := laid_front_off hL hc _ (fun j => by omega)
have hstep : step st (.tuck .L (fN (3 + 2 * (y : Int) - 1))) =
.ok (apply st (.tuck .L (fN (3 + 2 * (y : Int) - 1)))) := by
simp [step, refusal, linRefusal, extraRefusal, erase, fN, slot, arrivesFrom, hr, hcar]
rw [List.drop_left, sim_single win st _ _ hstep]
have harr : arrivesFrom .L (3 + 2 * (y : Int) - 1) = 3 + 2 * (y : Int) := by simp [arrivesFrom]
simp [simOp, slot, fN, hr, harr, hob, nF, nB, hoff, hb, ob, xs, ms] theorem sim_cons (win : Window) (s s' : State) (op : Op) (ops : List Op) (h : step s op = .ok s') :
sim win s (op :: ops) = simOp win s op ++ sim win s' opsProof
:= by
simp [sim, h] theorem seg_carrier (s : State) (z p : Int) (h1 : s.carrier ≠ p) (h2 : z ≠ p) :
seg { s with carrierProof
:= z } p = seg s p := by
simp [seg, nF, nB, h1, h2] theorem rowOf_moved_upto {c : Comp} {st : State} (hL : Laid c st) (hc : c.at_ = canonical 2 c.row.length)
(hne : NoEmpty c.row) (lo : Int) (hlo : lo ≤ 3) (z : Int) (m : Nat) (hm : m ≤ c.row.length)
(hz : z ≥ 3 + 2 * (m : Int)) (hym : yarnIdx c.row ≥ m) :
rowOf { st with carrierProof
:= z } lo (3 + 2 * (m : Int) - lo).toNat = c.row.take m := by
rw [← rowOf_canon_upto hL hc hne lo hlo m hm]
apply rowOf_congr
intro k hk
apply seg_carrier
· rw [laid_carrier hL hc]; omega
· omega theorem objsBefore_congr (win : Window) (s t : State) (i : Int)
(h : ∀ p : Int, win.lo ≤ p → p < i → seg s p = seg t p) : objsBefore win s i = objsBefore win t iProof
:= by
unfold objsBefore
rw [rowOf_congr (fun k hk => h _ (by omega) (by omega))] theorem knit_read : FragmentReadFor 2 isKnitProof
:= by
intro win c c1 st s r' M hs hne hL hc hF hrs hcs hpre hN
cases s with
| x _ _ => exact hs.elim
| drop _ => exact hs.elim
| merge _ => exact hs.elim
| tuck _ => exact hs.elim
| split _ _ => exact hs.elim
| knit d bd =>
have hy := hL.1
have hym := yarn_mem hy
have hyl := yarnIdx_lt hy
set y := yarnIdx c.row with hydef
have hlo := fits_lo hF hc (by omega)
have hcar : st.carrier = 3 + 2 * (y : Int) := laid_carrier hL hc
have hr : st.rack = 0 := hL.2.2.1
have hb : ∀ j, st.stk ⟨.b, j⟩ = [] := hL.2.2.2.1
have hys : c.at_.getD (yarnIdx c.row) 0 = 3 + 2 * (y : Int) := by rw [hc]; exact canonical_getD_two hyl
cases hk : stackAt c.row (besideIdx c.row d) with
| none => simp [runStepB, hk] at hrs
| some k =>
obtain ⟨hbl, hga, hk0⟩ := Frag.stackAt_some hk
have hbyl : besideIdx c.row d ≠ y := by
intro h; rw [h, getD_yarnIdx _ hy] at hga; exact absurd hga (by simp)
have hhb : hasBeside c.row d = true := by
cases d <;> simp [hasBeside, besideIdx] at hbl hbyl ⊢ <;> omega
have hbc : c.at_.getD (besideIdx c.row d) 0 = 3 + 2 * ((besideIdx c.row d : Nat) : Int) := by
rw [hc]; exact canonical_getD_two hbl
simp only [compileStep, hym, hhb, and_self, ite_true, hk, Option.some.injEq, hbc, hys] at hcs
subst hcs
simp only [emit, List.drop_left]
have hfront := laid_front_at hL hc
have hoff := laid_front_off hL hc
have hfy : st.stk ⟨.f, 3 + 2 * (y : Int)⟩ = [] := laid_front_yarn hL hc
cases d with
| R =>
have e1 : besideIdx c.row .R = y + 1 := rfl
simp only [e1] at hga hbl ⊢
set cc : Int := 3 + 2 * (y : Int) with hccdef
have ec1 : 3 + 2 * ((y + 1 : Nat) : Int) = cc + 2 := by push_cast; omega
simp only [ec1, ite_true]
have hkk : (st.stk ⟨.f, cc + 2⟩).length = k := by rw [← ec1, hfront, hga]; rfl
have hyt : yarnTo cc (cc + 2) = [.miss .R (fN cc), .miss .R (fN (cc + 1))] := by
unfold yarnTo; rw [ite_eq_left (by omega), show (cc + 2 - cc).toNat = 2 by omega, Frag.bind_coe_map]
simp [List.range_succ]
have hf1 : st.stk ⟨.f, cc + 1⟩ = [] := hoff _ (fun j => by omega)
obtain ⟨hs1, x1⟩ := sim_miss_R win st cc hr hcar hfy (hb cc)
obtain ⟨hs2', x2⟩ := sim_miss_R win { st with carrier := cc + 1 } (cc + 1) hr rfl hf1 (hb _)
have hs2 : step { st with carrier := cc + 1 } (.miss .R (fN (cc + 1))) = .ok { st with carrier := cc + 2 } := by
rw [hs2']; congr 1
have hob : objsBefore win { st with carrier := cc + 2 } (cc + 2) = y := by
unfold objsBefore
rw [show (cc + 2 - win.lo).toNat = (3 + 2 * (y : Int) - win.lo).toNat + 1 + 1 by omega, rowOf_succ, rowOf_succ,
rowOf_moved_upto hL hc hne win.lo (by omega) (cc + 2) y (by omega) (by omega) (by omega)]
have s0 : seg { st with carrier := cc + 2 } (win.lo + ((3 + 2 * (y : Int) - win.lo).toNat : Int)) = [] := by
rw [show win.lo + ((3 + 2 * (y : Int) - win.lo).toNat : Int) = cc by omega]
simp [seg, nF, nB, hfy, hb, stkO]
have s1 : seg { st with carrier := cc + 2 } (win.lo + (((3 + 2 * (y : Int) - win.lo).toNat + 1 : Nat) : Int)) = [] := by
rw [show win.lo + (((3 + 2 * (y : Int) - win.lo).toNat + 1 : Nat) : Int) = cc + 1 by push_cast; omega]
simp [seg, nF, nB, hf1, hb, stkO]
rw [s0, s1]; simp; omega
rw [hyt]
have hcarry : arrivesFrom .R (cc + 2) = cc + 2 := rfl
cases bd with
| f =>
have hst : step { st with carrier := cc + 2 } (.knit .R ⟨.f, cc + 2⟩) =
.ok (apply { st with carrier := cc + 2 } (.knit .R ⟨.f, cc + 2⟩)) := by
have hk' : (st.stk ⟨.f, cc + 2⟩).length ≠ 0 := by omega
simp [step, refusal, linRefusal, extraRefusal, erase, slot, arrivesFrom, hr, hk']
simp only [knitOps, show (Bed.f = Bed.b) = False by simp, ite_false, List.nil_append, List.append_nil,
List.cons_append]
rw [sim_cons win st _ _ _ hs1, sim_cons win _ _ _ _ hs2, sim_cons win _ _ _ _ hst, x1, x2]
simp [simOp, slot, hr, hcarry, nB, hb, ob, xs, sim]
| b =>
set t := { st with carrier := cc + 2 } with htdef
have a1 : aligned t.rack (fN (cc + 2)) (bN (cc + 2)) = true := by simp [htdef, aligned, slot, hr, fN, bN]
set t2 := apply t (.xfer (fN (cc + 2)) (bN (cc + 2))) with ht2
have hst : step t2 (.knit .R ⟨.b, cc + 2⟩) = .ok (apply t2 (.knit .R ⟨.b, cc + 2⟩)) := by
have hk' : (st.stk ⟨.f, cc + 2⟩).length ≠ 0 := by omega
simp [ht2, htdef, step, refusal, linRefusal, extraRefusal, erase, apply, upd_apply, slot, arrivesFrom, hr,
xferStack, hb, hk', fN, bN]
set t3 := apply t2 (.knit .R ⟨.b, cc + 2⟩) with ht3
have a3 : aligned t3.rack (bN (cc + 2)) (fN (cc + 2)) = true := by
simp [ht3, ht2, htdef, apply, aligned, slot, hr, fN, bN]
have hob2 : objsBefore win t2 (cc + 2) = y := by
rw [objsBefore_congr win t2 t (cc + 2) (fun p _ hp => by
have n1 : (⟨.f, p⟩ : Needle) ≠ ⟨.f, cc + 2⟩ := by simp; omega
have n2 : (⟨.f, p⟩ : Needle) ≠ ⟨.b, cc + 2⟩ := by simp
have n3 : (⟨.b, p⟩ : Needle) ≠ ⟨.b, cc + 2⟩ := by simp; omega
have n4 : (⟨.b, p⟩ : Needle) ≠ ⟨.f, cc + 2⟩ := by simp
simp only [seg, nF, nB, ht2, apply, upd_apply, htdef, hr, sub_zero, fN, bN, ite_eq_right n1, ite_eq_right n2, ite_eq_right n3,
ite_eq_right n4]), hob]
simp only [knitOps, ite_true, List.cons_append, List.nil_append]
rw [sim_cons win st _ _ _ hs1, sim_cons win _ _ _ _ hs2, sim_cons win t _ _ _ (step_xfer_of_aligned t _ _ a1),
sim_cons win t2 _ _ _ hst, sim_cons win t3 _ _ _ (step_xfer_of_aligned t3 _ _ a3), x1, x2]
have xa : simOp win t (.xfer (fN (cc + 2)) (bN (cc + 2))) = [] := simOp_xfer_empty win t _ _ (by simp [htdef, hb, bN])
have xb : simOp win t3 (.xfer (bN (cc + 2)) (fN (cc + 2))) = [] := simOp_xfer_empty win t3 _ _ (by
simp [ht3, ht2, htdef, apply, fN, bN])
have xk : simOp win t2 (.knit .R ⟨.b, cc + 2⟩) = [.knit .R .b] := by
simp [simOp, slot, ht2, htdef, apply, hr, hcarry, nF, ob, xs, fN, bN]
rw [xa, xb, xk]; simp [sim]
| L =>
have e1 : besideIdx c.row .L = y - 1 := rfl
have hy1 : 1 ≤ y := by
by_contra h0
exact hbyl (by rw [e1]; omega)
simp only [e1] at hga hbl ⊢
set cc : Int := 3 + 2 * (y : Int) with hccdef
have ec1 : 3 + 2 * ((y - 1 : Nat) : Int) = cc - 2 := by push_cast [hy1]; omega
simp only [ec1, show (Dir.L = Dir.R) = False by simp, ite_false]
have hkk : (st.stk ⟨.f, cc - 2⟩).length = k := by rw [← ec1, hfront, hga]; rfl
have hyt : yarnTo cc (cc - 2 + 1) = [.miss .L (fN (cc - 1))] := by
unfold yarnTo; rw [ite_eq_right (by omega), show (cc - (cc - 2 + 1)).toNat = 1 by omega, Frag.bind_coe_map]
simp
have hf1 : st.stk ⟨.f, cc - 1⟩ = [] := hoff _ (fun j => by omega)
obtain ⟨hs1', x1⟩ := sim_miss_L win st (cc - 1) hr (by rw [hcar]; omega) hf1 (hb _)
set t := { st with carrier := cc - 1 } with htdef
have hs1 : step st (.miss .L (fN (cc - 1))) = .ok t := hs1'
rw [hyt]
have hcarry : arrivesFrom .L (cc - 2) = cc - 1 := by simp [arrivesFrom]; omega
have hcc : cc - 1 = cc - 2 + 1 := by omega
cases bd with
| f =>
have hst : step t (.knit .L ⟨.f, cc - 2⟩) = .ok (apply t (.knit .L ⟨.f, cc - 2⟩)) := by
have hk' : (st.stk ⟨.f, cc - 2⟩).length ≠ 0 := by omega
simp [htdef, step, refusal, linRefusal, extraRefusal, erase, slot, arrivesFrom, hr, hk', hcc]
simp only [knitOps, show (Bed.f = Bed.b) = False by simp, ite_false, List.nil_append, List.append_nil,
List.cons_append]
rw [sim_cons win st _ _ _ hs1, sim_cons win _ _ _ _ hst, x1]
simp [simOp, slot, htdef, hr, nB, hb, ob, xs, sim]
| b =>
have a1 : aligned t.rack (fN (cc - 2)) (bN (cc - 2)) = true := by simp [htdef, aligned, slot, hr, fN, bN]
set t2 := apply t (.xfer (fN (cc - 2)) (bN (cc - 2))) with ht2
have hst : step t2 (.knit .L ⟨.b, cc - 2⟩) = .ok (apply t2 (.knit .L ⟨.b, cc - 2⟩)) := by
have hk' : (st.stk ⟨.f, cc - 2⟩).length ≠ 0 := by omega
simp [ht2, htdef, step, refusal, linRefusal, extraRefusal, erase, apply, upd_apply, slot, arrivesFrom, hr,
xferStack, hb, hk', fN, bN, hcc]
set t3 := apply t2 (.knit .L ⟨.b, cc - 2⟩) with ht3
have a3 : aligned t3.rack (bN (cc - 2)) (fN (cc - 2)) = true := by
simp [ht3, ht2, htdef, apply, aligned, slot, hr, fN, bN]
simp only [knitOps, ite_true, List.cons_append, List.nil_append]
rw [sim_cons win st _ _ _ hs1, sim_cons win t _ _ _ (step_xfer_of_aligned t _ _ a1),
sim_cons win t2 _ _ _ hst, sim_cons win t3 _ _ _ (step_xfer_of_aligned t3 _ _ a3), x1]
have xa : simOp win t (.xfer (fN (cc - 2)) (bN (cc - 2))) = [] :=
simOp_xfer_empty win t _ _ (by simp [htdef, hb, bN])
have xb : simOp win t3 (.xfer (bN (cc - 2)) (fN (cc - 2))) = [] := simOp_xfer_empty win t3 _ _ (by
simp [ht3, ht2, htdef, apply, fN, bN])
have xk : simOp win t2 (.knit .L ⟨.b, cc - 2⟩) = [.knit .L .b] := by
simp [simOp, slot, ht2, htdef, apply, hr, nF, ob, xs, fN, bN]
rw [xa, xb, xk]; simp [sim] The merge fragment reads as the merge
merge_read : FragmentReadFor 2 isMerge.
- The compiler moves stack
a + 1one needle left. This is a free move. It reads as nothing. - Then the compiler moves the stack one more needle left, onto stack
a. - In that second move, only the transfer onto the loaded needle reads as a step: one
merge a.
namespace FormalizeKnitting.Bridge.RM
open FormalizeKnitting.Knitout FormalizeKnitting.Knitout.MR FormalizeKnitting.Bridge.MR Frag
theorem merge_read : FragmentReadFor 2 isMergeProof
:= by
intro win c c1 st s r' M hs hne hL0 hc hF hrs hcs hpre hN
have hL := hL0
cases s with
| x _ _ | tuck _ | knit _ _ | split _ _ | drop _ => exact absurd hs id
| merge a =>
obtain ⟨hy, hlen, hr, hb, hfr, hcar, hsort⟩ := hL
have hyin : Obj.yarn ∈ c.row := List.count_pos_iff.mp (by omega)
cases hka : stackAt c.row a with
| none => simp [runStepB, hyin, hka] at hrs
| some k =>
cases hlb : stackAt c.row (a + 1) with
| none => simp [runStepB, hyin, hka, hlb] at hrs
| some l =>
simp only [runStepB, hyin, ite_true, hka, hlb, Option.some.injEq, Prod.mk.injEq] at hrs
obtain ⟨rfl, rfl⟩ := hrs
obtain ⟨ha, hga⟩ := stackAt_getD hka
obtain ⟨hb1, hgb⟩ := stackAt_getD hlb
set n := c.row.length with hn
have hpa : c.at_.getD a 0 = 3 + 2 * (a : Int) := by rw [hc]; exact canonical_getD_two ha
have hpb : c.at_.getD (a + 1) 0 = 3 + 2 * ((a + 1 : Nat) : Int) := by rw [hc]; exact canonical_getD_two hb1
set p : Int := 3 + 2 * (a : Int) with hp
have hmove : (moveObject c (a + 1) (p + 1)).ops = c.ops ++ loopStep (p + 2) false := by
simp only [moveObject, hgb, hpb]
rw [ite_eq_right (by simp), ite_eq_right (by push_cast; omega)]
have : (3 + 2 * ((a + 1 : Nat) : Int) - (p + 1)).toNat = 1 := by push_cast; omega
rw [this]
have e2 : 3 + 2 * ((a + 1 : Nat) : Int) - ((0 : Nat) : Int) = p + 2 := by push_cast; omega
simp only [List.range_one]
simp only [List.bind_eq_flatMap, List.pure_def, List.flatMap_cons, List.flatMap_nil, List.append_nil, e2]
simp only [compileStep, hyin, ite_true, hka, hlb, Option.some.injEq, hpa] at hcs
subst hcs
simp only [emit, hmove] at hpre hN ⊢
have hN1 : NamedIn3 win (.xfer (fN (p + 2)) (bN (p + 2))) := hN _ (by simp [loopStep])
have hN2 : NamedIn3 win (.xfer (bN (p + 1)) (fN (p + 1 - 1))) := hN _ (by simp [loopStep])
have i1 := hN1 (p + 2) (by simp [Op.named, fN, bN])
have i2 := hN2 (p + 1) (by simp [Op.named, fN, bN])
have i3 := hN2 (p + 1 - 1) (by simp [Op.named, fN, bN])
have hyl := yarnIdx_spec hy
set y := yarnIdx c.row with hydef
have hya : y ≠ a := fun e => by rw [e, hga] at hyl; exact absurd hyl.2 (by simp)
have hyb : y ≠ a + 1 := fun e => by rw [e, hgb] at hyl; exact absurd hyl.2 (by simp)
have hcy : st.carrier = 3 + 2 * (y : Int) := by rw [hcar, hc]; exact canonical_getD_two hyl.1
have hcan : c.at_ = c.at_.take a ++ [p, p + 2] ++ c.at_.drop (a + 2) := by
have := cut_two c.at_ (a := a) (by rw [hc, canonical_length]; omega) 0
have e3 : 3 + 2 * ((a + 1 : Nat) : Int) = p + 2 := by push_cast; omega
rw [hpa, hpb, e3] at this; exact this
have hrow := cut_two c.row (a := a) (by omega) .yarn
rw [hga, hgb] at hrow
have hloads : ∀ i, (st.stk ⟨.f, i⟩).length = loadAt (c.row.take a) (c.at_.take a) i +
((if p = i then k else 0) + (if p + 2 = i then l else 0)) + loadAt (c.row.drop (a + 2)) (c.at_.drop (a + 2)) i := by
intro i
rw [hfr i]
conv_lhs => rw [hrow, hcan]
rw [loadAt_append (by simp [hlen]; omega), loadAt_append (by simp [hlen]; omega), loadAt_two]
simp [stackSize]
have hA : ∀ x ∈ c.at_.take a, x + 2 ≤ p := fun x hx => by rw [hc] at hx; exact mem_canonical_take hx
have hC : ∀ x ∈ c.at_.drop (a + 2), p + 4 ≤ x := fun x hx => by
rw [hc] at hx; have := mem_canonical_drop hx; push_cast at this; omega
have hp0 : st.stk ⟨.f, p + 1⟩ = [] := by
have := hloads (p + 1)
rw [loadAt_not_mem _ _ _ (fun h => by have := hA _ h; omega),
loadAt_not_mem _ _ _ (fun h => by have := hC _ h; omega)] at this
simp at this; exact List.length_eq_zero_iff.mp (by simpa using this)
have hp0' : st.stk ⟨.f, p + 2 - 1⟩ = [] := by rw [show p + 2 - 1 = p + 1 by omega]; exact hp0
obtain ⟨st1, hd1, hr1, hc1, hk1⟩ := stack_left_free win st (p + 2) hr hb (by rw [hcy]; omega)
hp0' (inside_of (by omega) (by omega)) (inside_of (by omega) (by omega))
have hrun1 := runSteps_of_denote hd1
have hread1 : sim win st (loopStep (p + 2) false) = [] :=
sim_loopStep_left win st (p + 2) hr hb (by rw [hcy]; omega) hp0'
rw [List.append_assoc, List.drop_left, sim_append win st st1 _ _ hrun1, hread1, List.nil_append]
have hpy : st.carrier ≠ p := by rw [hcy]; omega
have hp1y : st.carrier ≠ p + 1 := by rw [hcy]; omega
have hst1f : ∀ q, st1.stk q = if q = ⟨.f, p + 2 - 1⟩ then st.stk ⟨.f, p + 2⟩ else if q = ⟨.f, p + 2⟩ then [] else st.stk q :=
hk1
have hlenk : (st.stk ⟨.f, p⟩).length = k := by have := hloads p; rw [loadAt_not_mem _ _ _ (fun h => by have := hA _ h; omega),
loadAt_not_mem _ _ _ (fun h => by have := hC _ h; omega)] at this; simpa using this
have hlenl : (st.stk ⟨.f, p + 2⟩).length = l := by have := hloads (p + 2); rw [loadAt_not_mem _ _ _ (fun h => by have := hA _ h; omega),
loadAt_not_mem _ _ _ (fun h => by have := hC _ h; omega)] at this; simpa using this
have hk0 : 0 < k := stackAt_pos hka
have hl0 : 0 < l := stackAt_pos hlb
have e21 : p + 2 - 1 = p + 1 := by omega
rw [e21] at hst1f
have a1 : aligned st1.rack (fN (p + 1)) (bN (p + 1)) = true := by simp [aligned, slot, fN, bN, hr1]
set s2 := apply st1 (.xfer (fN (p + 1)) (bN (p + 1))) with hs2
have r2 : (-1 - s2.rack).natAbs = 1 := by simp [hs2, apply, hr1]
set s3 := apply s2 (.rack (-1)) with hs3
have a3 : aligned s3.rack (bN (p + 1)) (fN (p + 1 - 1)) = true := by
simp [hs3, hs2, apply, aligned, slot, fN, bN]
set s4 := apply s3 (.xfer (bN (p + 1)) (fN (p + 1 - 1))) with hs4
have r4 : (0 - s4.rack).natAbs = 1 := by simp [hs4, hs3, apply]
have hs3stk : ∀ q, s3.stk q = if q = bN (p + 1) then xferStack (st1.stk (fN (p + 1))) (st1.stk (bN (p + 1)))
else if q = fN (p + 1) then [] else st1.stk q := by
intro q; simp [hs3, hs2, apply, upd_apply]
have hrk3 : s3.rack = -1 := by simp [hs3, hs2, apply]
have hcar3 : s3.carrier = st.carrier := by simp [hs3, hs2, apply, hc1]
have x1 : simOp win st1 (.xfer (fN (p + 1)) (bN (p + 1))) = [] :=
simOp_xfer_empty win st1 _ _ (by rw [hst1f]; simp [bN, hb])
have x2 : simOp win s2 (.rack (-1)) = [] := by
apply rackSteps_nil
intro t _
refine ⟨fun _ => ?_, fun h => absurd (by simp [hs2, apply, hr1]) h⟩
by_cases e : win.lo + (t : Int) = p + 1
· right
simp only [nY, nF, ob, hs2, apply, upd_apply, e, hc1]
simp [hp1y, fN, bN]
· left
simp only [nB, hs2, apply, upd_apply, hr1, sub_zero]
rw [ite_eq_right (by simp [bN]; omega), ite_eq_right (by simp [fN])]
rw [hst1f]; simp [hb]
have hob : objsBefore win s3 p = a := by
rw [objsBefore_congr win s3 st p (fun q _ hq => by
have hf' : s3.stk ⟨.f, q⟩ = st.stk ⟨.f, q⟩ := by
rw [hs3stk, ite_eq_right (by simp [bN]), ite_eq_right (by simp [fN]; omega), hst1f, ite_eq_right (by simp; omega),
ite_eq_right (by simp; omega)]
have hb' : s3.stk ⟨.b, q - s3.rack⟩ = [] := by
rw [hrk3, hs3stk, ite_eq_right (by simp [bN]; omega), ite_eq_right (by simp [fN]), hst1f, ite_eq_right (by simp),
ite_eq_right (by simp), hb]
simp only [seg, nF, nB, hcar3, hf', hb', hr, sub_zero, hb]),
objsBefore_canon hL0 hc hne win (by have := fits_lo hF hc (show 0 < c.row.length by omega); omega) a (by omega)]
have x3 : simOp win s3 (.xfer (bN (p + 1)) (fN (p + 1 - 1))) = [.merge a] := by
have src : (s3.stk (bN (p + 1))).length = l := by
rw [hs3stk, ite_eq_left rfl, xferStack, List.length_append, List.length_reverse, hst1f, hst1f,
ite_eq_left (by simp [fN]), ite_eq_right (by simp [bN]), ite_eq_right (by simp [bN])]
simp [bN, hb, hlenl]
have tgt : (s3.stk (fN (p + 1 - 1))).length = k := by
rw [show p + 1 - 1 = p by omega, hs3stk, ite_eq_right (by simp [fN, bN]), ite_eq_right (by simp [fN]), hst1f,
ite_eq_right (by simp [fN]), ite_eq_right (by simp [fN])]
exact hlenk
have hz : slot s3.rack (bN (p + 1)) = p := by simp [slot, bN, hrk3]
have hny : nY s3 p = 0 := by simp [nY, hcar3, hpy]
simp only [simOp, hz, hob, hny, src, tgt]
rw [ite_eq_right (by omega)]; simp
have x4 : simOp win s4 (.rack 0) = [] := by
apply rackSteps_nil
intro t _
refine ⟨fun h => absurd h (by simp [hs4, hrk3, apply]), fun _ => Or.inl ?_⟩
have hrk4 : s4.rack = -1 := by simp [hs4, apply, hrk3]
rw [hrk4, show win.lo + (t : Int) - 1 - -1 = win.lo + t by omega]
simp only [hs4, apply, upd_apply]
by_cases e : win.lo + (t : Int) = p + 1
· rw [ite_eq_right (by simp [fN]), ite_eq_left (by simp [bN, e])]; rfl
· rw [ite_eq_right (by simp [fN]), ite_eq_right (by simp [bN]; omega), hs3stk, ite_eq_right (by simp [bN]; omega),
ite_eq_right (by simp [fN]), hst1f, ite_eq_right (by simp), ite_eq_right (by simp), hb]; rfl
simp only [loopStep, Bool.false_eq_true, ite_false, sim, step_xfer_of_aligned st1 _ _ a1, ← hs2,
step_rack_of_step s2 (-1) r2, ← hs3, step_xfer_of_aligned s3 _ _ a3, ← hs4, step_rack_of_step s4 0 r4, x1, x2,
x3, x4, List.nil_append, List.append_nil] The split fragment reads as the split
split_read : FragmentReadFor 2 isSplit.
sim_post3: the parking moves read as nothing. The parking moves arerack 1, a transfer from the back needle onto the next empty front needle,rack 0.
namespace FormalizeKnitting.Bridge.RM
open FormalizeKnitting.Knitout FormalizeKnitting.Knitout.MR FormalizeKnitting.Bridge.MR Frag
theorem sim_post3 (win : Window) (s : State) (c1 : Int) (hr : s.rack = 0)
(hb : ∀ j, j ≠ c1 → s.stk ⟨.b, j⟩ = []) (hcar : s.carrier ≠ c1 + 1) (hf1 : s.stk ⟨.f, c1 + 1⟩ = []) :
sim win s [.rack 1, .xfer (bN c1) (fN (c1 + 1)), .rack 0] = []Proof
:= by
have r1 : (1 - s.rack).natAbs = 1 := by simp [hr]
set s2 := apply s (.rack 1) with hs2
have a2 : aligned s2.rack (bN c1) (fN (c1 + 1)) = true := by simp [hs2, apply, aligned, slot, fN, bN]
set s3 := apply s2 (.xfer (bN c1) (fN (c1 + 1))) with hs3
have r3 : (0 - s3.rack).natAbs = 1 := by simp [hs3, hs2, apply]
have x2 : simOp win s (.rack 1) = [] := by
apply rackSteps_nil
intro k _
refine ⟨fun h => absurd h (by rw [hr]; omega), fun _ => ?_⟩
by_cases e : win.lo + (k : Int) - 1 - s.rack = c1
· right
have ek : win.lo + (k : Int) = c1 + 1 := by rw [hr] at e; omega
simp [nY, nF, ob, ek, hcar, hf1]
· left
rw [hb _ e]; rfl
have x3 : simOp win s2 (.xfer (bN c1) (fN (c1 + 1))) = [] := by
apply simOp_xfer_empty
simp [hs2, apply, fN, hf1]
have x4 : simOp win s3 (.rack 0) = [] := by
apply rackSteps_nil
intro k _
refine ⟨fun _ => Or.inl ?_, fun h => absurd (by simp [hs3, hs2, apply]) h⟩
have e3 : s3.rack = 1 := rfl
simp only [nB, e3]
simp only [hs3, hs2, apply, upd_apply]
rw [ite_eq_right (by simp [fN])]
by_cases e : win.lo + (k : Int) - 1 = c1
· rw [ite_eq_left (by simp [bN, e])]; rfl
· rw [ite_eq_right (by simp [bN]; omega), hb _ e]; rfl
simp only [sim, step_rack_of_step s 1 r1, ← hs2, step_xfer_of_aligned s2 _ _ a2, ← hs3, step_rack_of_step s3 0 r3,
x2, x3, x4, List.nil_append] theorem split_read : FragmentReadFor 2 isSplitProof
:= by
intro win c c1 st s r' M hs hne hL hc hF hrs hcs hpre hN
cases s with
| x _ _ => exact hs.elim
| drop _ => exact hs.elim
| merge _ => exact hs.elim
| tuck _ => exact hs.elim
| knit _ _ => exact hs.elim
| split d bd =>
have hy := hL.1
have hym := yarn_mem hy
have hyl := yarnIdx_lt hy
set y := yarnIdx c.row with hydef
have hlo := fits_lo hF hc (by omega)
have hcar : st.carrier = 3 + 2 * (y : Int) := laid_carrier hL hc
have hr : st.rack = 0 := hL.2.2.1
have hb : ∀ j, st.stk ⟨.b, j⟩ = [] := hL.2.2.2.1
have hys : c.at_.getD (yarnIdx c.row) 0 = 3 + 2 * (y : Int) := by rw [hc]; exact canonical_getD_two hyl
cases hk : stackAt c.row (besideIdx c.row d) with
| none => simp [runStepB, hk] at hrs
| some k =>
obtain ⟨hbl, hga, hk0⟩ := Frag.stackAt_some hk
have hbyl : besideIdx c.row d ≠ y := by
intro h; rw [h, getD_yarnIdx _ hy] at hga; exact absurd hga (by simp)
have hhb : hasBeside c.row d = true := by
cases d <;> simp [hasBeside, besideIdx] at hbl hbyl ⊢ <;> omega
have hbc : c.at_.getD (besideIdx c.row d) 0 = 3 + 2 * ((besideIdx c.row d : Nat) : Int) := by
rw [hc]; exact canonical_getD_two hbl
simp only [compileStep, hym, hhb, and_self, ite_true, hk, Option.some.injEq, hbc, hys] at hcs
subst hcs
simp only [emit, List.drop_left]
have hfront := laid_front_at hL hc
have hoff := laid_front_off hL hc
have hfy : st.stk ⟨.f, 3 + 2 * (y : Int)⟩ = [] := laid_front_yarn hL hc
cases d with
| R =>
have e1 : besideIdx c.row .R = y + 1 := rfl
simp only [e1] at hga hbl ⊢
set cc : Int := 3 + 2 * (y : Int) with hccdef
have ec1 : 3 + 2 * ((y + 1 : Nat) : Int) = cc + 2 := by push_cast; omega
simp only [ec1, ite_true]
have hkk : (st.stk ⟨.f, cc + 2⟩).length = k := by rw [← ec1, hfront, hga]; rfl
have hk' : (st.stk ⟨.f, cc + 2⟩).length ≠ 0 := by omega
have hyt : yarnTo cc (cc + 2) = [.miss .R (fN cc), .miss .R (fN (cc + 1))] := by
unfold yarnTo; rw [ite_eq_left (by omega), show (cc + 2 - cc).toNat = 2 by omega, Frag.bind_coe_map]
simp [List.range_succ]
have hf1 : st.stk ⟨.f, cc + 1⟩ = [] := hoff _ (fun j => by omega)
have hf3 : st.stk ⟨.f, cc + 2 + 1⟩ = [] := hoff _ (fun j => by omega)
obtain ⟨hs1, x1⟩ := sim_miss_R win st cc hr hcar hfy (hb cc)
obtain ⟨hs2', x2⟩ := sim_miss_R win { st with carrier := cc + 1 } (cc + 1) hr rfl hf1 (hb _)
have hs2 : step { st with carrier := cc + 1 } (.miss .R (fN (cc + 1))) = .ok { st with carrier := cc + 2 } := by
rw [hs2']; congr 1
rw [hyt]
set t := { st with carrier := cc + 2 } with htdef
cases bd with
| f =>
have hops : splitOps .R .f (cc + 2) = [.split .R ⟨.f, cc + 2⟩ ⟨.b, cc + 2⟩, .miss .R (fN (cc + 2 + 1)),
.rack 1, .xfer (bN (cc + 2)) (fN (cc + 2 + 1)), .rack 0] := by simp [splitOps]
have hsp : step t (.split .R ⟨.f, cc + 2⟩ ⟨.b, cc + 2⟩) = .ok (apply t (.split .R ⟨.f, cc + 2⟩ ⟨.b, cc + 2⟩)) := by
simp [htdef, step, refusal, linRefusal, extraRefusal, erase, slot, arrivesFrom, aligned, hr, hk']
have xsp : simOp win t (.split .R ⟨.f, cc + 2⟩ ⟨.b, cc + 2⟩) = [.split .R .f] := by
simp [simOp, slot, htdef, hr, nB, hb, ob, xs, ms]
set t2 := apply t (.split .R ⟨.f, cc + 2⟩ ⟨.b, cc + 2⟩) with ht2
obtain ⟨hm, xm⟩ := sim_miss_R win t2 (cc + 2 + 1) (by simp [ht2, htdef, apply, hr])
(by simp [ht2, htdef, apply, slot, leavesTo])
(by simp [ht2, htdef, apply, hf3]) (by simp [ht2, htdef, apply, hb])
have hpost := sim_post3 win { t2 with carrier := cc + 2 + 1 + 1 } (cc + 2) (by simp [ht2, htdef, apply, hr])
(fun j hj => by simp [ht2, htdef, apply, hb, hj]) (by simp)
(by simp [ht2, htdef, apply, hf3])
rw [hops]
simp only [List.cons_append, List.nil_append]
rw [sim_cons win st _ _ _ hs1, sim_cons win _ _ _ _ hs2, sim_cons win t _ _ _ hsp, sim_cons win t2 _ _ _ hm,
hpost, x1, x2, xsp, xm]
simp
| b =>
have hops : splitOps .R .b (cc + 2) = [.xfer (fN (cc + 2)) (bN (cc + 2)), .split .R ⟨.b, cc + 2⟩ ⟨.f, cc + 2⟩,
.miss .R (fN (cc + 2 + 1)), .rack 1, .xfer (bN (cc + 2)) (fN (cc + 2 + 1)), .rack 0] := by
simp [splitOps]
have a1 : aligned t.rack (fN (cc + 2)) (bN (cc + 2)) = true := by simp [htdef, aligned, slot, hr, fN, bN]
have xa : simOp win t (.xfer (fN (cc + 2)) (bN (cc + 2))) = [] :=
simOp_xfer_empty win t _ _ (by simp [htdef, hb, bN])
set t1 := apply t (.xfer (fN (cc + 2)) (bN (cc + 2))) with ht1
have hsp : step t1 (.split .R ⟨.b, cc + 2⟩ ⟨.f, cc + 2⟩) =
.ok (apply t1 (.split .R ⟨.b, cc + 2⟩ ⟨.f, cc + 2⟩)) := by
simp [ht1, htdef, step, refusal, linRefusal, extraRefusal, erase, apply, upd_apply, slot, arrivesFrom,
aligned, hr, xferStack, hb, hk', fN, bN]
have xsp : simOp win t1 (.split .R ⟨.b, cc + 2⟩ ⟨.f, cc + 2⟩) = [.split .R .b] := by
simp [simOp, slot, ht1, htdef, apply, hr, nF, ob, xs, ms, fN, bN]
set t2 := apply t1 (.split .R ⟨.b, cc + 2⟩ ⟨.f, cc + 2⟩) with ht2
obtain ⟨hm, xm⟩ := sim_miss_R win t2 (cc + 2 + 1) (by simp [ht2, ht1, htdef, apply, hr])
(by simp [ht2, ht1, htdef, apply, slot, leavesTo, hr])
(by simp [ht2, ht1, htdef, apply, hf3, fN, bN])
(by simp [ht2, ht1, htdef, apply, hb, fN, bN])
have hpost := sim_post3 win { t2 with carrier := cc + 2 + 1 + 1 } (cc + 2)
(by simp [ht2, ht1, htdef, apply, hr])
(fun j hj => by simp [ht2, ht1, htdef, apply, hb, hj, fN, bN]) (by simp)
(by simp [ht2, ht1, htdef, apply, hf3, fN, bN])
rw [hops]
simp only [List.cons_append, List.nil_append]
rw [sim_cons win st _ _ _ hs1, sim_cons win _ _ _ _ hs2, sim_cons win t _ _ _ (step_xfer_of_aligned t _ _ a1),
sim_cons win t1 _ _ _ hsp, sim_cons win t2 _ _ _ hm, hpost, x1, x2, xa, xsp, xm]
simp
| L =>
have e1 : besideIdx c.row .L = y - 1 := rfl
have hy1 : 1 ≤ y := by
by_contra h0
exact hbyl (by rw [e1]; omega)
simp only [e1] at hga hbl ⊢
set cc : Int := 3 + 2 * (y : Int) with hccdef
have ec1 : 3 + 2 * ((y - 1 : Nat) : Int) = cc - 2 := by push_cast [hy1]; omega
simp only [ec1, show (Dir.L = Dir.R) = False by simp, ite_false]
have hkk : (st.stk ⟨.f, cc - 2⟩).length = k := by rw [← ec1, hfront, hga]; rfl
have hk' : (st.stk ⟨.f, cc - 2⟩).length ≠ 0 := by omega
have hyt : yarnTo cc (cc - 2 + 1) = [.miss .L (fN (cc - 1))] := by
unfold yarnTo; rw [ite_eq_right (by omega), show (cc - (cc - 2 + 1)).toNat = 1 by omega, Frag.bind_coe_map]
simp
have hf1 : st.stk ⟨.f, cc - 1⟩ = [] := hoff _ (fun j => by omega)
have hf1' : st.stk ⟨.f, cc - 2 + 1⟩ = [] := hoff _ (fun j => by omega)
obtain ⟨hs1', x1⟩ := sim_miss_L win st (cc - 1) hr (by rw [hcar]; omega) hf1 (hb _)
set t := { st with carrier := cc - 1 } with htdef
have hs1 : step st (.miss .L (fN (cc - 1))) = .ok t := hs1'
rw [hyt]
have hcc : cc - 1 = cc - 2 + 1 := by omega
cases bd with
| f =>
have hops : splitOps .L .f (cc - 2) = [.split .L ⟨.f, cc - 2⟩ ⟨.b, cc - 2⟩,
.rack 1, .xfer (bN (cc - 2)) (fN (cc - 2 + 1)), .rack 0] := by simp [splitOps]
have hsp : step t (.split .L ⟨.f, cc - 2⟩ ⟨.b, cc - 2⟩) = .ok (apply t (.split .L ⟨.f, cc - 2⟩ ⟨.b, cc - 2⟩)) := by
simp [htdef, step, refusal, linRefusal, extraRefusal, erase, slot, arrivesFrom, aligned, hr, hk', hcc]
have xsp : simOp win t (.split .L ⟨.f, cc - 2⟩ ⟨.b, cc - 2⟩) = [.split .L .f] := by
simp [simOp, slot, htdef, hr, nB, hb, ob, xs, ms]
set t2 := apply t (.split .L ⟨.f, cc - 2⟩ ⟨.b, cc - 2⟩) with ht2
have hpost := sim_post3 win t2 (cc - 2) (by simp [ht2, htdef, apply, hr])
(fun j hj => by simp [ht2, htdef, apply, hb, hj]) (by simp [ht2, htdef, apply, slot, leavesTo])
(by simp [ht2, htdef, apply, hf1'])
rw [hops]
simp only [List.cons_append, List.nil_append]
rw [sim_cons win st _ _ _ hs1, sim_cons win t _ _ _ hsp, hpost, x1, xsp]
simp
| b =>
have hops : splitOps .L .b (cc - 2) = [.xfer (fN (cc - 2)) (bN (cc - 2)), .split .L ⟨.b, cc - 2⟩ ⟨.f, cc - 2⟩,
.rack 1, .xfer (bN (cc - 2)) (fN (cc - 2 + 1)), .rack 0] := by simp [splitOps]
have a1 : aligned t.rack (fN (cc - 2)) (bN (cc - 2)) = true := by simp [htdef, aligned, slot, hr, fN, bN]
have xa : simOp win t (.xfer (fN (cc - 2)) (bN (cc - 2))) = [] :=
simOp_xfer_empty win t _ _ (by simp [htdef, hb, bN])
set t1 := apply t (.xfer (fN (cc - 2)) (bN (cc - 2))) with ht1
have hsp : step t1 (.split .L ⟨.b, cc - 2⟩ ⟨.f, cc - 2⟩) =
.ok (apply t1 (.split .L ⟨.b, cc - 2⟩ ⟨.f, cc - 2⟩)) := by
simp [ht1, htdef, step, refusal, linRefusal, extraRefusal, erase, apply, upd_apply, slot, arrivesFrom,
aligned, hr, xferStack, hb, hk', fN, bN, hcc]
have xsp : simOp win t1 (.split .L ⟨.b, cc - 2⟩ ⟨.f, cc - 2⟩) = [.split .L .b] := by
simp [simOp, slot, ht1, htdef, apply, hr, nF, ob, xs, ms, fN, bN]
set t2 := apply t1 (.split .L ⟨.b, cc - 2⟩ ⟨.f, cc - 2⟩) with ht2
have hpost := sim_post3 win t2 (cc - 2) (by simp [ht2, ht1, htdef, apply, hr])
(fun j hj => by simp [ht2, ht1, htdef, apply, hb, hj, fN, bN])
(by simp [ht2, ht1, htdef, apply, slot, leavesTo, hr])
(by simp [ht2, ht1, htdef, apply, hf1', fN, bN])
rw [hops]
simp only [List.cons_append, List.nil_append]
rw [sim_cons win st _ _ _ hs1, sim_cons win t _ _ _ (step_xfer_of_aligned t _ _ a1),
sim_cons win t1 _ _ _ hsp, hpost, x1, xa, xsp]
simp The exchange fragment reads as the exchange
x_read : FragmentReadFor 2 isX. From the canonical layout, the compiled fragment of x a front reads as
x a front.
| Theorem | Exchange |
|---|---|
x_read_ys |
yarn–stack, both senses |
x_read_sy |
stack–yarn, both senses |
x_read_ss_false |
stack–stack, x a false: the left stack is behind |
x_read_ss_true |
stack–stack, x a true: the left stack is in front |
rackSteps_single: a rack that reads as steps at one position only.
namespace FormalizeKnitting.Bridge.RM
open FormalizeKnitting.Knitout FormalizeKnitting.Knitout.MR FormalizeKnitting.Bridge.MR FormalizeKnitting.Knitout.Exch Frag
theorem rackSteps_single (win : Window) (s : State) (r : Int) (k0 : Nat) (hk0 : k0 < win.w)
(h : ∀ k < win.w, k ≠ k0 →
(r < s.rack → nB s (win.lo + k) = 0 ∨ nY s (win.lo + k) + ob (nF s (win.lo + k)) = 0) ∧
(¬ r < s.rack → (s.stk ⟨.b, win.lo + k - 1 - s.rack⟩).length = 0 ∨
nY s (win.lo + k) + ob (nF s (win.lo + k)) = 0)) :
rackSteps win s r =
if r < s.rack then
if nB s (win.lo + k0) = 0 then [] else
(List.range (nY s (win.lo + k0) + ob (nF s (win.lo + k0)))).map fun j =>
.x (objsBefore win s (win.lo + k0) + (nY s (win.lo + k0) + ob (nF s (win.lo + k0))) - 1 - j) true
else
if (s.stk ⟨.b, win.lo + k0 - 1 - s.rack⟩).length = 0 then [] else
(List.range (nY s (win.lo + k0) + ob (nF s (win.lo + k0)))).map fun j =>
.x (objsBefore win s (win.lo + k0) - 1 + j) falseProof
:= by
unfold rackSteps
rw [bind_coe_map1, flatMap_single _ (k0 : Int) _ (List.nodup_range.map (fun a b h => by exact_mod_cast h))
(List.mem_map.mpr ⟨k0, List.mem_range.mpr hk0, rfl⟩)]
intro ki hki hne
obtain ⟨k, hk, rfl⟩ := List.mem_map.mp hki
obtain ⟨h1, h2⟩ := h k (List.mem_range.mp hk) (fun e => hne (by rw [e]))
dsimp only
by_cases hr : r < s.rack
· simp only [hr, ite_true]
rcases h1 hr with e | e
· simp [e]
· split <;> simp [e]
· simp only [hr, ite_false]
rcases h2 hr with e | e
· simp [e]
· split <;> simp [e] theorem x_read_ys (win : Window) (c c1 : Comp) (st : State) (a : Nat) (front : Bool) (r' : Row) (M : BWord Label)
(hP : c.row.getD a .yarn = .yarn) (hne : NoEmpty c.row) (hL : Laid c st)
(hc : c.at_ = canonical 2 c.row.length) (hF : Fits win c) (hrs : runStepB c.row (.x a front) = some (r', M))
(hcs : compileStep c (.x a front) = some c1) :
sim win st (c1.ops.drop c.ops.length) = [.x a front]Proof
:= by
have hy := hL.1
by_cases ha : a + 1 < c.row.length
swap
· simp [runStepB, ha] at hrs
have hya : a = yarnIdx c.row := (getD_eq_yarn_iff hy (by omega)).mp hP
obtain ⟨k, hk⟩ : ∃ k, c.row.getD (a + 1) .yarn = .stack k := by
cases h : c.row.getD (a + 1) .yarn with
| yarn => exact absurd ((getD_eq_yarn_iff hy ha).mp h) (by omega)
| stack k => exact ⟨k, rfl⟩
have hk0 : k ≠ 0 := by have := hne k (hk ▸ getD_mem_of_lt ha); omega
have hym := yarn_mem hy
set p : Int := 3 + 2 * (a : Int) with hpdef
have hp : c.at_.getD a 0 = p := by rw [hc]; exact canonical_getD_two (by omega)
have hq : c.at_.getD (a + 1) 0 = p + 2 := by rw [hc, canonical_getD_two ha]; push_cast; omega
simp only [compileStep, hym, ha, and_self, ite_true, hP, hk, hp, hq, Option.map_some,
Option.some.injEq, emit, show p + 2 + 1 = p + 3 by omega, yarnTo_R3] at hcs
subst hcs
simp only [List.drop_left]
have hlo := fits_lo hF hc (by omega)
have hcar : st.carrier = p := by rw [laid_carrier hL hc, ← hya]
have hfy : st.stk ⟨.f, p⟩ = [] := by have := laid_front_yarn hL hc; rw [← hya] at this; exact this
have hf1 : st.stk ⟨.f, p + 1⟩ = [] := laid_front_off hL hc _ (fun j => by omega)
have ec : 3 + 2 * ((a + 1 : Nat) : Int) = p + 2 := by push_cast; omega
have hkk : (st.stk ⟨.f, p + 2⟩).length = k := by rw [← ec, laid_front_at hL hc, hk]; rfl
have hr : st.rack = 0 := hL.2.2.1
have hb : ∀ j, st.stk ⟨.b, j⟩ = [] := hL.2.2.2.1
have hob : objsBefore win { st with carrier := p + 2 } (p + 2) = a := by
unfold objsBefore
rw [show (p + 2 - win.lo).toNat = (3 + 2 * (a : Int) - win.lo).toNat + 1 + 1 by omega, rowOf_succ, rowOf_succ,
rowOf_moved_upto hL hc hne win.lo (by omega) (p + 2) a (by omega) (by omega) (by omega)]
have s0 : seg { st with carrier := p + 2 } (win.lo + ((3 + 2 * (a : Int) - win.lo).toNat : Int)) = [] := by
rw [show win.lo + ((3 + 2 * (a : Int) - win.lo).toNat : Int) = p by omega]
simp [seg, nF, nB, hfy, hb, stkO]
have s1 : seg { st with carrier := p + 2 } (win.lo + (((3 + 2 * (a : Int) - win.lo).toNat + 1 : Nat) : Int)) =
[] := by
rw [show win.lo + (((3 + 2 * (a : Int) - win.lo).toNat + 1 : Nat) : Int) = p + 1 by push_cast; omega]
simp [seg, nF, nB, hf1, hb, stkO]
rw [s0, s1]; simp; omega
cases front with
| false =>
simp only [Bool.false_eq_true, ite_false, List.nil_append, List.append_nil]
obtain ⟨hs1, x1⟩ := sim_miss_R win st p hr hcar hfy (hb p)
obtain ⟨hs2', x2⟩ := sim_miss_R win { st with carrier := p + 1 } (p + 1) hr rfl hf1 (hb _)
have hs2 : step { st with carrier := p + 1 } (.miss .R (fN (p + 1))) = .ok { st with carrier := p + 2 } := by
rw [hs2']; congr 1
have hs3 : step { st with carrier := p + 2 } (.miss .R (fN (p + 2))) =
.ok (apply { st with carrier := p + 2 } (.miss .R (fN (p + 2)))) := by
simp [step, refusal, linRefusal, extraRefusal, erase, arrivesFrom, slot, fN, hr]
rw [sim_cons win st _ _ _ hs1, sim_cons win _ _ _ _ hs2, sim_single win _ _ _ hs3, x1, x2]
simp [simOp, slot, fN, arrivesFrom, hob, nF, nB, hkk, hb, ob, xs, hk0]
| true =>
simp only [ite_true, List.cons_append, List.nil_append]
have aX : aligned st.rack (fN (p + 2)) (bN (p + 2)) = true := by simp [aligned, slot, hr, fN, bN]
set sX := apply st (.xfer (fN (p + 2)) (bN (p + 2))) with hsX
have kX : ∀ q, sX.stk q = if q = ⟨.b, p + 2⟩ then (st.stk ⟨.f, p + 2⟩).reverse
else if q = ⟨.f, p + 2⟩ then [] else st.stk q := by
intro q; simp only [hsX, apply, upd_apply, xferStack, hb, List.append_nil, fN, bN]
have xX : simOp win st (.xfer (fN (p + 2)) (bN (p + 2))) = [] := simOp_xfer_empty win st _ _ (by simp [hb, bN])
have rX : sX.rack = 0 := hr
have cX : sX.carrier = p := hcar
obtain ⟨hs1, x1⟩ := sim_miss_R win sX p rX cX (by rw [kX]; simp [hfy]) (by rw [kX]; simp [hb])
obtain ⟨hs2', x2⟩ := sim_miss_R win { sX with carrier := p + 1 } (p + 1) rX rfl
(by simp [kX, hf1]) (by simp [kX, hb])
have hs2 : step { sX with carrier := p + 1 } (.miss .R (fN (p + 1))) = .ok { sX with carrier := p + 2 } := by
rw [hs2']; congr 1
have hs3 : step { sX with carrier := p + 2 } (.miss .R (fN (p + 2))) =
.ok (apply { sX with carrier := p + 2 } (.miss .R (fN (p + 2)))) := by
simp [step, refusal, linRefusal, extraRefusal, erase, arrivesFrom, slot, fN, rX]
set s3 := apply { sX with carrier := p + 2 } (.miss .R (fN (p + 2))) with hs3d
have a4 : aligned s3.rack (bN (p + 2)) (fN (p + 2)) = true := by
simp [hs3d, apply, aligned, slot, rX, fN, bN]
have x4 : simOp win s3 (.xfer (bN (p + 2)) (fN (p + 2))) = [] := simOp_xfer_empty win s3 _ _ (by
simp [hs3d, apply, kX, fN])
have hob2 : objsBefore win { sX with carrier := p + 2 } (p + 2) = a := by
rw [objsBefore_congr win _ { st with carrier := p + 2 } (p + 2) (fun z _ hz => by
have n1 : z ≠ p + 2 := by omega
simp [seg, nF, nB, kX, rX, hr, n1]), hob]
have x3 : simOp win { sX with carrier := p + 2 } (.miss .R (fN (p + 2))) = [.x a true] := by
simp only [simOp, slot, fN, arrivesFrom, hob2]
simp [nF, nB, kX, hkk, ob, xs, hk0, rX]
rw [sim_cons win st _ _ _ (step_xfer_of_aligned st _ _ aX), sim_cons win sX _ _ _ hs1, sim_cons win _ _ _ _ hs2,
sim_cons win _ _ _ _ hs3, sim_single win _ _ _ (step_xfer_of_aligned s3 _ _ a4), xX, x1, x2, x3, x4]
simp theorem x_read_sy (win : Window) (c c1 : Comp) (st : State) (a : Nat) (front : Bool) (r' : Row) (M : BWord Label)
(hP : c.row.getD (a + 1) .yarn = .yarn) (hne : NoEmpty c.row) (hL : Laid c st)
(hc : c.at_ = canonical 2 c.row.length) (hF : Fits win c) (hrs : runStepB c.row (.x a front) = some (r', M))
(hcs : compileStep c (.x a front) = some c1) :
sim win st (c1.ops.drop c.ops.length) = [.x a front]Proof
:= by
have hy := hL.1
by_cases ha : a + 1 < c.row.length
swap
· simp [runStepB, ha] at hrs
have hya : a + 1 = yarnIdx c.row := (getD_eq_yarn_iff hy ha).mp hP
obtain ⟨k, hk⟩ : ∃ k, c.row.getD a .yarn = .stack k := by
cases h : c.row.getD a .yarn with
| yarn => exact absurd ((getD_eq_yarn_iff hy (by omega)).mp h) (by omega)
| stack k => exact ⟨k, rfl⟩
have hk0 : k ≠ 0 := by have := hne k (hk ▸ getD_mem_of_lt (show a < c.row.length by omega)); omega
have hym := yarn_mem hy
set p : Int := 3 + 2 * (a : Int) with hpdef
have hp : c.at_.getD a 0 = p := by rw [hc]; exact canonical_getD_two (by omega)
have hq : c.at_.getD (a + 1) 0 = p + 2 := by rw [hc, canonical_getD_two ha]; push_cast; omega
simp only [compileStep, hym, ha, and_self, ite_true, hP, hk, hp, hq, Option.map_some,
Option.some.injEq, emit, yarnTo_L2] at hcs
subst hcs
simp only [List.drop_left]
have hlo := fits_lo hF hc (by omega)
have hcar : st.carrier = p + 2 := by rw [laid_carrier hL hc, ← hya]; push_cast; omega
have hf1 : st.stk ⟨.f, p + 1⟩ = [] := laid_front_off hL hc _ (fun j => by omega)
have hkk : (st.stk ⟨.f, p⟩).length = k := by rw [laid_front_at hL hc, hk]; rfl
have hr : st.rack = 0 := hL.2.2.1
have hb : ∀ j, st.stk ⟨.b, j⟩ = [] := hL.2.2.2.1
have hob0 : objsBefore win st (p + 1) = a + 1 := objsBefore_canon_between hL hc hne win (by omega) a (by omega)
cases front with
| true =>
simp only [Bool.not_true, Bool.false_eq_true, ite_false, List.nil_append, List.append_nil]
obtain ⟨hs1, x1⟩ := sim_miss_L win st (p + 1) hr (by rw [hcar]; omega) hf1 (hb _)
have hs2 : step { st with carrier := p + 1 } (.miss .L (fN p)) =
.ok (apply { st with carrier := p + 1 } (.miss .L (fN p))) := by
simp [step, refusal, linRefusal, extraRefusal, erase, arrivesFrom, slot, fN, hr]
have hob : objsBefore win { st with carrier := p + 1 } (p + 1) = a + 1 := by
rw [objsBefore_congr win _ st (p + 1) (fun z _ hz => seg_carrier st (p + 1) z (by rw [hcar]; omega)
(by omega)), hob0]
rw [sim_cons win st _ _ _ hs1, sim_single win _ _ _ hs2, x1]
simp only [simOp, slot, fN, arrivesFrom, hob]
simp [nF, nB, hkk, hb, ob, xs, hk0]
| false =>
simp only [Bool.not_false, ite_true, List.cons_append, List.nil_append,
]
have aX : aligned st.rack (fN p) (bN p) = true := by simp [aligned, slot, hr, fN, bN]
set sX := apply st (.xfer (fN p) (bN p)) with hsX
have kX : ∀ q, sX.stk q = if q = ⟨.b, p⟩ then (st.stk ⟨.f, p⟩).reverse
else if q = ⟨.f, p⟩ then [] else st.stk q := by
intro q; simp only [hsX, apply, upd_apply, xferStack, hb, List.append_nil, fN, bN]
have xX : simOp win st (.xfer (fN p) (bN p)) = [] := simOp_xfer_empty win st _ _ (by simp [hb, bN])
have rX : sX.rack = 0 := hr
have cX : sX.carrier = p + 2 := hcar
obtain ⟨hs1, x1⟩ := sim_miss_L win sX (p + 1) rX (by rw [cX]; omega) (by rw [kX]; simp [hf1])
(by rw [kX]; simp [hb])
have hs2 : step { sX with carrier := p + 1 } (.miss .L (fN p)) =
.ok (apply { sX with carrier := p + 1 } (.miss .L (fN p))) := by
simp [step, refusal, linRefusal, extraRefusal, erase, arrivesFrom, slot, fN, rX]
set s3 := apply { sX with carrier := p + 1 } (.miss .L (fN p)) with hs3d
have a4 : aligned s3.rack (bN p) (fN p) = true := by
simp [hs3d, apply, aligned, slot, rX, fN, bN]
have x4 : simOp win s3 (.xfer (bN p) (fN p)) = [] := simOp_xfer_empty win s3 _ _ (by
simp [hs3d, apply, kX, fN])
have hob : objsBefore win { sX with carrier := p + 1 } (p + 1) = a + 1 := by
rw [objsBefore_congr win _ st (p + 1) (fun z _ hz => by
by_cases hzp : z = p
· subst hzp
simp [seg, nF, nB, kX, rX, hr, hcar, hkk, hb, stkO, hk0]
· have n1 : p + 1 ≠ z := by omega
have n2 : p + 2 ≠ z := by omega
simp [seg, nF, nB, kX, rX, hr, hcar, hzp, n1, n2]), hob0]
have x3 : simOp win { sX with carrier := p + 1 } (.miss .L (fN p)) = [.x a false] := by
simp only [simOp, slot, fN, arrivesFrom, hob]
simp [nF, nB, kX, hkk, ob, xs, hk0, rX]
rw [sim_cons win st _ _ _ (step_xfer_of_aligned st _ _ aX), sim_cons win sX _ _ _ hs1,
sim_cons win _ _ _ _ hs2, sim_single win _ _ _ (step_xfer_of_aligned s3 _ _ a4), xX, x1, x3, x4]
simp theorem x_read_ss_false (win : Window) (c c1 : Comp) (st : State) (a : Nat) (r' : Row) (M : BWord Label)
(hP : Frag.IsSS (c.row.getD a .yarn) (c.row.getD (a + 1) .yarn)) (hne : NoEmpty c.row) (hL : Laid c st)
(hc : c.at_ = canonical 2 c.row.length) (hF : Fits win c) (hrs : runStepB c.row (.x a false) = some (r', M))
(hcs : compileStep c (.x a false) = some c1) :
sim win st (c1.ops.drop c.ops.length) = [.x a false]Proof
:= by
have hy := hL.1
by_cases ha : a + 1 < c.row.length
swap
· simp [runStepB, ha] at hrs
obtain ⟨k, hk⟩ := stack_of_ne hP.1
obtain ⟨l, hl⟩ := stack_of_ne hP.2
have hk0 : k ≠ 0 := by have := hne k (hk ▸ getD_mem_of_lt (show a < c.row.length by omega)); omega
have hl0 : l ≠ 0 := by have := hne l (hl ▸ getD_mem_of_lt ha); omega
have hym := yarn_mem hy
have hyl := yarnIdx_lt hy
set y := yarnIdx c.row with hydef
have hgy : c.row.getD y .yarn = .yarn := Frag.getD_yarnIdx _ hy
have hya : y ≠ a := fun h => by rw [h, hk] at hgy; exact absurd hgy (by simp)
have hya1 : y ≠ a + 1 := fun h => by rw [h, hl] at hgy; exact absurd hgy (by simp)
set p : Int := 3 + 2 * (a : Int) with hpdef
have hp : c.at_.getD a 0 = p := by rw [hc]; exact canonical_getD_two (by omega)
have hq : c.at_.getD (a + 1) 0 = p + 2 := by rw [hc, canonical_getD_two ha]; push_cast; omega
have hm := moveObject_right1 c a hk hp
simp only [compileStep, hym, ha, and_self, ite_true, hk, hl, hq, Option.map_some, Option.some.injEq, emit,
Bool.not_false, show p + 2 - 1 = p + 1 by omega, show p + 2 + 1 = p + 3 by omega, hm] at hcs
subst hcs
simp only [List.append_assoc, List.drop_left]
have hlo := fits_lo hF hc (by omega)
have hfit := fits_canon hF hc (show a < c.row.length by omega)
have hfit1 := fits_canon hF hc ha
set cc : Int := 3 + 2 * (y : Int) with hccdef
have hcar : st.carrier = cc := laid_carrier hL hc
have hfp1 : st.stk ⟨.f, p + 1⟩ = [] := laid_front_off hL hc _ (fun j => by omega)
have hfp3 : st.stk ⟨.f, p + 3⟩ = [] := laid_front_off hL hc _ (fun j => by omega)
have honeA : (st.stk ⟨.f, p⟩).length = k := by rw [laid_front_at hL hc, hk]; rfl
have honeB : (st.stk ⟨.f, p + 2⟩).length = l := by
have := laid_front_at hL hc (a + 1)
rw [show 3 + 2 * ((a + 1 : Nat) : Int) = p + 2 by push_cast; omega, hl] at this
exact this
have hb := hL.2.2.2.1
have hr := hL.2.2.1
have ins : ∀ z, win.lo + 1 ≤ z → z + 2 ≤ win.lo + win.w → win.inside z = true := fun z h1 h2 => inside_of h1 h2
obtain ⟨st1, hd1, h1r, h1c, k1⟩ := loopStep_right_free win st p hr hb (by rw [hcar]; omega) hfp1
(ins _ (by omega) (by omega)) (ins _ (by omega) (by omega))
have hrun : runSteps st (loopStep p true) = some st1 :=
runSteps_of_denote (denote_of_denote1 win st _ _ _ (by
intro op hop; simp [loopStep] at hop; rcases hop with h | h | h | h <;> subst h <;> trivial) hd1)
rw [sim_append win st st1 _ _ hrun, sim_loopStep_right win st p hr hb (by rw [hcar]; omega) hfp1, List.nil_append]
have b1 : ∀ j, st1.stk ⟨.b, j⟩ = [] := by intro j; rw [k1]; simp [hb]
set t1 := apply st1 (.rack (-1)) with ht1
set t2 := apply t1 (.xfer (fN (p + 1)) (bN (p + 2))) with ht2
set t3 := apply t2 (.rack 0) with ht3
set t4 := apply t3 (.rack 1) with ht4
set t5 := apply t4 (.xfer (bN (p + 2)) (fN (p + 3))) with ht5
set t6 := apply t5 (.rack 0) with ht6
have e1r : t1.rack = -1 := rfl
have k2 : ∀ q, t2.stk q = if q = ⟨.b, p + 2⟩ then (st.stk ⟨.f, p⟩).reverse else
if q = ⟨.f, p + 1⟩ ∨ q = ⟨.f, p⟩ then [] else st.stk q := by
intro q
show Function.update (Function.update st1.stk (fN (p + 1)) []) (bN (p + 2))
(xferStack (st1.stk (fN (p + 1))) (st1.stk (bN (p + 2)))) q = _
simp only [upd_apply, xferStack, k1, fN, bN]
by_cases h1 : q = ⟨.b, p + 2⟩
· subst h1; simp [hb]
· by_cases h2 : q = ⟨.f, p + 1⟩
· subst h2; simp
· by_cases h3 : q = ⟨.f, p⟩
· subst h3; simp
· simp [h1, h2, h3]
have e2r : t2.rack = -1 := rfl
have e2c : t2.carrier = cc := by rw [← hcar, ← h1c]; rfl
have b2 : ∀ j, j ≠ p + 2 → t2.stk ⟨.b, j⟩ = [] := by intro j h; simp [k2, h, hb]
have e3r : t3.rack = 0 := rfl
have e4r : t4.rack = 1 := rfl
have k5 : ∀ q, t5.stk q = if q = ⟨.f, p + 3⟩ then st.stk ⟨.f, p⟩ else if q = ⟨.b, p + 2⟩ then []
else t2.stk q := by
intro q
show Function.update (Function.update t2.stk (bN (p + 2)) []) (fN (p + 3))
(xferStack (t2.stk (bN (p + 2))) (t2.stk (fN (p + 3)))) q = _
simp only [upd_apply, xferStack, fN, bN]
by_cases h1 : q = ⟨.f, p + 3⟩
· subst h1; rw [k2, k2]; simp [hfp3]
· simp [h1]
have b5 : ∀ j, t5.stk ⟨.b, j⟩ = [] := by
intro j; rw [k5]; by_cases h : j = p + 2
· subst h; simp
· simp [h, k2, hb]
have e5r : t5.rack = 1 := rfl
have g1 : step st1 (.rack (-1)) = .ok t1 := step_rack_of_step st1 (-1) (by simp [h1r])
have g2 : step t1 (.xfer (fN (p + 1)) (bN (p + 2))) = .ok t2 :=
step_xfer_of_aligned t1 _ _ (by simp [e1r, aligned, slot, fN, bN, show p + 2 + -1 = p + 1 by omega])
have g3 : step t2 (.rack 0) = .ok t3 := step_rack_of_step t2 0 (by simp [e2r])
have g4 : step t3 (.rack 1) = .ok t4 := step_rack_of_step t3 1 (by simp [e3r])
have g5 : step t4 (.xfer (bN (p + 2)) (fN (p + 3))) = .ok t5 :=
step_xfer_of_aligned t4 _ _ (by simp [e4r, aligned, slot, fN, bN, show p + 2 + 1 = p + 3 by omega])
have g6 : step t5 (.rack 0) = .ok t6 := step_rack_of_step t5 0 (by simp [e5r])
have y1 : simOp win st1 (.rack (-1)) = [] := by
apply rackSteps_nil
intro k _
exact ⟨fun _ => Or.inl (by simp [nB, b1]), fun h => absurd (show (-1 : Int) < st1.rack by rw [h1r]; omega) h⟩
have y2 : simOp win t1 (.xfer (fN (p + 1)) (bN (p + 2))) = [] :=
simOp_xfer_empty win t1 _ _ (by simp [ht1, apply, b1, bN])
have hob2 : objsBefore win t2 (p + 2) = a + 1 := by
have hcg : ∀ z, z < p → seg t2 z = seg st z := by
intro z hz
have n1 : z ≠ p := by omega
have n2 : z ≠ p + 1 := by omega
have n3 : z + 1 ≠ p + 2 := by omega
simp [seg, nF, nB, k2, e2r, hr, e2c, hcar, hb, n1, n2, n3]
unfold objsBefore
rw [show (p + 2 - win.lo).toNat = (3 + 2 * (a : Int) - win.lo).toNat + 1 + 1 by omega, rowOf_succ, rowOf_succ,
rowOf_congr (fun k hk => hcg _ (by omega)), rowOf_canon_upto hL hc hne win.lo (by omega) a (by omega)]
have s0 : seg t2 (win.lo + ((3 + 2 * (a : Int) - win.lo).toNat : Int)) = [] := by
rw [show win.lo + ((3 + 2 * (a : Int) - win.lo).toNat : Int) = p by omega]
have n1 : cc ≠ p := by omega
simp [seg, nF, nB, k2, e2r, e2c, hb, stkO, n1]
have s1 : seg t2 (win.lo + (((3 + 2 * (a : Int) - win.lo).toNat + 1 : Nat) : Int)) = [.stack k] := by
rw [show win.lo + (((3 + 2 * (a : Int) - win.lo).toNat + 1 : Nat) : Int) = p + 1 by push_cast; omega]
have n1 : cc ≠ p + 1 := by omega
simp [seg, nF, nB, k2, e2r, e2c, stkO, n1, honeA, hk0, show p + 1 - -1 = p + 2 by omega]
rw [s0, s1]; simp; omega
have y3 : simOp win t2 (.rack 0) = [.x a false] := by
show rackSteps win t2 0 = _
rw [rackSteps_single win t2 0 (p + 2 - win.lo).toNat (by omega) (fun k hk hne =>
⟨fun h => absurd h (by rw [e2r]; omega), fun _ => Or.inl (by rw [e2r, b2 _ (by omega)]; rfl)⟩)]
have ei : win.lo + ((p + 2 - win.lo).toNat : Int) = p + 2 := by omega
rw [ite_eq_right (by rw [e2r]; omega)]
simp only [ei, e2r, show p + 2 - 1 - -1 = p + 2 by omega, hob2]
have n1 : cc ≠ p + 2 := by omega
simp [nY, nF, k2, e2c, honeA, honeB, hk0, hl0, ob, n1]
have y4 : simOp win t3 (.rack 1) = [] := by
apply rackSteps_nil
intro k _
refine ⟨fun h => absurd h (by rw [e3r]; omega), fun _ => ?_⟩
by_cases h : win.lo + (k : Int) - 1 - t3.rack = p + 2
· right
have e : win.lo + (k : Int) = p + 3 := by rw [e3r] at h; omega
have n1 : cc ≠ p + 3 := by omega
rw [e]; simp [nY, nF, ob, show t3.stk = t2.stk from rfl, show t3.carrier = cc from e2c, k2, hfp3, n1]
· left; rw [show t3.stk = t2.stk from rfl, b2 _ h]; rfl
have y5 : simOp win t4 (.xfer (bN (p + 2)) (fN (p + 3))) = [] :=
simOp_xfer_empty win t4 _ _ (by simp [ht4, ht3, apply, k2, fN, hfp3])
have y6 : simOp win t5 (.rack 0) = [] := by
apply rackSteps_nil
intro k _
exact ⟨fun _ => Or.inl (by simp [nB, b5]), fun h => absurd (show (0 : Int) < t5.rack by rw [e5r]; omega) h⟩
rw [sim_cons win st1 _ _ _ g1, sim_cons win t1 _ _ _ g2, sim_cons win t2 _ _ _ g3, sim_cons win t3 _ _ _ g4,
sim_cons win t4 _ _ _ g5, sim_single win t5 _ _ g6, y1, y2, y3, y4, y5, y6]
simp theorem x_read_ss_true (win : Window) (c c1 : Comp) (st : State) (a : Nat) (r' : Row) (M : BWord Label)
(hP : Frag.IsSS (c.row.getD a .yarn) (c.row.getD (a + 1) .yarn)) (hne : NoEmpty c.row) (hL : Laid c st)
(hc : c.at_ = canonical 2 c.row.length) (hF : Fits win c) (hrs : runStepB c.row (.x a true) = some (r', M))
(hcs : compileStep c (.x a true) = some c1) :
sim win st (c1.ops.drop c.ops.length) = [.x a true]Proof
:= by
have hy := hL.1
by_cases ha : a + 1 < c.row.length
swap
· simp [runStepB, ha] at hrs
obtain ⟨k, hk⟩ := stack_of_ne hP.1
obtain ⟨l, hl⟩ := stack_of_ne hP.2
have hk0 : k ≠ 0 := by have := hne k (hk ▸ getD_mem_of_lt (show a < c.row.length by omega)); omega
have hl0 : l ≠ 0 := by have := hne l (hl ▸ getD_mem_of_lt ha); omega
have hym := yarn_mem hy
have hyl := yarnIdx_lt hy
set y := yarnIdx c.row with hydef
have hgy : c.row.getD y .yarn = .yarn := Frag.getD_yarnIdx _ hy
have hya : y ≠ a := fun h => by rw [h, hk] at hgy; exact absurd hgy (by simp)
have hya1 : y ≠ a + 1 := fun h => by rw [h, hl] at hgy; exact absurd hgy (by simp)
set p : Int := 3 + 2 * (a : Int) with hpdef
have hp : c.at_.getD a 0 = p := by rw [hc]; exact canonical_getD_two (by omega)
have hq : c.at_.getD (a + 1) 0 = p + 2 := by rw [hc, canonical_getD_two ha]; push_cast; omega
have hm := moveObject_left1 c (a + 1) hl hq
rw [show p + 2 - 1 = p + 1 by omega] at hm
simp only [compileStep, hym, ha, and_self, ite_true, hk, hl, hp, hq, Option.map_some, Option.some.injEq, emit,
Bool.not_true, ite_false, Bool.false_eq_true, hm] at hcs
subst hcs
simp only [List.append_assoc, List.drop_left]
have hlo := fits_lo hF hc (by omega)
have hfit := fits_canon hF hc (show a < c.row.length by omega)
have hfit1 := fits_canon hF hc ha
set cc : Int := 3 + 2 * (y : Int) with hccdef
have hcar : st.carrier = cc := laid_carrier hL hc
have hfp1 : st.stk ⟨.f, p + 1⟩ = [] := laid_front_off hL hc _ (fun j => by omega)
have hfm1 : st.stk ⟨.f, p - 1⟩ = [] := laid_front_off hL hc _ (fun j => by omega)
have honeA : (st.stk ⟨.f, p⟩).length = k := by rw [laid_front_at hL hc, hk]; rfl
have honeB : (st.stk ⟨.f, p + 2⟩).length = l := by
have := laid_front_at hL hc (a + 1)
rw [show 3 + 2 * ((a + 1 : Nat) : Int) = p + 2 by push_cast; omega, hl] at this
exact this
have hb := hL.2.2.2.1
have hr := hL.2.2.1
have ins : ∀ z, win.lo + 1 ≤ z → z + 2 ≤ win.lo + win.w → win.inside z = true := fun z h1 h2 => inside_of h1 h2
have hfp1' : st.stk ⟨.f, p + 2 - 1⟩ = [] := by rw [show p + 2 - 1 = p + 1 by omega]; exact hfp1
obtain ⟨st1, hd1, h1r, h1c, k1⟩ := loopStep_left_free win st (p + 2) hr hb (by rw [hcar]; omega) hfp1'
(ins _ (by omega) (by omega)) (ins _ (by omega) (by omega))
have hrun : runSteps st (loopStep (p + 2) false) = some st1 :=
runSteps_of_denote (denote_of_denote1 win st _ _ _ (by
intro op hop; simp [loopStep] at hop; rcases hop with h | h | h | h <;> subst h <;> trivial) hd1)
rw [sim_append win st st1 _ _ hrun, sim_loopStep_left win st (p + 2) hr hb (by rw [hcar]; omega) hfp1',
List.nil_append]
simp only [show p + 2 - 1 = p + 1 by omega] at k1
have b1 : ∀ j, st1.stk ⟨.b, j⟩ = [] := by intro j; rw [k1]; simp [hb]
set t1 := apply st1 (.rack 1) with ht1
set t2 := apply t1 (.xfer (fN (p + 1)) (bN p)) with ht2
set t3 := apply t2 (.rack 0) with ht3
set t4 := apply t3 (.rack (-1)) with ht4
set t5 := apply t4 (.xfer (bN p) (fN (p - 1))) with ht5
set t6 := apply t5 (.rack 0) with ht6
have e1r : t1.rack = 1 := rfl
have k2 : ∀ q, t2.stk q = if q = ⟨.b, p⟩ then (st.stk ⟨.f, p + 2⟩).reverse else
if q = ⟨.f, p + 1⟩ ∨ q = ⟨.f, p + 2⟩ then [] else st.stk q := by
intro q
show Function.update (Function.update st1.stk (fN (p + 1)) []) (bN p)
(xferStack (st1.stk (fN (p + 1))) (st1.stk (bN p))) q = _
simp only [upd_apply, xferStack, k1, fN, bN]
by_cases h1 : q = ⟨.b, p⟩
· subst h1; simp [hb]
· by_cases h2 : q = ⟨.f, p + 1⟩
· subst h2; simp
· by_cases h3 : q = ⟨.f, p + 2⟩
· subst h3; simp
· simp [h1, h2, h3]
have e2r : t2.rack = 1 := rfl
have e2c : t2.carrier = cc := by rw [← hcar, ← h1c]; rfl
have b2 : ∀ j, j ≠ p → t2.stk ⟨.b, j⟩ = [] := by intro j h; simp [k2, h, hb]
have e3r : t3.rack = 0 := rfl
have e4r : t4.rack = -1 := rfl
have k5 : ∀ q, t5.stk q = if q = ⟨.f, p - 1⟩ then st.stk ⟨.f, p + 2⟩ else if q = ⟨.b, p⟩ then []
else t2.stk q := by
intro q
show Function.update (Function.update t2.stk (bN p) []) (fN (p - 1))
(xferStack (t2.stk (bN p)) (t2.stk (fN (p - 1)))) q = _
simp only [upd_apply, xferStack, fN, bN]
by_cases h1 : q = ⟨.f, p - 1⟩
· subst h1; rw [k2, k2]; simp [hfm1]
· simp [h1]
have b5 : ∀ j, t5.stk ⟨.b, j⟩ = [] := by
intro j; rw [k5]; by_cases h : j = p
· subst h; simp
· simp [h, k2, hb]
have e5r : t5.rack = -1 := rfl
have g1 : step st1 (.rack 1) = .ok t1 := step_rack_of_step st1 1 (by simp [h1r])
have g2 : step t1 (.xfer (fN (p + 1)) (bN p)) = .ok t2 :=
step_xfer_of_aligned t1 _ _ (by simp [e1r, aligned, slot, fN, bN])
have g3 : step t2 (.rack 0) = .ok t3 := step_rack_of_step t2 0 (by simp [e2r])
have g4 : step t3 (.rack (-1)) = .ok t4 := step_rack_of_step t3 (-1) (by simp [e3r])
have g5 : step t4 (.xfer (bN p) (fN (p - 1))) = .ok t5 :=
step_xfer_of_aligned t4 _ _ (by simp [e4r, aligned, slot, fN, bN, show p + -1 = p - 1 by omega])
have g6 : step t5 (.rack 0) = .ok t6 := step_rack_of_step t5 0 (by simp [e5r])
have y1 : simOp win st1 (.rack 1) = [] := by
apply rackSteps_nil
intro k _
exact ⟨fun h => absurd h (by rw [h1r]; omega), fun _ => Or.inl (by simp [b1])⟩
have y2 : simOp win t1 (.xfer (fN (p + 1)) (bN p)) = [] :=
simOp_xfer_empty win t1 _ _ (by simp [ht1, apply, b1, bN])
have y3 : simOp win t2 (.rack 0) = [] := by
apply rackSteps_nil
intro k _
refine ⟨fun _ => ?_, fun h => absurd (show (0 : Int) < t2.rack by rw [e2r]; omega) h⟩
by_cases h : win.lo + (k : Int) - t2.rack = p
· right
have e : win.lo + (k : Int) = p + 1 := by rw [e2r] at h; omega
have n1 : cc ≠ p + 1 := by omega
rw [e]; simp [nY, nF, ob, e2c, k2, n1]
· left; simp only [nB]; rw [b2 _ h]; rfl
have hob3 : objsBefore win t3 p = a := by
rw [objsBefore_congr win t3 st p (fun z _ hz => by
have n1 : z ≠ p := by omega
have n2 : z ≠ p + 1 := by omega
have n3 : z ≠ p + 2 := by omega
simp [seg, nF, nB, show t3.stk = t2.stk from rfl, show t3.carrier = cc from e2c, k2, e3r, hr, hcar, hb,
n1, n2, n3]), objsBefore_canon hL hc hne win (by omega) a (by omega)]
have y4 : simOp win t3 (.rack (-1)) = [.x a true] := by
show rackSteps win t3 (-1) = _
rw [rackSteps_single win t3 (-1) (p - win.lo).toNat (by omega) (fun k hk hne =>
⟨fun _ => Or.inl (by simp only [nB, e3r]; rw [show t3.stk = t2.stk from rfl, b2 _ (by omega)]; rfl),
fun h => absurd (show (-1 : Int) < t3.rack by rw [e3r]; omega) h⟩)]
have ei : win.lo + ((p - win.lo).toNat : Int) = p := by omega
rw [ite_eq_left (by rw [e3r]; omega)]
simp only [ei, hob3]
have n1 : cc ≠ p := by omega
simp [nY, nF, nB, e3r, show t3.stk = t2.stk from rfl, show t3.carrier = cc from e2c, k2, honeA, honeB, hk0,
hl0, ob, n1]
have y5 : simOp win t4 (.xfer (bN p) (fN (p - 1))) = [] :=
simOp_xfer_empty win t4 _ _ (by simp [ht4, ht3, apply, k2, fN, hfm1])
have y6 : simOp win t5 (.rack 0) = [] := by
apply rackSteps_nil
intro k _
exact ⟨fun h => absurd h (by rw [e5r]; omega), fun _ => Or.inl (by simp [b5])⟩
rw [sim_cons win st1 _ _ _ g1, sim_cons win t1 _ _ _ g2, sim_cons win t2 _ _ _ g3, sim_cons win t3 _ _ _ g4,
sim_cons win t4 _ _ _ g5, sim_single win t5 _ _ g6, y1, y2, y3, y4, y5, y6]
simp theorem x_read : FragmentReadFor 2 isXProof
:= by
intro win c c1 st s r' M hs hne hL hc hF hrs hcs hpre hN
cases s with
| tuck _ => exact hs.elim
| merge _ => exact hs.elim
| knit _ _ => exact hs.elim
| split _ _ => exact hs.elim
| drop _ => exact hs.elim
| x a front =>
by_cases h1 : c.row.getD a .yarn = .yarn
· exact x_read_ys win c c1 st a front r' M h1 hne hL hc hF hrs hcs
by_cases h2 : c.row.getD (a + 1) .yarn = .yarn
· exact x_read_sy win c c1 st a front r' M h2 hne hL hc hF hrs hcs
cases front with
| false => exact x_read_ss_false win c c1 st a r' M ⟨h1, h2⟩ hne hL hc hF hrs hcs
| true => exact x_read_ss_true win c c1 st a r' M ⟨h1, h2⟩ hne hL hc hF hrs hcs Compile, then read: the same shorthand program
read_compile: start from the canonical layout of a row with one yarn and no empty stack. Compile a shorthand
program with compileSteps 2. Read the knitout program back with sim. The result is the shorthand program.
retract uses it for C ⋙ R = 𝟭 ShorthandObj.
| Side | Pieces |
|---|---|
| Compile | arrangeFree, arrangeTotal_two, compileTotal, stepFragment_two (one fragment proof for each kind of step) |
| Read | arrangeReadFree, fragmentRead_two (x_read, tuck_read, merge_read, knit_read, split_read, drop_read) |
fragmentRead_of_kinds:FragmentReadForfor all steps, fromFragmentReadForfor each kind of step.
namespace FormalizeKnitting.Bridge.RM
open FormalizeKnitting.Knitout FormalizeKnitting.Bridge.MR
theorem fragmentRead_of_kinds {S : Nat} (hx : FragmentReadFor S isX) (ht : FragmentReadFor S isTuck)
(hm : FragmentReadFor S isMerge) (hk : FragmentReadFor S isKnit) (hs : FragmentReadFor S isSplit)
(hd : FragmentReadFor S isDrop) : FragmentReadFor S (fun _ => True)Proof
:= by
intro win c c1 st s r' M _
cases s with
| x a f => exact hx win c c1 st _ r' M trivial
| tuck d => exact ht win c c1 st _ r' M trivial
| merge a => exact hm win c c1 st _ r' M trivial
| knit d bd => exact hk win c c1 st _ r' M trivial
| split d bd => exact hs win c c1 st _ r' M trivial
| drop a => exact hd win c c1 st _ r' M trivial theorem stepFragment_two : StepFragment 2Proof
:=
stepFragment_of_kinds (Frag.x_frag_of_kinds Frag.xYS Frag.xSY xSS) Frag.tuck_frag
(mergeFragment_of_listFacts mergeListFacts) Frag.knit_frag Frag.split_frag Frag.drop_frag theorem fragmentRead_two : FragmentReadFor 2 (fun _ => True)Proof
:=
fragmentRead_of_kinds x_read tuck_read merge_read knit_read split_read drop_read theorem read_compile (w : List Step) (c : Comp) (r' : Row) (M : BWord Label) (hy : c.row.count .yarn = 1)
(hne : NoEmpty c.row) (hcan : c.at_ = canonical 2 c.row.length) (hDB : DB c.row w = some (r', M))
(c2 : Comp) (hc2 : compileSteps 2 c w = some c2) (win : Window) (st : State) (hL : Laid c st)
(hF : Fits win c) (hN : ∀ op ∈ c2.ops, NamedIn3 win op) :
sim win st (c2.ops.drop c.ops.length) = wProof
:=
sim_compileSteps arrangeFree arrangeTotal_two stepFragment_two compileTotal arrangeReadFree fragmentRead_two
w c r' M hy hne hcan hDB c2 hc2 win st hL hF hN 𝒮 is a retract of 𝒦 over 𝒯
The three headline theorems:
| Theorem | Statement | Meaning |
|---|---|---|
retract |
C ⋙ R = 𝟭 ShorthandObj |
Compile a shorthand program, then read it back: the result is the same program. A row, laid out and read back, is the same row. |
R_drawS |
R ⋙ drawS = drawK |
A knitout program and its reading draw the same tangle. |
C_drawK |
C ⋙ drawK = drawS |
A shorthand program and its compilation draw the same tangle. The proof uses retract and R_drawS. |
R_C_obj: the canonical state of a row reads back as the row.
namespace FormalizeKnitting.Retract
open CategoryTheory FormalizeKnitting.Knitout FormalizeKnitting.Bridge
theorem eqToHom_hom {m m' n n' : Nat} (hm : m = m') (hn : n = n') (e1 : (⟨m⟩ : TangleObj) = ⟨m'⟩)
(e2 : (⟨n'⟩ : TangleObj) = ⟨n⟩) (u : MWord) (h : WF m' n' u) (h' : WF m n u) :
eqToHom e1 ≫ TangleObj.hom u h ≫ eqToHom e2 = TangleObj.hom u h'Proof
:= by
subst hm; subst hn; simp theorem R_drawS : R ⋙ drawS = drawKProof
:= by
refine CategoryTheory.Functor.ext (fun a => congrArg TangleObj.mk (rowWidth_rowL a)) (fun a b f => ?_)
show TangleObj.hom (dw (rowL a).row (Rmap f).1) (wf_dw (Rmap f)) =
eqToHom _ ≫ drawKmap f ≫ eqToHom _
have hWF := wf_good f (winHom_good f)
rw [← rowWidth_rowL a, ← rowWidth_rowL b] at hWF
refine Eq.trans ?_ (eqToHom_hom (rowWidth_rowL a) (rowWidth_rowL b) _ _ _ _ hWF).symm
refine (TangleObj.hom_eq_hom _ _).2 ?_
obtain ⟨M, hD, hiso⟩ := read_good f (winHom_good f)
rw [show dw (rowL a).row (Rmap f).1 = M from dw_eq hD, rowWidth_rowL]
exact hiso theorem R_C_obj (r : ShorthandObj) : R.obj (C.obj r) = rProof
:= by
obtain ⟨c2, hc2, hops, win, hF, hN, hG⟩ := compileOps_spec (𝟙 r : r ⟶ r)
apply ShorthandObj.ext'
rw [R_obj, rowL_eq (a := C.obj r) (win := win) (by rw [C_obj]; exact hG.1), C_obj]
have hL := laid_canon r
have hc : (⟨r.row, canonical 2 r.row.length, []⟩ : RM.Comp).at_ =
canonical 2 (⟨r.row, canonical 2 r.row.length, []⟩ : RM.Comp).row.length := rfl
have hcnt : 0 < r.row.count .yarn := by rw [r.yarn]; omega
have hlen : 0 < r.row.length := List.length_pos_of_mem (List.count_pos_iff.mp hcnt)
have hlo : win.lo ≤ 1 := RM.Frag.fits_lo hF hc hlen
have hhi : 3 + 2 * (r.row.length : Int) ≤ win.lo + win.w := by
have := RM.Frag.fits_canon hF hc (show r.row.length - 1 < r.row.length by omega)
omega
set n0 : Nat := (3 + 2 * (r.row.length : Int) - win.lo).toNat with hn0
have e0 : (n0 : Int) = 3 + 2 * (r.row.length : Int) - win.lo := by rw [hn0]; omega
rw [show win.w = n0 + (win.w - n0) by omega, MR.rowOf_add,
RM.rowOf_canon_upto hL hc r.ne win.lo (by omega) r.row.length (le_refl _), List.take_length]
have hrest : MR.rowOf (lift (canonL r)) (win.lo + n0) (win.w - n0) = [] := by
unfold MR.rowOf
rw [List.flatMap_eq_nil_iff]
intro k hk
simp at hk
obtain ⟨m, hm, rfl⟩ := hk
exact RM.seg_canon_off hL hc _ (fun j hj => by simp only at hj; omega)
rw [hrest, List.append_nil] theorem retract : C ⋙ R = 𝟭 ShorthandObjProof
:= by
refine CategoryTheory.Functor.ext (h_obj := fun r => R_C_obj r) ?_
intro a b w
apply Subtype.ext
obtain ⟨c2, hc2, hops, win, hF, hN, hG⟩ := compileOps_spec (show ShorthandProgram a b from w)
obtain ⟨M, hDB⟩ := CompileC.db_of_shom (show ShorthandProgram a b from w)
have hG' : Good win (lift (C.obj a).t) (show KnitoutProgram (C.obj a) (C.obj b) from C.map w).1 := by
rw [C_map_val, C_obj, ← hops]; exact hG
have hread := RM.read_compile (show ShorthandProgram a b from w).1 ⟨a.row, canonical 2 a.row.length, []⟩ b.row M a.yarn
a.ne rfl hDB c2 hc2 win (lift (canonL a)) (laid_canon a) hF hN
simp only [List.length_nil, List.drop_zero] at hread
rw [ShorthandObj.hom_val_comp, ShorthandObj.hom_val_comp, ShorthandObj.eqToHom_val, ShorthandObj.eqToHom_val, List.nil_append, List.append_nil]
show (show ShorthandProgram (R.obj (C.obj a)) (R.obj (C.obj b)) from R.map (C.map w)).1 = (show ShorthandProgram a b from w).1
rw [R_map_val (C.map w) hG', C_map_val, C_obj, ← hops, hread] theorem C_drawK : C ⋙ drawK = drawSProof
:= by
rw [← R_drawS, ← Functor.assoc, retract, Functor.id_comp]