theoremprefixTime_le (v : Nat) (rs : ListRow) :
prefixTime v rs ≤3 * (encodeWords (rs.flatMap rowWords)).length + 2:=by
proof · 7
lines
induction rs with
| nil => simp only [prefixTime, List.flatMap_nil, encodeWords, List.length_nil,
Nat.mul_zero, Nat.zero_add, le_refl]
| cons r rs ih =>have hr := rowTime_le v r
simp only [prefixTime, List.flatMap_cons, encodeWords_append, List.length_append]
omega
have h := congrArg (funxs : ListRow=> encodeWords (xs.flatMap rowWords))
(List.take_append_drop k rs)
simpa only [List.flatMap_append, encodeWords_append] using h.symm
theoremrankTrace (n m v k : Nat) (rs : ListRow) (hk : k ≤ rs.length)
(hwidth : ∀ r ∈ rs, r.2.length = 4097) (querySuffix count : ListBool)
(ambient : σ) (register : OptionBool) :
(advance (TM2.step (rankProgram (σ := σ))))^[rankSteps n m v k rs querySuffix]
(some ⟨some .queryFirst, ((ambient, false), register),
memory (inputWord n m rs) [] (queryWord v k querySuffix) [] count []⟩) =
some ⟨none, ((ambient, false), none),
memory (inputWord n m rs) (encodeWords ((rs.drop k).flatMap rowWords))
(queryWord v k querySuffix) []
(List.replicate (rowsHits v (rs.take k)) true ++ count)
(encodeWord 0 ++ querySuffix)⟩ :=by
proof · 46
lines
have hquery :=MachineCopy.copyTrace Tape.target Tape.spare Tape.scratch
(by decide) (by decide) (by decide) falseRankLabel.queryFirst RankLabel.querySecond
(some RankLabel.querySkip) rankProgram rfl rfl
(memory (inputWord n m rs) [] (queryWord v k querySuffix) [] count [])
rfl (ambient, false) register
simp only [memory_target, memory_spare, List.append_nil, update_memory_spare] at hquery
have hquerySkip :=MachineLookup.discardTrace Tape.spare RankLabel.querySkip RankLabel.tableFirst
rankProgram rfl
(memory (inputWord n m rs) [] (queryWord v k querySuffix) [] count (queryWord v k querySuffix))
v (encodeWord k ++ querySuffix) rfl (ambient, false) none
simp only [update_memory_spare] at hquerySkip
have hcopy :=MachineCopy.copyTrace Tape.original Tape.work Tape.scratch
(by decide) (by decide) (by decide) falseRankLabel.tableFirst RankLabel.tableSecond
(some RankLabel.headerFirst) rankProgram rfl rfl
(memory (inputWord n m rs) [] (queryWord v k querySuffix) [] count
(encodeWord k ++ querySuffix)) rfl (ambient, false) none
simp only [memory_original, memory_work, List.append_nil, update_memory_work] at hcopy
have hfirst :=MachineLookup.discardTrace Tape.work RankLabel.headerFirst RankLabel.headerSecond
rankProgram rfl
(memory (inputWord n m rs) (inputWord n m rs) (queryWord v k querySuffix) [] count
(encodeWord k ++ querySuffix))
n (encodeWord m ++ encodeWords (rs.flatMap rowWords))
(inputWord_eq n m rs) (ambient, false) none
simp only [update_memory_work] at hfirst
have hsecond :=MachineLookup.discardTrace Tape.work RankLabel.headerSecond RankLabel.row
rankProgram rfl
(memory (inputWord n m rs) (encodeWord m ++ encodeWords (rs.flatMap rowWords))
(queryWord v k querySuffix) [] count (encodeWord k ++ querySuffix))
m (encodeWords (rs.flatMap rowWords)) rfl (ambient, false) none
simp only [update_memory_work] at hsecond
have hprefix := prefixTrace (rs.take k)
(by intro r hr; exact hwidth r (List.mem_of_mem_take hr)) v
(inputWord n m rs) (encodeWords ((rs.drop k).flatMap rowWords))
(encodeWord k ++ querySuffix) querySuffix count ambient none
havehtake : (rs.take k).length = k :=by
simp only [List.length_take, Nat.min_eq_left hk]
rw [htake, ← encoded_rows_split rs k] at hprefix
rw [show rankSteps n m v k rs querySuffix = prefixTime v (rs.take k) +
((m + 1) + ((n + 1) + (2 * ((inputWord n m rs).length + 1) +
((v + 1) + 2 * ((queryWord v k querySuffix).length + 1))))) by
unfold rankSteps; omega,
Function.iterate_add_apply _ (prefixTime v (rs.take k)),
Function.iterate_add_apply _ (m + 1), Function.iterate_add_apply _ (n + 1),
Function.iterate_add_apply _ (2 * ((inputWord n m rs).length + 1)),
Function.iterate_add_apply _ (v + 1), hquery, hquerySkip, hcopy, hfirst, hsecond]
exact hprefix
theoremrankSteps_le (n m v k : Nat) (rs : ListRow) (querySuffix : ListBool) :
rankSteps n m v k rs querySuffix ≤5 * (inputWord n m rs).length + 3 * (queryWord v k querySuffix).length + 6:=by
proof · 9
lines
have hprefix := prefixTime_le v (rs.take k)
have hsplit := congrArg List.length (encoded_rows_split rs k)
simp only [List.length_append] at hsplit
have hinput := inputWord_length n m rs
havehquery : v + 1≤ (queryWord v k querySuffix).length :=by
simp only [queryWord, List.length_append, encodeWord_length]
omega
unfold rankSteps
omega
theoremidxOf_filter_prefix {α : Type*} [BEq α] [LawfulBEq α]
(p : α →Bool) (a : α) (hp : p a = true) :
∀xs : List α, a ∈ xs →
(xs.filter p).idxOf a = ((xs.take (xs.idxOf a)).filter p).length :=by
proof · 12
lines
classical
intro xs
induction xs with
| nil => intro ha; simp at ha
| cons b xs ih =>
intro ha
by_cases hba : b = a
· subst b
simp [hp]
· haveha' : a ∈ xs :=by simpa [hba, Ne.symm hba] using ha
cases hb : p b <;>
simp [hba, hb, ih ha']
havehk : e.val.val ≤ (tableRows t).length :=by
rw [tableRows_length]
exact e.val.isLt.le
have h := rankTrace t.vertices t.darts v.val e.val.val (tableRows t) hk
(tableRows_width t) querySuffix (encodeWord 0 ++ countSuffix) ambient register
rw [tableRows_input, ← cloudRank_eq_prefix_count t v e, ←List.append_assoc,
replicate_encodeWord, Nat.add_zero] at h
exact h