Elm Exactly

TheoremcopyLoop_inv

StandardElmMech/Stat/CopyLoop.lean:177, The invariant · term proof

What the loop returns: every pending and made assignment is made; the made ones are the least set closed under requests above the pending and made ones; and they are closed when the made ones' requests are pending or made.

Statement

theorem copyLoop_inv {gid : Nat} {tr : Assignment → Except SpecError (Spec (List D.Def))} :
    ∀ (fuel : Nat) (pending : List Assignment) (made : List (Assignment × List D.Def)) (out : List Req)
      (madeF : List (Assignment × List D.Def)) (outF : List Req),
    copyLoop gid tr fuel pending made out = .ok (madeF, outF) →
      (∀ a, a ∈ pending ∨ a ∈ made.map (·.1) → a ∈ madeF.map (·.1)) ∧
      (∀ M : List Assignment, (∀ a, a ∈ pending ∨ a ∈ made.map (·.1) → a ∈ M) → Closed gid tr M →
        ∀ a ∈ madeF.map (·.1), a ∈ M) ∧
      ((∀ a ∈ made.map (·.1), ∀ b, ReqsOn gid tr a b → b ∈ pending ∨ b ∈ made.map (·.1)) →
        Closed gid tr (madeF.map (·.1)))
  | 0, _, _, _, _, _, h => by simp [copyLoop] at h
  | fuel + 1, pending, made, out, madeF, outF, h => by
    rw [copyLoop_succ] at h
    by_cases ht : todoOf pending made = []
    · simp only [ht, ↓reduceIte] at h
      simp only [Except.ok.injEq, Prod.mk.injEq] at h
      obtain ⟨rfl, rfl⟩

Proof

h
      have hp : ∀ a ∈ pending, a ∈ made.map (·.1) := by
        intro a ha
        refine Classical.byContradiction fun hn => ?_
        have : a ∈ todoOf pending made := mem_todoOf.mpr ⟨ha, hn⟩
        rw [ht] at this; cases this
      refine ⟨?_, ?_, ?_⟩
      · rintro a (ha | ha)
        · exact hp a ha
        · exact ha
      · intro M hM _ a ha; exact hM a (.inr ha)
      · intro hc a ha b hb
        rcases hc a ha b hb with hb | hb
        · exact hp b hb
        · exact hb
    · simp only [ht, ↓reduceIte] at h
      cases htr : translateAll tr (todoOf pending made) with
      | error e => rw [htr] at h; cases h
      | ok rs =>
        rw [htr] at h
        simp only at h
        obtain ⟨hkeys, hrs⟩ := translateAll_ok htr
        obtain ⟨ih1, ih2, ih3⟩ := copyLoop_inv fuel _ _ _ madeF outF h
        rw [keys_append, hkeys] at ih1 ih2 ih3
        have hpend : ∀ a ∈ pending, a ∈ made.map (·.1) ++ todoOf pending made := by
          intro a ha
          by_cases hm : a ∈ made.map (·.1)
          · exact List.mem_append_left _ hm
          · exact List.mem_append_right _ (mem_todoOf.mpr ⟨ha, hm⟩)
        refine ⟨?_, ?_, ?_⟩
        · rintro a (ha | ha)
          · exact ih1 a (.inr (hpend a ha))
          · exact ih1 a (.inr (List.mem_append_left _ ha))
        · intro M hM hC
          apply ih2 M _ hC
          rintro a (ha | ha)
          · obtain ⟨p, hp, r, hr, hg, rfl⟩ := (mem_next_pending (gid := gid)).mp ha
            have hp1 : p.1 ∈ todoOf pending made := by rw [← hkeys]; exact List.mem_map_of_mem hp
            exact hC p.1 (hM p.1 (.inl (mem_todoOf.mp hp1).1)) r.2 ⟨p.2, hrs p hp, r, hr, hg, rfl⟩
          · rcases List.mem_append.mp ha with ha | ha
            · exact hM a (.inr ha)
            · exact hM a (.inl (mem_todoOf.mp ha).1)
        · intro hc
          apply ih3
          intro a ha b hb
          rcases List.mem_append.mp ha with ha | ha
          · rcases hc a ha b hb with hb' | hb'
            · exact .inr (hpend b hb')
            · exact .inr (List.mem_append_left _ hb')
          · left
            obtain ⟨s, hs, r, hr, hg, rfl⟩ := hb
            rw [← hkeys] at ha
            obtain ⟨p, hp, rfl⟩ := List.mem_map.mp ha
            have : s = p.2 := by rw [hrs p hp] at hs; cases hs; rfl
            subst this
            exact (mem_next_pending (gid := gid)).mpr ⟨p, hp, r, hr, hg, rfl⟩

Proved 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.