Documentation

Complexity.Computability.Ram.Memory.Indexed.Basic

Ordinary indexed functions as memory models #

IndexedRep mem address values describes selected RAM cells by an ordinary function values : ι → Word w. The index can be Fin n, a product of finite indices, or another existing mathematical type; no new vector or map datatype is introduced. Reindexing is ordinary function composition, and a machine store is mathlib's Function.update on the model.

The assertion by itself describes values, not ownership or allocation. A store requires an injective address layout; Source.IndexedAt separately records that the observed addresses belong to the source heap. Thus no-wrap, aliasing and heap-access obligations stay explicit without being repeated in model proofs.

def Ram.IndexedRep {w : Nat} {ι : Type u} (mem : Word w → Word w) (address values : ι → Word w) :

Selected memory cells have the values of an ordinary indexed function.

Equations
Instances For
    theorem Ram.IndexedRep.lookup {w : Nat} {ι : Type u} {mem : Word w → Word w} {address values : ι → Word w} (h : IndexedRep mem address values) (i : ι) :
    mem (address i) = values i
    theorem Ram.IndexedRep.contents_eq {w : Nat} {ι : Type u} {mem : Word w → Word w} {address values : ι → Word w} (h : IndexedRep mem address values) :
    mem ∘ address = values

    The whole model is an ordinary function equality, suitable for rewriting any mathlib property without a second pointwise representation proof.

    theorem Ram.IndexedRep.iff_contents_eq {w : Nat} {ι : Type u} {mem : Word w → Word w} {address values : ι → Word w} :
    IndexedRep mem address values ↔ mem ∘ address = values
    theorem Ram.IndexedRep.of_contents {w : Nat} {ι : Type u} (mem : Word w → Word w) (address : ι → Word w) :
    IndexedRep mem address (mem ∘ address)
    theorem Ram.IndexedRep.eq {w : Nat} {ι : Type u} {mem : Word w → Word w} {address values other : ι → Word w} (h : IndexedRep mem address values) (h' : IndexedRep mem address other) :
    values = other

    The same observed cells cannot represent two different functions.

    theorem Ram.IndexedRep.congr_mem {w : Nat} {ι : Type u} {before after : Word w → Word w} {address values : ι → Word w} (h : IndexedRep before address values) (hmem : ∀ (i : ι), after (address i) = before (address i)) :
    IndexedRep after address values
    theorem Ram.IndexedRep.reindex {w : Nat} {ι : Type u} {κ : Type v} {mem : Word w → Word w} {address values : ι → Word w} (h : IndexedRep mem address values) (f : κ → ι) :
    IndexedRep mem (address ∘ f) (values ∘ f)

    Restricting, permuting or reshaping a model uses existing functions.

    theorem Ram.IndexedRep.reindex_iff {w : Nat} {ι : Type u} {κ : Type v} {mem : Word w → Word w} {address values : ι → Word w} (f : κ → ι) (hf : Function.Surjective f) :
    IndexedRep mem (address ∘ f) (values ∘ f) ↔ IndexedRep mem address values

    A surjective reindexing loses no information. In particular this applies to any mathlib Equiv, without inventing a representation-specific bijection.

    theorem Ram.IndexedRep.predicate_iff {w : Nat} {ι : Type u} {mem : Word w → Word w} {address values : ι → Word w} (h : IndexedRep mem address values) (P : (ι → Word w) → Prop) :
    P (mem ∘ address) ↔ P values
    theorem Ram.IndexedRep.store {w : Nat} {ι : Type u} {mem : Word w → Word w} {address values : ι → Word w} [DecidableEq ι] (h : IndexedRep mem address values) (hinj : Function.Injective address) (i : ι) (value : Word w) :
    IndexedRep (fun (a : Word w) => if a = address i then value else mem a) address (Function.update values i value)

    A real one-word store is standard function update when indices do not alias. The proof uses mathlib's update/composition theorem directly.

    theorem Ram.IndexedRep.store_outside {w : Nat} {ι : Type u} {mem : Word w → Word w} {address values : ι → Word w} (h : IndexedRep mem address values) (storedAt value : Word w) (hout : ∀ (i : ι), address i ≠ storedAt) :
    IndexedRep (fun (a : Word w) => if a = storedAt then value else mem a) address values

    Updates outside the selected cells preserve the complete model.

    theorem Ram.IndexedRep.setMem {w : Nat} {ι : Type u} {address values : ι → Word w} [DecidableEq ι] {s : State w} (h : IndexedRep s.mem address values) (hinj : Function.Injective address) (i : ι) (value : Word w) :
    IndexedRep (s.setMem (address i) value).mem address (Function.update values i value)
    theorem Ram.IndexedRep.exec_store {w : Nat} {ι : Type u} {address values : ι → Word w} [DecidableEq ι] {s : State w} (h : IndexedRep s.mem address values) (hinj : Function.Injective address) (i : ι) (addrReg src : Reg) (haddr : s.regs addrReg = address i) :
    IndexedRep (execInstr (Instr.store addrReg src) s).mem address (Function.update values i (s.regs src))

    The existing store instruction has the same model update; no additional executable primitive or abstract cost annotation is introduced.

    def Ram.Source.IndexedAt {w : Nat} {ι : Type u} (heapLimit : Nat) (address values : ι → Word w) (s : State w) :

    An indexed model all of whose cells lie in the source-visible heap.

    Equations
    Instances For
      theorem Ram.Source.IndexedAt.lookup {w : Nat} {ι : Type u} {heapLimit : Nat} {address values : ι → Word w} {s : State w} (h : IndexedAt heapLimit address values s) (i : ι) :
      s.mem (address i) = values i
      theorem Ram.Source.IndexedAt.addr_lt {w : Nat} {ι : Type u} {heapLimit : Nat} {address values : ι → Word w} {s : State w} (h : IndexedAt heapLimit address values s) (i : ι) :
      BitVec.toNat (address i) < heapLimit
      theorem Ram.Source.IndexedAt.contents_eq {w : Nat} {ι : Type u} {heapLimit : Nat} {address values : ι → Word w} {s : State w} (h : IndexedAt heapLimit address values s) :
      s.mem ∘ address = values
      theorem Ram.Source.IndexedAt.reindex {w : Nat} {ι : Type u} {κ : Type v} {heapLimit : Nat} {address values : ι → Word w} {s : State w} (h : IndexedAt heapLimit address values s) (f : κ → ι) :
      IndexedAt heapLimit (address ∘ f) (values ∘ f) s
      theorem Ram.Source.IndexedAt.setReg {w : Nat} {ι : Type u} {heapLimit : Nat} {address values : ι → Word w} {s : State w} (h : IndexedAt heapLimit address values s) (dst : Reg) (value : Word w) :
      IndexedAt heapLimit address values (s.setReg dst value)
      theorem Ram.Source.IndexedAt.setMem {w : Nat} {ι : Type u} {heapLimit : Nat} {address values : ι → Word w} {s : State w} [DecidableEq ι] (h : IndexedAt heapLimit address values s) (hinj : Function.Injective address) (i : ι) (value : Word w) :
      IndexedAt heapLimit address (Function.update values i value) (s.setMem (address i) value)
      theorem Ram.Source.IndexedAt.setMem_outside {w : Nat} {ι : Type u} {heapLimit : Nat} {address values : ι → Word w} {s : State w} (h : IndexedAt heapLimit address values s) (storedAt value : Word w) (hout : ∀ (i : ι), address i ≠ storedAt) :
      IndexedAt heapLimit address values (s.setMem storedAt value)
      theorem Ram.Source.IndexedAt.enter {w : Nat} {ι : Type u} {heapLimit : Nat} {address values : ι → Word w} {s : State w} (h : IndexedAt heapLimit address values s) (args : List (Word w)) :
      IndexedAt heapLimit address values (s.enter args)
      theorem Ram.Source.IndexedAt.leave {w : Nat} {ι : Type u} {heapLimit : Nat} {address values : ι → Word w} {s : State w} (h : IndexedAt heapLimit address values s) (caller : State w) (dsts : List Reg) (results : List Expr) :
      IndexedAt heapLimit address values (caller.leave s dsts results)
      theorem Ram.Source.IndexedAt.heapEqBelow {w : Nat} {ι : Type u} {heapLimit : Nat} {address values : ι → Word w} {s t : State w} (h : IndexedAt heapLimit address values s) (hmem : HeapEqBelow heapLimit s.mem t.mem) :
      IndexedAt heapLimit address values t

      Only heap agreement is needed to move a model between source states.

      theorem Ram.Source.State.Observes.indexed {w : Nat} {ι : Type u} {heapLimit locals : Nat} {address values : ι → Word w} {s : State w} {t : Ram.State w} (ho : Observes heapLimit locals s t) (h : IndexedAt heapLimit address values s) :
      IndexedRep t.mem address values

      Any indexed model, including product-indexed matrices and finite maps, is observable on the actual target state through the existing compiler bridge.