Library QedkConcrete.memorybulk

Bulk memory writes preserve the sequential update semantics

Upstream batches aligned writes into one map update. Its runtime proves that optimization equivalent to repeated insertion, even for unsorted or duplicate keys. This bridge lets the existing memory invariants apply to the optimized executor without strengthening their premises.
From Stdlib Require Import List String ZArith.
Require Import RocqOfOCaml.RocqOfOCaml RocqOfOCaml.OCamlMapBulkInt.
Require Import MonadVM.Vm.
Set Default Goal Selector "!".

Module Mem := Vm.Make.Memory.

Lemma memory_bulk_as_fold {Host_t : Set} {F : @Vm.Make.FArgs Host_t}
    {UU : Basics.Unreachable unit}
    (indices : list int) (entry : int → int × string) (memory : @Mem.t Host_t F) :
  Mem.t.with_contents
    ((@Mem.M Host_t F).(MonadVM.Map.S.add_seq)
      (OCamlList.to_seq (OCamlList.map entry indices)) (Mem.t.contents memory)) memory =
  OCamlList.fold_left
    (fun mem index ⇒ let '(address, bytes) := entry index in
      @Mem.write_aligned Host_t F address bytes mem) memory indices.
Proof.
  change ((@Mem.M Host_t F).(MonadVM.Map.S.add_seq)
    (OCamlList.to_seq (OCamlList.map entry indices)) (Mem.t.contents memory))
    with (OCamlMapBulkInt.bulk_add_seq (OCamlList.map entry indices) (Mem.t.contents memory)).
  rewrite OCamlMapBulkInt.int_add_seq_matches_original_fold.
  unfold OCamlMapBulkInt.reference_add_seq.
  assert (Hmap : ∀ xs, OCamlList.map entry xs = Lists.List.map entry xs).
  { fix IH 1. intros [|first [|second rest]]; [reflexivity|reflexivity|].
    cbn [OCamlList.map Lists.List.map]. rewrite IH. reflexivity. }
  rewrite Hmap.
  revert memory. induction indices as [|index indices IH]; intro memory.
  - destruct memory. reflexivity.
  - cbn [Lists.List.map OCamlList.fold_left List.fold_left].
    destruct (entry index) as [address bytes].
    exact (IH (@Mem.write_aligned Host_t F address bytes memory)).
Qed.