Documentation

Examples.Ram.ArrayFold

Folding an array through a proved function #

The sumSquares function in LocalBindings.functions calls addSquare for each represented word. That step calls the existing square function; neither the traversal proof nor the mathematical specification supplies a second squaring implementation. The specification is the ordinary natural-number sum of the squared decoded words, encoded back into the word range.

The source uses a scoped for element binding. Generated body and return equations select the shared function rule, so this proof does not name the private cursor, extract call expressions or reconstruct parameter frames.

theorem Ram.Examples.ArrayFold.foldl_addSquare {w : ℕ} (xs : List (Word w)) :
List.foldl (fun (accumulator : BitVec w) (x : Word w) => accumulator + x * x) 0 xs = BitVec.ofNat w (List.map (fun (x : Word w) => BitVec.toNat x ^ 2) xs).sum

The word-valued fold is the encoded ordinary sum of natural squares. This equation uses list homomorphisms, independently of a machine execution.

theorem Ram.Examples.ArrayFold.function_contract {w heapLimit : ℕ} {array : ArrayRef w} {xs : List (Word w)} (hw : 0 < w) (fit : BitVec.toNat array.base + xs.length < 2 ^ w) :
Source.FunctionContract LocalBindings.functions.program heapLimit 2 LocalBindings.functions.function.sumSquares (fun (args : List (Word w)) (entry : Source.State w) => args = LocalBindings.functions.arguments.sumSquares array ∧ array.Rep heapLimit xs entry) fun (x : List (Word w)) (entry : Source.State w) (value : List (Word w)) (finish : Source.State w) => value = [BitVec.ofNat w (List.map (fun (x : Word w) => BitVec.toNat x ^ 2) xs).sum] ∧ finish = entry

The function returns the mathematical sum of squares through its word encoding and preserves caller state. No stream driver or time bound is needed.

theorem Ram.Examples.ArrayFold.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) :
LocalBindings.functions.eval.sumSquares array heapLimit entry = Part.some (BitVec.ofNat w (List.map (fun (x : Word w) => BitVec.toNat x ^ 2) xs).sum, entry)

Observe the same implemented function using an ordinary list expression.

theorem Ram.Examples.ArrayFold.eval_toNat {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) (resultFit : (List.map (fun (x : Word w) => BitVec.toNat x ^ 2) xs).sum < 2 ^ w) :
Part.map (fun (result : DSL.ValueKind.word.Value w × Source.State w) => BitVec.toNat result.1) (LocalBindings.functions.eval.sumSquares array heapLimit entry) = Part.some (List.map (fun (x : Word w) => BitVec.toNat x ^ 2) xs).sum

When the mathematical sum fits, the decoded result is exactly that natural number. The modular theorem above does not assume intermediate no-overflow.

theorem Ram.Examples.ArrayFold.bodyTime_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) :

Body time is observed from the terminating implementation and then identified by the independent loop count. It includes both nested calls.

Execute the same declared fold on a preloaded array. The projection retains the returned word, full call count and stopping reason; it performs no list loading.

Equations
  • One or more equations did not get rendered due to their size.
Instances For
    theorem Ram.Examples.ArrayFold.runSumSquares_eq {heapLimit : ℕ} {array : ArrayRef 32} {xs : List (Word 32)} {entry : Source.State 32} (fit : BitVec.toNat array.base + xs.length < 2 ^ 32) (hstack : heapLimit + 3 * ABI.frameSize LocalBindings.functions.registers < 2 ^ 32) (represented : array.Rep heapLimit xs entry) :
    runSumSquares array heapLimit entry = some ((List.map (fun (x : Word 32) => BitVec.toNat x ^ 2) xs).sum % 2 ^ 32, 76 * xs.length + 67, StopReason.halted)

    The executable function application returns that list sum and the complete machine count, including its enclosing call and halt. Memory is preloaded; stack capacity is a safety premise, not a proposed execution budget.

    The declared parameters' actual by-value word fields.

    @[reducible, inline]

    Project the declared word, array, or Unit result of the normally terminating call. Use runTotal or applyState to observe execution when the result is Unit.

    Equations
    • One or more equations did not get rendered due to their size.
    Instances For
      @[reducible, inline]

      The declared function's compiled body count, excluding its enclosing call overhead.

      Equations
      • One or more equations did not get rendered due to their size.
      Instances For
        @[reducible, inline]

        Execute the declared function's compiled call, retaining the complete machine result.

        Equations
        • One or more equations did not get rendered due to their size.
        Instances For
          @[reducible, inline]

          Execute the declared function and return its typed value and source shared state, excluding private stack cells and restoring caller registers while retaining actual stream effects.

          Equations
          • One or more equations did not get rendered due to their size.
          Instances For
            @[reducible, inline]

            The declared function's result and shared state, observed through its actual execution.

            Equations
            • One or more equations did not get rendered due to their size.
            Instances For
              @[reducible, inline]

              The declared function's result and shared state, observed through its actual execution.

              Equations
              • One or more equations did not get rendered due to their size.
              Instances For
                @[reducible, inline]

                Execute the declared function and return its typed value and source shared state, excluding private stack cells and restoring caller registers while retaining actual stream effects.

                Equations
                • One or more equations did not get rendered due to their size.
                Instances For
                  theorem Ram.Examples.ArrayFold.Factorials.functions.arguments_eq.addFactorial {w✝ : ℕ} (accumulator x : Word w✝) :
                  arguments.addFactorial accumulator x = [accumulator, x]

                  The declared parameters' actual by-value word fields.

                  @[reducible, inline]
                  abbrev Ram.Examples.ArrayFold.Factorials.functions.run.addFactorial {w✝ : ℕ} (accumulator x : Word w✝) :
                  ℕ → Source.State w✝ → Option (RunResult (State w✝))

                  Execute the declared function's compiled call, retaining the complete machine result.

                  Equations
                  • One or more equations did not get rendered due to their size.
                  Instances For

                    The declared parameters' actual by-value word fields.

                    @[reducible, inline]

                    Execute the declared function's compiled call, retaining the complete machine result.

                    Equations
                    • One or more equations did not get rendered due to their size.
                    Instances For
                      @[reducible, inline]

                      Project the declared word, array, or Unit result of the normally terminating call. Use runTotal or applyState to observe execution when the result is Unit.

                      Equations
                      • One or more equations did not get rendered due to their size.
                      Instances For
                        @[reducible, inline]
                        noncomputable abbrev Ram.Examples.ArrayFold.Factorials.functions.eval.addFactorial {w✝ : ℕ} (accumulator x : Word w✝) :

                        The declared function's result and shared state, observed through its actual execution.

                        Equations
                        • One or more equations did not get rendered due to their size.
                        Instances For
                          @[reducible, inline]

                          Execute the declared function with a normal-halt proof, retaining its state and count.

                          Equations
                          • One or more equations did not get rendered due to their size.
                          Instances For
                            @[reducible, inline]

                            Execute the declared function with a normal-halt proof, retaining its state and count.

                            Equations
                            • One or more equations did not get rendered due to their size.
                            Instances For
                              @[reducible, inline]

                              Project the declared word, array, or Unit result of the normally terminating call. Use runTotal or applyState to observe execution when the result is Unit.

                              Equations
                              • One or more equations did not get rendered due to their size.
                              Instances For
                                @[reducible, inline]

                                Execute the declared function and return its typed value and source shared state, excluding private stack cells and restoring caller registers while retaining actual stream effects.

                                Equations
                                • One or more equations did not get rendered due to their size.
                                Instances For

                                  A varying-cost fold imports the existing recursive factorial implementation. The adapter performs one actual call; the traversal performs one adapter call per represented array element. Neither source body computes a host-side list.

                                  Equations
                                  • One or more equations did not get rendered due to their size.
                                  Instances For
                                    @[reducible, inline]
                                    Equations
                                    • One or more equations did not get rendered due to their size.
                                    Instances For
                                      @[reducible, inline]
                                      Equations
                                      Instances For
                                        @[reducible, inline]
                                        noncomputable abbrev Ram.Examples.ArrayFold.Factorials.functions.bodyTime.addFactorial {w✝ : ℕ} (accumulator x : Word w✝) :
                                        ℕ → Source.State w✝ → Part ℕ

                                        The declared function's compiled body count, excluding its enclosing call overhead.

                                        Equations
                                        • One or more equations did not get rendered due to their size.
                                        Instances For
                                          @[reducible, inline]

                                          The declared function's compiled body count, excluding its enclosing call overhead.

                                          Equations
                                          • One or more equations did not get rendered due to their size.
                                          Instances For
                                            @[reducible, inline]

                                            Execute the declared function with a normal-halt proof, retaining its state and count.

                                            Equations
                                            • One or more equations did not get rendered due to their size.
                                            Instances For
                                              def Ram.Examples.ArrayFold.Factorials.step {w : ℕ} (accumulator x : Word w) :

                                              The mathematical update describes the adapter's returned word, including the word model's modular factorial and addition. It is not executable source.

                                              Equations
                                              Instances For
                                                theorem Ram.Examples.ArrayFold.Factorials.addFactorial_contract {w : ℕ} (heapLimit K : ℕ) (accumulator x : Word w) (bounded : BitVec.toNat x ≤ K) :
                                                Source.FunctionContract functions.program heapLimit (K + 1) functions.function.addFactorial (fun (args : List (Word w)) (x_1 : Source.State w) => args = [accumulator, x]) fun (x_1 : List (Word w)) (entry : Source.State w) (value : List (Word w)) (finish : Source.State w) => value = [step accumulator x] ∧ finish = entry

                                                A bound on this element gives sufficient recursive call depth. Correctness uses only the imported factorial contract, without a proposed instruction budget.

                                                theorem Ram.Examples.ArrayFold.Factorials.addFactorial_timeBound {w : ℕ} (control heapLimit depth : ℕ) (accumulator x : Word w) :
                                                Source.FunctionTimeBound control functions.program heapLimit (depth + 1) functions.function.addFactorial (fun (args : List (Word w)) (x_1 : Source.State w) => args = [accumulator, x]) fun (x_1 : List (Word w)) (x_2 : Source.State w) => 37 * BitVec.toNat x + 32

                                                The adapter body pays the factorial body's input-dependent bound and its actual 28-step call overhead. Its final addition belongs to its own return code.

                                                The machine-word fold is the encoding of the ordinary sum of factorials. No no-overflow claim is implicit in this equality.

                                                Sum the actual element-dependent factorial-call bounds. The additional 53 steps per element pay the adapter's enclosing call and the traversal; the final eight steps initialize and finish the array function's body.

                                                Equations
                                                Instances For
                                                  theorem Ram.Examples.ArrayFold.Factorials.typed_contract {w heapLimit K : ℕ} {xs : List (Word w)} (hw : 0 < w) (bounded : ∀ x ∈ xs, BitVec.toNat x ≤ K) :
                                                  Source.TypedFunctionContract functions.program heapLimit (K + 2) functions.function.sumFactorials DSL.ValueKind.word functions.arguments.sumFactorials (fun (array : ArrayRef w) (entry : Source.State w) => array.Rep heapLimit xs entry ∧ BitVec.toNat array.base + xs.length < 2 ^ w) fun (x : ArrayRef w) (entry : Source.State w) (value : DSL.ValueKind.word.Value w) (finish : Source.State w) => value = List.foldl step 0 xs ∧ finish = entry

                                                  The returned scalar is an ordinary mathematical fold. The array is already represented in the entry heap, and the element bound supplies recursive depth; the invocation preserves every part of the caller's shared state.

                                                  theorem Ram.Examples.ArrayFold.Factorials.eval_eq {w heapLimit K : ℕ} {array : ArrayRef w} {xs : List (Word w)} {entry : Source.State w} (hw : 0 < w) (fit : BitVec.toNat array.base + xs.length < 2 ^ w) (bounded : ∀ x ∈ xs, BitVec.toNat x ≤ K) (represented : array.Rep heapLimit xs entry) :
                                                  functions.eval.sumFactorials array heapLimit entry = Part.some (List.foldl step 0 xs, entry)

                                                  The declaration's typed semantic result describes the implemented fold, not a second factorial algorithm or a stream-output wrapper.

                                                  theorem Ram.Examples.ArrayFold.Factorials.eval_toNat {w heapLimit K : ℕ} {array : ArrayRef w} {xs : List (Word w)} {entry : Source.State w} (hw : 0 < w) (fit : BitVec.toNat array.base + xs.length < 2 ^ w) (bounded : ∀ x ∈ xs, BitVec.toNat x ≤ K) (represented : array.Rep heapLimit xs entry) (resultFit : (List.map (fun (x : Word w) => (BitVec.toNat x).factorial) xs).sum < 2 ^ w) :
                                                  Part.map (fun (result : DSL.ValueKind.word.Value w × Source.State w) => BitVec.toNat result.1) (functions.eval.sumFactorials array heapLimit entry) = Part.some (List.map (fun (x : Word w) => (BitVec.toNat x).factorial) xs).sum

                                                  With no overflow in the mathematical sum, the implemented fold has that exact natural-number value. Modular semantics remain available without this premise.

                                                  theorem Ram.Examples.ArrayFold.Factorials.function_timeBound {w control heapLimit K : ℕ} {array : ArrayRef w} {xs : List (Word w)} (hw : 0 < w) (fit : BitVec.toNat array.base + xs.length < 2 ^ w) (bounded : ∀ x ∈ xs, BitVec.toNat x ≤ K) :
                                                  Source.FunctionTimeBound control functions.program heapLimit (K + 2) functions.function.sumFactorials (fun (args : List (Word w)) (entry : Source.State w) => args = functions.arguments.sumFactorials array ∧ array.Rep heapLimit xs entry) fun (x : List (Word w)) (x_1 : Source.State w) => bodyBudget xs

                                                  The independent bound sums factorial work at the actual array elements. Only their admissibility and the accumulator's mathematical invariant are used to instantiate the general data-dependent traversal rule.

                                                  theorem Ram.Examples.ArrayFold.Factorials.halts {heapLimit K : ℕ} {array : ArrayRef 32} {xs : List (Word 32)} {entry : Source.State 32} (fit : BitVec.toNat array.base + xs.length < 2 ^ 32) (bounded : ∀ x ∈ xs, BitVec.toNat x ≤ K) (represented : array.Rep heapLimit xs entry) (hstack : heapLimit + (K + 3) * ABI.frameSize functions.registers < 2 ^ 32) :

                                                  Budget-free correctness establishes normal termination of the compiled invocation. The bound reserves the outer fold frame, adapter frame and recursive factorial frames; it is a sufficient address-space condition, not a peak-space claim.

                                                  theorem Ram.Examples.ArrayFold.Factorials.applyState_eq {heapLimit K : ℕ} {array : ArrayRef 32} {xs : List (Word 32)} {entry : Source.State 32} (fit : BitVec.toNat array.base + xs.length < 2 ^ 32) (bounded : ∀ x ∈ xs, BitVec.toNat x ≤ K) (represented : array.Rep heapLimit xs entry) (hstack : heapLimit + (K + 3) * ABI.frameSize functions.registers < 2 ^ 32) :
                                                  functions.applyState.sumFactorials array heapLimit entry ⋯ = (List.foldl step 0 xs, entry)

                                                  The ordinary executable typed projection returns the mathematical fold and the unchanged shared state of this same compiled invocation.

                                                  theorem Ram.Examples.ArrayFold.Factorials.runTotal_steps_le {heapLimit K : ℕ} {array : ArrayRef 32} {xs : List (Word 32)} {entry : Source.State 32} (fit : BitVec.toNat array.base + xs.length < 2 ^ 32) (bounded : ∀ x ∈ xs, BitVec.toNat x ≤ K) (represented : array.Rep heapLimit xs entry) (hstack : heapLimit + (K + 3) * ABI.frameSize functions.registers < 2 ^ 32) :
                                                  (functions.runTotal.sumFactorials array heapLimit entry ⋯).steps ≤ bodyBudget xs + 59

                                                  The same executable run pays the element-dependent body bound plus its actual outer call, return and halt. No array-loading work is included.