Lemmas about the tracked knitout machine
Facts about apply, step1 and the stacks: validity, erasure, where loops land, distinct identities, order kept.
points_xfer fixes the stack order under a transfer: xferStack.
pairKept_step1 holds for each instruction valid in Lin's sense. So it also serves the machine step:
shared_order_kept.
Validity
theorem validCrate_validLin {s : State} {op : Op} (h : validCrate s op) : validLin s op
Proof
:= by
unfold validCrate crateRefusal1 at h
unfold validLin
cases hl : linRefusal (erase s) op with
| none => rfl
| some r => simp [hl] at h
theorem step1_ok {s s' : State} {op : Op} : step1 s op = .ok s' ↔ validCrate s op ∧ s' = apply s op
Proof
:= by
unfold step1 validCrate
cases crateRefusal1 s op with
| none => simp [eq_comm]
| some r => simp
theorem stepLin_some {s s' : State} {op : Op} : stepLin s op = some s' ↔ validLin s op ∧ s' = apply s op
Proof
:= by
unfold stepLin validLin
cases linRefusal (erase s) op with
| none => simp [eq_comm]
| some r => simp
theorem aligned_ne {r : Int} {a b : Needle} (h : aligned r a b = true) : a ≠ b
Proof
:= by
rintro rfl
simp [aligned] at h
theorem validLin_xfer {s : State} {a b : Needle} (h : validLin s (.xfer a b)) : aligned s.rack a b = true
Proof
:= by
simp only [validLin, linRefusal] at h
split_ifs at h with ha
· exact ha
theorem validLin_split {s : State} {d : Dir} {n m : Needle} (h : validLin s (.split d n m)) :
aligned s.rack n m = true
Proof
:= by
simp only [validLin, linRefusal] at h
split_ifs at h with h1 h2
· exact h2
theorem validLin_knit {s : State} {d : Dir} {n : Needle} (h : validLin s (.knit d n)) : s.stk n ≠ []
Proof
:= by
simp only [validLin, linRefusal] at h
split_ifs at h with h1 h2
simpa [erase] using h2
theorem validLin_drop {s : State} {n : Needle} (h : validLin s (.drop n)) : s.stk n ≠ []
Proof
:= by
simp only [validLin, linRefusal] at h
split_ifs at h with h1
simpa [erase] using h1
The erasure commutes with the step
theorem erase_apply (s : State) (op : Op) : erase (apply s op) = linApply (erase s) op
Proof
:= by
cases op with
| xfer a b =>
simp only [erase, apply, linApply, xferStack, LinState.mk.injEq, true_and, and_true]
funext n; simp only [update_apply]; split_ifs <;> simp
| rack r => rfl
| knit d n =>
simp only [erase, apply, linApply, LinState.mk.injEq, true_and, and_true]
funext m; simp only [update_apply]; split_ifs <;> simp
| tuck d n =>
simp only [erase, apply, linApply, LinState.mk.injEq, true_and, and_true]
funext m; simp only [update_apply]; split_ifs <;> simp_all
| split d n m =>
simp only [erase, apply, linApply, xferStack, LinState.mk.injEq, true_and, and_true]
funext k; simp only [update_apply]; split_ifs <;> simp
| miss d n => rfl
| drop n =>
simp only [erase, apply, linApply, LinState.mk.injEq, true_and, and_true]
funext m; simp only [update_apply]; split_ifs <;> simp
theorem erase_stepLin (s : State) (op : Op) : (stepLin s op).map erase = linStep (erase s) op
Proof
:= by
unfold stepLin linStep
cases linRefusal (erase s) op with
| none => simp [erase_apply]
| some r => rfl
New loops and moved loops go on top
def top (l : List Nat) : Option Nat := l.head?
theorem tuck_stk (s : State) (d : Dir) (n : Needle) : (apply s (.tuck d n)).stk n = s.fresh :: s.stk n
Proof
:= by
simp [apply]
theorem tuck_top (s : State) (d : Dir) (n : Needle) : top ((apply s (.tuck d n)).stk n) = some s.fresh
Proof
:= by
simp [top, apply]
theorem tuck_other (s : State) (d : Dir) (n m : Needle) (h : m ≠ n) : (apply s (.tuck d n)).stk m = s.stk m
Proof
:= by
simp [apply, h]
theorem xfer_dst (s : State) (a b : Needle) : (apply s (.xfer a b)).stk b = (s.stk a).reverse ++ s.stk b
Proof
:= by
simp [apply, xferStack]
theorem xfer_src (s : State) (a b : Needle) (h : validLin s (.xfer a b)) : (apply s (.xfer a b)).stk a = []
Proof
:= by
have hne := aligned_ne (validLin_xfer h)
simp [apply, hne]
theorem xfer_other (s : State) (a b m : Needle) (ha : m ≠ a) (hb : m ≠ b) :
(apply s (.xfer a b)).stk m = s.stk m
Proof
:= by
simp [apply, ha, hb]
theorem xfer_top (s : State) (a b : Needle) (h : s.stk a ≠ []) :
top ((apply s (.xfer a b)).stk b) = (s.stk a).getLast?
Proof
:= by
rw [xfer_dst]
cases hs : (s.stk a).reverse with
| nil => simp_all
| cons x xs =>
have : (s.stk a).getLast? = some x := by
rw [← List.head?_reverse, hs]; rfl
simp [top, this]
theorem split_src (s : State) (d : Dir) (n m : Needle) (h : validLin s (.split d n m)) :
(apply s (.split d n m)).stk n = [s.fresh]
Proof
:= by
have hne := aligned_ne (validLin_split h)
simp [apply, hne]
theorem split_dst (s : State) (d : Dir) (n m : Needle) :
(apply s (.split d n m)).stk m = (s.stk n).reverse ++ s.stk m
Proof
:= by
simp [apply, xferStack]
theorem drop_stk (s : State) (n : Needle) : (apply s (.drop n)).stk n = []
Proof
:= by
simp [apply]
theorem knit_stk (s : State) (d : Dir) (n : Needle) : (apply s (.knit d n)).stk n = [s.fresh]
Proof
:= by
simp [apply]
No instruction reorders a stack in place
inductive StackChange (s : State) (n : Needle) : List Nat → Prop
| same : StackChange s n (s.stk n)
| push : StackChange s n (s.fresh :: s.stk n)
| cleared : StackChange s n []
| renewed : StackChange s n [s.fresh]
| received (m : Needle) : aligned s.rack m n = true → StackChange s n (xferStack (s.stk m) (s.stk n))
theorem no_reorder (s : State) (op : Op) (h : validLin s op) (n : Needle) :
StackChange s n ((apply s op).stk n)
Proof
:= by
cases op with
| xfer a b =>
have ha := validLin_xfer h
have hne := aligned_ne ha
by_cases hb : n = b
· subst hb; simpa [apply] using StackChange.received a ha
· by_cases hn : n = a
· subst hn; simpa [apply, hb] using StackChange.cleared (s := s) (n := n)
· simpa [apply, hb, hn] using StackChange.same (s := s) (n := n)
| rack r => exact .same
| knit d m =>
by_cases hn : n = m
· subst hn; simpa [apply] using StackChange.renewed (s := s) (n := n)
· simpa [apply, hn] using StackChange.same (s := s) (n := n)
| tuck d m =>
by_cases hn : n = m
· subst hn; simpa [apply] using StackChange.push (s := s) (n := n)
· simpa [apply, hn] using StackChange.same (s := s) (n := n)
| split d a b =>
have ha := validLin_split h
have hne := aligned_ne ha
by_cases hb : n = b
· subst hb; simpa [apply] using StackChange.received a ha
· by_cases hn : n = a
· subst hn; simpa [apply, hb] using StackChange.renewed (s := s) (n := n)
· simpa [apply, hb, hn] using StackChange.same (s := s) (n := n)
| miss d m => exact .same
| drop m =>
by_cases hn : n = m
· subst hn; simpa [apply] using StackChange.cleared (s := s) (n := n)
· simpa [apply, hn] using StackChange.same (s := s) (n := n)
theorem suffix_or_gone (s : State) (op : Op) (h : validLin s op) (n : Needle) :
s.stk n <:+ (apply s op).stk n ∨ (apply s op).stk n ⊆ [s.fresh]
Proof
:= by
have hc := no_reorder s op h n
generalize (apply s op).stk n = l at hc ⊢
rcases hc with _ | _ | _ | _ | ⟨m, _⟩
· exact Or.inl (List.suffix_refl _)
· exact Or.inl (List.suffix_cons _ _)
· exact Or.inr (List.nil_subset _)
· exact Or.inr (List.Subset.refl _)
· exact Or.inl (List.suffix_append _ _)
Transfer is the identity on the row of points
theorem seg1_xfer (s : State) (a b : Needle) (h : aligned s.rack a b = true) (i : Int) :
seg1 (apply s (.xfer a b)) i = seg1 s i
Proof
:= by
obtain ⟨ba, xa⟩ := a
obtain ⟨bb, xb⟩ := b
cases ba <;> cases bb <;> simp [aligned, slot] at h
· simp only [seg1, apply, leftToRight, xferStack, update_apply, Needle.mk.injEq]
by_cases hi : i = xa
· subst hi
have : i - s.rack = xb := by omega
simp [this]
by_cases hc : s.carrier = i <;> simp [hc]
· have : i - s.rack ≠ xb := by omega
simp [hi, this]
by_cases hc : s.carrier = i <;> simp [hc]
· simp only [seg1, apply, leftToRight, xferStack, update_apply, Needle.mk.injEq]
by_cases hi : i = xb
· subst hi
have : i - s.rack = xa := by omega
simp [this]
by_cases hc : s.carrier = i <;> simp [hc]
· have : i - s.rack ≠ xa := by omega
simp [hi, this]
by_cases hc : s.carrier = i <;> simp [hc]
theorem points_xfer (s : State) (a b : Needle) (h : validLin s (.xfer a b)) (lo : Int) (w : Nat) :
points (apply s (.xfer a b)) lo w = points s lo w
Proof
:= by
simp only [points, seg1_xfer s a b (validLin_xfer h)]
theorem cellRowWidth_map_obj (l : List Pt) : cellRowWidth (l.map Pt.obj) = (l.flatMap Pt.legs).length
Proof
:= by
induction l with
| nil => rfl
| cons p l ih =>
cases p <;> simp [cellRowWidth, Pt.obj, Pt.legs, CellObj.width] at ih ⊢ <;> omega
theorem cellRowWidth_rowOf1 (s : State) (lo : Int) (w : Nat) : cellRowWidth (rowOf1 s lo w) = (points2 s lo w).length
Proof
:=
cellRowWidth_map_obj _
Loop identities stay distinct
theorem mem_update {f : Needle → List Nat} {a n : Needle} {v : List Nat} {x : Nat} :
x ∈ update f a v n ↔ (n = a ∧ x ∈ v) ∨ (n ≠ a ∧ x ∈ f n)
Proof
:= by
by_cases h : n = a
· subst h; simp
· simp [h]
theorem distinct_apply {s : State} {op : Op} (hD : Distinct s) (h : validLin s op) : Distinct (apply s op)
Proof
:= by
obtain ⟨hnd, hu, hlt⟩ := hD
cases op with
| rack r => exact ⟨hnd, hu, hlt⟩
| miss d n => exact ⟨hnd, hu, hlt⟩
| drop n =>
refine ⟨fun m => ?_, fun m₁ m₂ x h₁ h₂ => ?_, fun m x hx => ?_⟩
· by_cases hm : m = n
· subst hm; simp [apply]
· simpa [apply, hm] using hnd m
· simp only [apply, mem_update, List.not_mem_nil, and_false, false_or] at h₁ h₂
exact hu _ _ x h₁.2 h₂.2
· simp only [apply, mem_update, List.not_mem_nil, and_false, false_or] at hx
exact hlt _ _ hx.2
| knit d n =>
refine ⟨fun m => ?_, fun m₁ m₂ x h₁ h₂ => ?_, fun m x hx => ?_⟩
· by_cases hm : m = n
· subst hm; simp [apply]
· simpa [apply, hm] using hnd m
· simp only [apply, mem_update, List.mem_singleton] at h₁ h₂
rcases h₁ with ⟨rfl, rfl⟩ | ⟨h₁n, h₁⟩ <;> rcases h₂ with ⟨rfl, h₂⟩ | ⟨h₂n, h₂⟩
· rfl
· exact absurd (hlt _ _ h₂) (by omega)
· subst h₂; exact absurd (hlt _ _ h₁) (by omega)
· exact hu _ _ x h₁ h₂
· show x < s.fresh + 1
simp only [apply, mem_update, List.mem_singleton] at hx
rcases hx with ⟨_, rfl⟩ | ⟨_, hx⟩
· omega
· have := hlt _ _ hx; omega
| tuck d n =>
refine ⟨fun m => ?_, fun m₁ m₂ x h₁ h₂ => ?_, fun m x hx => ?_⟩
· by_cases hm : m = n
· subst hm
simp only [apply, update_self, List.nodup_cons]
exact ⟨fun hf => absurd (hlt _ _ hf) (by omega), hnd m⟩
· simpa [apply, hm] using hnd m
· simp only [apply, mem_update, List.mem_cons] at h₁ h₂
rcases h₁ with ⟨rfl, h₁ | h₁⟩ | ⟨h₁n, h₁⟩ <;> rcases h₂ with ⟨rfl, h₂ | h₂⟩ | ⟨h₂n, h₂⟩
all_goals first
| rfl
| (subst_vars; first | exact absurd (hlt _ _ h₁) (by omega) | exact absurd (hlt _ _ h₂) (by omega))
| exact hu _ _ x h₁ h₂
· show x < s.fresh + 1
simp only [apply, mem_update, List.mem_cons] at hx
rcases hx with ⟨_, rfl | hx⟩ | ⟨_, hx⟩
· omega
· have := hlt _ _ hx; omega
· have := hlt _ _ hx; omega
| xfer a b =>
have hne := aligned_ne (validLin_xfer h)
refine ⟨fun m => ?_, fun m₁ m₂ x h₁ h₂ => ?_, fun m x hx => ?_⟩
· by_cases hb : m = b
· subst hb
simp only [apply, update_self, xferStack, List.nodup_append, List.nodup_reverse, List.mem_reverse]
refine ⟨hnd a, hnd m, fun x hx y hy hxy => hne ?_⟩
subst hxy; exact hu _ _ x hx hy
· by_cases ha : m = a
· subst ha; simp [apply, hb]
· simpa [apply, ha, hb] using hnd m
· simp only [apply, xferStack, mem_update, List.mem_append, List.mem_reverse, List.not_mem_nil,
and_false, false_or] at h₁ h₂
rcases h₁ with ⟨rfl, h₁ | h₁⟩ | ⟨h₁b, h₁a, h₁⟩ <;> rcases h₂ with ⟨rfl, h₂ | h₂⟩ | ⟨h₂b, h₂a, h₂⟩
all_goals first
| rfl
| exact hu _ _ x h₁ h₂
| exact absurd (hu _ _ x h₁ h₂) ‹_›
| exact absurd (hu _ _ x h₁ h₂).symm ‹_›
· simp only [apply, xferStack, mem_update, List.mem_append, List.mem_reverse, List.not_mem_nil,
and_false, false_or] at hx
rcases hx with ⟨_, hx | hx⟩ | ⟨_, _, hx⟩ <;> exact hlt _ _ hx
| split d a b =>
have hne := aligned_ne (validLin_split h)
refine ⟨fun m => ?_, fun m₁ m₂ x h₁ h₂ => ?_, fun m x hx => ?_⟩
· by_cases hb : m = b
· subst hb
simp only [apply, update_self, xferStack, List.nodup_append, List.nodup_reverse, List.mem_reverse]
refine ⟨hnd a, hnd m, fun x hx y hy hxy => hne ?_⟩
subst hxy; exact hu _ _ x hx hy
· by_cases ha : m = a
· subst ha; simp [apply, hb]
· simpa [apply, ha, hb] using hnd m
· simp only [apply, xferStack, mem_update, List.mem_append, List.mem_reverse, List.mem_singleton] at h₁ h₂
rcases h₁ with ⟨rfl, h₁ | h₁⟩ | ⟨h₁b, ⟨rfl, rfl⟩ | ⟨h₁a, h₁⟩⟩ <;>
rcases h₂ with ⟨rfl, h₂ | h₂⟩ | ⟨h₂b, ⟨rfl, h₂⟩ | ⟨h₂a, h₂⟩⟩
all_goals first
| rfl
| (subst_vars; first | exact absurd (hlt _ _ h₁) (by omega) | exact absurd (hlt _ _ h₂) (by omega))
| exact hu _ _ x h₁ h₂
| exact absurd (hu _ _ x h₁ h₂) ‹_›
| exact absurd (hu _ _ x h₁ h₂).symm ‹_›
· show x < s.fresh + 1
simp only [apply, mem_update, xferStack, List.mem_append, List.mem_reverse, List.mem_singleton] at hx
rcases hx with ⟨_, hx | hx⟩ | ⟨_, ⟨_, rfl⟩ | ⟨_, hx⟩⟩
· have := hlt _ _ hx; omega
· have := hlt _ _ hx; omega
· omega
· have := hlt _ _ hx; omega
theorem distinct_empty (z : Int) : Distinct (empty z)
Proof
:= by
refine ⟨fun _ => List.nodup_nil, fun _ _ x h _ => ?_, fun _ x h => ?_⟩ <;> simp [empty] at h
theorem Reachable.distinct {s : State} (h : Reachable s) : Distinct s
Proof
:= by
induction h with
| empty z => exact distinct_empty z
| step op _ hv ih => exact distinct_apply ih hv
theorem knit_takes_all {s : State} (hD : Distinct s) (d : Dir) (n : Needle) :
∀ x ∈ s.stk n, ∀ m, x ∉ (apply s (.knit d n)).stk m
Proof
:= by
intro x hx m hm
obtain ⟨_, hu, hlt⟩ := hD
simp only [apply, mem_update, List.mem_singleton] at hm
rcases hm with ⟨_, rfl⟩ | ⟨hmn, hm⟩
· have := hlt _ _ hx; omega
· exact hmn (hu _ _ x hm hx)
Worked examples ("Tower of Hanoi")
- From the empty machine, tuck loop 0 on
f0 and loop 1 on f1.
- Either loop can go on top of the other: move it to the back bed, rack, move it back (
hanoi₁, hanoi₂).
- A transfer of the stack of two loops to the back bed turns the stack over (
hanoi₃). The loop that was below
is then on top.
def f (i : Int) : Needle := ⟨.f, i⟩
def b (i : Int) : Needle := ⟨.b, i⟩
def tuckTwo : List Op := [.tuck .R (f 0), .tuck .R (f 1)]
def hanoi₁prog : List Op := tuckTwo ++ [.xfer (f 1) (b 1), .rack (-1), .xfer (b 1) (f 0)]
def hanoi₂prog : List Op := tuckTwo ++ [.xfer (f 0) (b 0), .rack 1, .xfer (b 0) (f 1)]
def hanoi₃prog : List Op := hanoi₁prog ++ [.xfer (f 0) (b 1)]
theorem hanoi₁ : (run (empty 0) hanoi₁prog).toOption.map (fun t => t.stk (f 0)) = some [1, 0]
Proof
:= by decide
theorem hanoi₂ : (run (empty 0) hanoi₂prog).toOption.map (fun t => t.stk (f 1)) = some [0, 1]
Proof
:= by decide
theorem hanoi₃ : (run (empty 0) hanoi₃prog).toOption.map (fun t => t.stk (b 1)) = some [0, 1]
Proof
:= by decide
Loops that share a needle keep their left-to-right order
- Two loops
x, y are on one needle, x left of y in the row ([x, y] <+ leftToRight s n).
- After a valid program, both are on one needle with
x left of y, or both are gone.
- Transfers move whole stacks. So a stack on the front bed is never turned over in place.
- A stack is turned over (as a list) only when it goes to the other bed.
def lrList : Bed → List Nat → List Nat
| .f, l => l.reverse
| .b, l => l
theorem leftToRight_eq (s : State) (n : Needle) : leftToRight s n = lrList n.bed (s.stk n)
Proof
:= by
unfold leftToRight lrList; cases n.bed <;> rfl
theorem lr_recv {p : List Nat} {ba bb : Bed} (hne : ba ≠ bb) (la lb : List Nat) :
(p <+ lrList ba la → p <+ lrList bb (la.reverse ++ lb)) ∧ (p <+ lrList bb lb → p <+ lrList bb (la.reverse ++ lb))
Proof
:= by
cases ba <;> cases bb <;> simp only [ne_eq, not_true_eq_false, reduceCtorEq, not_false_eq_true] at hne <;>
simp only [lrList, List.reverse_append, List.reverse_reverse]
· exact ⟨fun h => h.trans (List.sublist_append_left _ _), fun h => h.trans (List.sublist_append_right _ _)⟩
· exact ⟨fun h => h.trans (List.sublist_append_right _ _), fun h => h.trans (List.sublist_append_left _ _)⟩
theorem lr_push {p : List Nat} (bd : Bed) (z : Nat) (l : List Nat) (h : p <+ lrList bd l) : p <+ lrList bd (z :: l)
Proof
:= by
cases bd
· simp only [lrList, List.reverse_cons]; exact h.trans (List.sublist_append_left _ _)
· exact h.trans (List.sublist_cons_self _ _)
theorem mem_of_lr {p : List Nat} {bd : Bed} {l : List Nat} {x : Nat} (h : p <+ lrList bd l) (hx : x ∈ p) : x ∈ l
Proof
:= by
cases bd
· simpa [lrList] using h.subset hx
· exact h.subset hx
def PairKept (s : State) (x y : Nat) : Prop :=
(∃ n, [x, y] <+ leftToRight s n) ∨ (x < s.fresh ∧ y < s.fresh ∧ ∀ n, x ∉ s.stk n ∧ y ∉ s.stk n)
theorem mem_apply {s : State} {op : Op} (hv : validLin s op) {n : Needle} {x : Nat}
(hx : x ∈ (apply s op).stk n) : x = s.fresh ∨ ∃ m, x ∈ s.stk m
Proof
:= by
have hc := no_reorder s op hv n
generalize (apply s op).stk n = l at hc hx
rcases hc with _ | _ | _ | _ | ⟨m, _⟩
· exact Or.inr ⟨n, hx⟩
· rcases List.mem_cons.1 hx with h | h
· exact Or.inl h
· exact Or.inr ⟨n, h⟩
· simp at hx
· exact Or.inl (List.mem_singleton.1 hx)
· simp only [xferStack, List.mem_append, List.mem_reverse] at hx
rcases hx with h | h
· exact Or.inr ⟨m, h⟩
· exact Or.inr ⟨n, h⟩
theorem gone_step1 {s : State} {op : Op} (hv : validLin s op) {x : Nat} (hx : x < s.fresh)
(hg : ∀ n, x ∉ s.stk n) : x < (apply s op).fresh ∧ ∀ n, x ∉ (apply s op).stk n
Proof
:= by
constructor
· cases op <;> simp only [apply] <;> omega
· intro n hn
rcases mem_apply hv hn with h | ⟨m, h⟩
· omega
· exact hg m h
theorem pairKept_step1 {s : State} {op : Op} {x y : Nat} (hD : Distinct s) (hv : validLin s op)
(h : PairKept s x y) : PairKept (apply s op) x y
Proof
:= by
rcases h with ⟨n, hn⟩ | ⟨hx, hy, hg⟩
swap
· have gx := gone_step1 hv hx (fun n => (hg n).1)
have gy := gone_step1 hv hy (fun n => (hg n).2)
exact Or.inr ⟨gx.1, gy.1, fun n => ⟨gx.2 n, gy.2 n⟩⟩
obtain ⟨_, hu, hlt⟩ := hD
rw [leftToRight_eq] at hn
have hxn : x ∈ s.stk n := mem_of_lr hn (by simp)
have hyn : y ∈ s.stk n := mem_of_lr hn (by simp)
have keep : ∀ s', s'.stk n = s.stk n → (∃ m, [x, y] <+ leftToRight s' m) :=
fun s' e => ⟨n, by rw [leftToRight_eq, e]; exact hn⟩
cases op with
| rack r => exact Or.inl (keep _ rfl)
| miss d t => exact Or.inl (keep _ rfl)
| tuck d t =>
by_cases ht : n = t
· subst ht; exact Or.inl ⟨n, by rw [leftToRight_eq, tuck_stk]; exact lr_push _ _ _ hn⟩
· exact Or.inl (keep _ (tuck_other s d t n ht))
| knit d t =>
by_cases ht : n = t
· subst ht
refine Or.inr ⟨by simp only [apply]; have := hlt _ _ hxn; omega,
by simp only [apply]; have := hlt _ _ hyn; omega, fun m => ⟨fun hm => ?_, fun hm => ?_⟩⟩
· simp only [apply, mem_update, List.mem_singleton] at hm
rcases hm with ⟨_, rfl⟩ | ⟨hmn, hm⟩
· have := hlt _ _ hxn; omega
· exact hmn (hu _ _ x hm hxn)
· simp only [apply, mem_update, List.mem_singleton] at hm
rcases hm with ⟨_, rfl⟩ | ⟨hmn, hm⟩
· have := hlt _ _ hyn; omega
· exact hmn (hu _ _ y hm hyn)
· exact Or.inl (keep _ (by simp [apply, ht]))
| drop t =>
by_cases ht : n = t
· subst ht
refine Or.inr ⟨by simp only [apply]; have := hlt _ _ hxn; omega,
by simp only [apply]; have := hlt _ _ hyn; omega, fun m => ⟨fun hm => ?_, fun hm => ?_⟩⟩
· simp only [apply, mem_update, List.not_mem_nil, and_false, false_or] at hm
exact hm.1 (hu _ _ x hm.2 hxn)
· simp only [apply, mem_update, List.not_mem_nil, and_false, false_or] at hm
exact hm.1 (hu _ _ y hm.2 hyn)
· exact Or.inl (keep _ (by simp [apply, ht]))
| xfer a b =>
have hal := validLin_xfer hv
have hne := aligned_ne hal
have hbed : a.bed ≠ b.bed := by simp [aligned] at hal; exact hal.1
by_cases hb : n = b
· subst hb
exact Or.inl ⟨n, by rw [leftToRight_eq, xfer_dst]; exact (lr_recv hbed _ _).2 hn⟩
· by_cases ha : n = a
· subst ha
exact Or.inl ⟨b, by rw [leftToRight_eq, xfer_dst]; exact (lr_recv hbed _ _).1 hn⟩
· exact Or.inl (keep _ (xfer_other s a b n ha hb))
| split d a b =>
have hal := validLin_split hv
have hne := aligned_ne hal
have hbed : a.bed ≠ b.bed := by simp [aligned] at hal; exact hal.1
by_cases hb : n = b
· subst hb
exact Or.inl ⟨n, by rw [leftToRight_eq, split_dst]; exact (lr_recv hbed _ _).2 hn⟩
· by_cases ha : n = a
· subst ha
exact Or.inl ⟨b, by rw [leftToRight_eq, split_dst]; exact (lr_recv hbed _ _).1 hn⟩
· exact Or.inl (keep _ (by simp [apply, ha, hb]))
theorem run_ok_cons {s t : State} {op : Op} {ops : List Op} (h : run s (op :: ops) = .ok t) :
validCrate s op ∧ run (apply s op) ops = .ok t
Proof
:= by
unfold run at h
cases hs : step1 s op with
| error e => simp [hs] at h
| ok s' =>
rw [step1_ok] at hs
obtain ⟨hv, rfl⟩ := hs
refine ⟨hv, ?_⟩
rw [step1_ok.2 ⟨hv, rfl⟩] at h
cases hr : run (apply s op) ops with
| error e => simp [hr] at h
| ok t' => simp only [hr] at h; rw [h]
theorem shared_order_kept1 {s t : State} {ops : List Op} {n : Needle} {x y : Nat} (hs : Reachable s)
(hrun : run s ops = .ok t) (hxy : [x, y] <+ leftToRight s n) :
(∃ m, [x, y] <+ leftToRight t m) ∨ (∀ m, x ∉ t.stk m ∧ y ∉ t.stk m)
Proof
:= by
suffices key : ∀ (ops : List Op) (s : State), Reachable s → PairKept s x y → run s ops = .ok t → PairKept t x y by
rcases key ops s hs (Or.inl ⟨n, hxy⟩) hrun with h | ⟨_, _, h⟩
· exact Or.inl h
· exact Or.inr h
intro ops
induction ops with
| nil => intro s _ hp hr; simp only [run, Except.ok.injEq] at hr; exact hr ▸ hp
| cons op ops ih =>
intro s hs hp hr
obtain ⟨hv, hr'⟩ := run_ok_cons hr
have hl := validCrate_validLin hv
exact ih _ (hs.step op hl) (pairKept_step1 hs.distinct hl hp) hr'