TheoremcopyLoop_lengths
The loop's invariant on lengths: made assignments are distinct and of length k.
Statement
theorem copyLoop_lengths {gid k : Nat} {tr : Assignment → Except SpecError (Spec (List D.Def))}
(htr : ∀ a s, a.length = k → tr a = .ok s → ∀ r ∈ s.reqs, r.1 = gid → r.2.length = k) :
∀ (fuel : Nat) (pending : List Assignment) (made : List (Assignment × List D.Def)) (out : List Req),
(∀ a ∈ pending, a.length = k) → (∀ a ∈ made.map (·.1), a.length = k) → (made.map (·.1)).Nodup →
(made.length + fuel > 2 ^ k → copyLoop gid tr fuel pending made out ≠ .error .copyFuel ∨
∃ a e, tr a = .error e) ∧
(∀ madeF outF, copyLoop gid tr fuel pending made out = .ok (madeF, outF) →
(∀ a ∈ madeF.map (·.1), a.length = k) ∧ (madeF.map (·.1)).Nodup)
| 0, _, made, _, _, hm, hn => by
refine ⟨fun hgt => ?_, fun _ _ h => by simp [copyLoop] at h⟩
haveProof
length_le_pow hn hm
simp at this; omega
| fuel + 1, pending, made, out, hp, hm, hn => by
rw [copyLoop_succ]
by_cases ht : todoOf pending made = []
· simp only [ht, ↓reduceIte]
refine ⟨fun _ => .inl (by simp), fun madeF outF h => ?_⟩
simp only [Except.ok.injEq, Prod.mk.injEq] at h
obtain ⟨rfl, rfl⟩ := h
exact ⟨hm, hn⟩
· simp only [ht, ↓reduceIte]
cases htr' : translateAll tr (todoOf pending made) with
| error e =>
obtain ⟨a, -, ha⟩ := translateAll_error htr'
exact ⟨fun _ => .inr ⟨a, e, ha⟩, fun _ _ h => by cases h⟩
| ok rs =>
simp only
obtain ⟨hkeys, hrs⟩ := translateAll_ok htr'
have hm' : ∀ a ∈ (addMade made rs).map (·.1), a.length = k := by
rw [keys_append, hkeys]
intro a ha
rcases List.mem_append.mp ha with ha | ha
· exact hm a ha
· exact hp a (mem_todoOf.mp ha).1
have hn' : ((addMade made rs).map (·.1)).Nodup := by
rw [keys_append, hkeys]
refine List.nodup_append.mpr ⟨hn, nodup_dedupFirst _, ?_⟩
intro a ha b hb e
subst e
exact (mem_todoOf.mp hb).2 ha
have hp' : ∀ b ∈ ((rs.flatMap (·.2.reqs)).filter (·.1 == gid)).map (·.2), b.length = k := by
intro b hb
obtain ⟨p, hpm, r, hr, hg, rfl⟩ := (mem_next_pending (gid := gid)).mp hb
have hp1 : p.1 ∈ todoOf pending made := by rw [← hkeys]; exact List.mem_map_of_mem hpm
exact htr p.1 p.2 (hp p.1 (mem_todoOf.mp hp1).1) (hrs p hpm) r hr hg
obtain ⟨ih1, ih2⟩ := copyLoop_lengths htr fuel _ _ (out ++ (rs.flatMap (·.2.reqs)).filter (·.1 != gid)) hp' hm' hn'
refine ⟨fun hgt => ih1 ?_, ih2⟩
have hlen : rs.length ≥ 1 := by
cases rs with
| nil => simp at hkeys; exact absurd hkeys ht
| cons _ _ => simp
simp only [addMade, List.length_append, List.length_map]
omegaProved by a term, which Lean's kernel checks directly.
Theorems it uses
Used by
In StandardElmMech/Stat/CopyLoop.lean: The copy loop terminates within 2^k + 1 rounds, makes the least set of copies closed under requests, at most 2^k (copyLoop_fuel, copyLoop_least, copies_le_pow).
A Standard Definition of Elm? (Draft version 0.4) © Rupert Smith. The text is licensed under CC BY 4.0: share and adapt it freely, with credit and a link to the licence.