Documentation

Examples.Ram.ArraySum

Calling the reusable array-sum function #

The function declared in Complexity.Computability.Ram.Array.Sum iterates over an array reference and returns its sum. The mathematical specification uses an ordinary list; its value and execution count describe that same implementation. The example reuses function contracts without opening the loop or its locals.

The array is already represented in memory. Neither the mathematical list nor the host-side sample state is an executable loader. The function requires no input/output driver; its unbounded runner additionally counts the actual outer call, return and halt. Word sums retain their modular arithmetic semantics. The ordinary sum function executes that same compiled call and returns a natural number. Its termination proof is erased; neither a mathematical list nor a proposed result or time budget is passed to the executable function.

theorem Ram.Examples.ArraySum.eval_eq {w heapLimit : ℕ} {array : ArrayRef w} {xs : List (Word w)} {entry : Source.State w} (hw : 0 < w) (fit : BitVec.toNat array.base + xs.length < 2 ^ w) (represented : array.Rep heapLimit xs entry) :

Function application exposes the represented list's sum and unchanged caller state, without a stream driver or an instruction budget.

A represented array and sufficient stack space make the compiled function halt. Only budget-free correctness is used to establish this fact.

def Ram.Examples.ArraySum.sum (array : ArrayRef 32) (heapLimit : ℕ) (entry : Source.State 32) (safe : ∃ (xs : List (Word 32)), array.Rep heapLimit xs entry ∧ BitVec.toNat array.base + xs.length < 2 ^ 32) (hstack : heapLimit + ABI.frameSize Source.Array.sumFunctions.registers < 2 ^ 32) :

Execute the compiled array-sum function and return its decoded word. The logical safety proof is erased; the only data inputs are the reference, heap boundary and preloaded state. No mathematical answer or fuel is supplied.

Equations
Instances For
    theorem Ram.Examples.ArraySum.sum_eq {heapLimit : ℕ} {array : ArrayRef 32} {xs : List (Word 32)} {entry : Source.State 32} (fit : BitVec.toNat array.base + xs.length < 2 ^ 32) (hstack : heapLimit + ABI.frameSize Source.Array.sumFunctions.registers < 2 ^ 32) (represented : array.Rep heapLimit xs entry) :
    sum array heapLimit entry ⋯ hstack = (List.map BitVec.toNat xs).sum % 2 ^ 32

    The ordinary executable function equals the mathematical list sum modulo the word range. The list specifies memory; it is not an execution parameter.

    theorem Ram.Examples.ArraySum.sum_eq_of_sum_lt {heapLimit : ℕ} {array : ArrayRef 32} {xs : List (Word 32)} {entry : Source.State 32} (fit : BitVec.toNat array.base + xs.length < 2 ^ 32) (hstack : heapLimit + ABI.frameSize Source.Array.sumFunctions.registers < 2 ^ 32) (represented : array.Rep heapLimit xs entry) (hsum : (List.map BitVec.toNat xs).sum < 2 ^ 32) :
    sum array heapLimit entry ⋯ hstack = (List.map BitVec.toNat xs).sum

    If the mathematical sum fits, the executable function returns that natural number exactly. This is a property of the same modular implementation.

    def Ram.Examples.ArraySum.runSum (array : ArrayRef 32) (heapLimit : ℕ) (entry : Source.State 32) :

    Execute the declared function on a preloaded array and observe its returned natural number, full transition count and stopping reason.

    Equations
    • One or more equations did not get rendered due to their size.
    Instances For
      theorem Ram.Examples.ArraySum.runSum_eq {heapLimit : ℕ} {array : ArrayRef 32} {xs : List (Word 32)} {entry : Source.State 32} (fit : BitVec.toNat array.base + xs.length < 2 ^ 32) (hstack : heapLimit + ABI.frameSize Source.Array.sumFunctions.registers < 2 ^ 32) (represented : array.Rep heapLimit xs entry) :
      runSum array heapLimit entry = some ((List.map BitVec.toNat xs).sum % 2 ^ 32, 18 * xs.length + 67, StopReason.halted)

      The compiled function returns the mathematical sum and its exact full count. Stack capacity is a safety premise, not a supplied execution limit; the count includes the enclosing call and halt but not host-side preloading.

      theorem Ram.Examples.ArraySum.runTotal_steps {heapLimit : ℕ} {array : ArrayRef 32} {xs : List (Word 32)} {entry : Source.State 32} (fit : BitVec.toNat array.base + xs.length < 2 ^ 32) (hstack : heapLimit + ABI.frameSize Source.Array.sumFunctions.registers < 2 ^ 32) (represented : array.Rep heapLimit xs entry) :
      (Source.Array.sumFunctions.runTotal.sum array heapLimit entry ⋯).steps = 18 * xs.length + 67

      The total executable application has the same complete transition count. This independent time proof is not used to define sum or prove it terminates.