Documentation

LeanPool.OrderMaintenance.Proof.Top.Loops

M5: running loops #

forM and mapM in CM, when every iteration succeeds: the result is a fold (forM_run, mapM_run). And folds of array updates at distinct indices (foldl_modify_getX).

theorem OrderMaintenance.Proof.Top.forM_run {α : Type} (l : List α) (f : α → Model.CM PUnit.{1}) (g : α → Model.Core → Model.Core) (Inv : List α → Model.Core → Prop) (s : Model.Core) :
Inv l s → (∀ (x : α) (rest : List α) (t : Model.Core), Inv (x :: rest) t → EStateM.run (f x) t = EStateM.Result.ok PUnit.unit (g x t) ∧ Inv rest (g x t)) → EStateM.run (l.forM f) s = EStateM.Result.ok PUnit.unit (List.foldl (fun (t : Model.Core) (x : α) => g x t) s l)

A forM whose iterations succeed, with an invariant over the remaining list.

theorem OrderMaintenance.Proof.Top.forM_run' {α : Type} (l : List α) (f : α → Model.CM PUnit.{1}) (g : α → Model.Core → Model.Core) (h : ∀ (x : α) (t : Model.Core), EStateM.run (f x) t = EStateM.Result.ok PUnit.unit (g x t)) (s : Model.Core) :
EStateM.run (l.forM f) s = EStateM.Result.ok PUnit.unit (List.foldl (fun (t : Model.Core) (x : α) => g x t) s l)

A forM whose iterations always succeed.

theorem OrderMaintenance.Proof.Top.mapM_run {α β : Type} (l : List α) (f : α → Model.CM β) (g : α → β) (s : Model.Core) (h : ∀ x ∈ l, EStateM.run (f x) s = EStateM.Result.ok (g x) s) :

A mapM of reads that succeed.

theorem OrderMaintenance.Proof.Top.foldl_modify_getX {α γ : Type} [Inhabited α] (l : List γ) (idx : γ → ℕ) (F : γ → α → α) (hl : (List.map idx l).Nodup) (a : Array α) (u : ℕ) :
(List.foldl (fun (b : Array α) (x : γ) => b.modify (idx x) (F x)) a l).getX u = match List.find? (fun (x : γ) => idx x == u) l with | some x => if u < a.size then F x (a.getX u) else a.getX u | none => a.getX u

Updates at distinct indices, folded.

theorem OrderMaintenance.Proof.Top.foldl_modify_getX' {α : Type} [Inhabited α] (F : ℕ → α → α) (l : List ℕ) :
l.Nodup → ∀ (a : Array α) (u : ℕ), (List.foldl (fun (b : Array α) (x : ℕ) => b.modify x (F x)) a l).getX u = if u ∈ l ∧ u < a.size then F u (a.getX u) else a.getX u

Updates at distinct indices, folded (the index is the element).

theorem OrderMaintenance.Proof.Top.foldl_modify_size {α γ : Type} (l : List γ) (idx : γ → ℕ) (F : γ → α → α) (a : Array α) :
(List.foldl (fun (b : Array α) (x : γ) => b.modify (idx x) (F x)) a l).size = a.size