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.