Documentation

Examples.Ram.ArraySlice

Composing a returned borrowed array #

slice returns a borrowed subarray reference. sumSlice calls it and passes the actual returned array to the existing sum implementation. Its specification is an ordinary list drop followed by take; neither function copies or allocates the selected elements. Reference arithmetic and both returned fields are evaluated by the same compiled function call.

The function has no input/output driver. Its correctness proof reuses the array representation and sum contract, while its separate time proof charges both inner calls, the outer calling convention and halt. The initial heap is preloaded; no host loader is included in these RAM counts.

theorem Ram.Examples.ArraySlice.functions.arguments_eq.Sum.sumPair {w✝ : ℕ} (left right : ArrayRef w✝) :
arguments.Sum.sumPair left right = [left.base, left.length, right.base, right.length]

The declared parameters' actual by-value word fields.

@[reducible, inline]
abbrev Ram.Examples.ArraySlice.functions.applyState.slice {w✝ : ℕ} (xs : ArrayRef w✝) (offset count : Word w✝) (heapLimit✝ : ℕ) (entry✝ : Source.State w✝) :

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]
    noncomputable abbrev Ram.Examples.ArraySlice.functions.eval.sumSlice {w✝ : ℕ} (xs : ArrayRef w✝) (offset count : 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]
      noncomputable abbrev Ram.Examples.ArraySlice.functions.bodyTime.sumSlice {w✝ : ℕ} (xs : ArrayRef w✝) (offset count : 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

        The declared parameters' actual by-value word fields.

        @[reducible, inline]
        noncomputable abbrev Ram.Examples.ArraySlice.functions.eval.Sum.sumPair {w✝ : ℕ} (left right : ArrayRef 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'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]
            abbrev Ram.Examples.ArraySlice.functions.run.sumSlice {w✝ : ℕ} (xs : ArrayRef w✝) (offset count : 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
              @[reducible, inline]
              Equations
              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]
                    abbrev Ram.Examples.ArraySlice.functions.runTotal.sumSlice {w✝ : ℕ} (xs : ArrayRef w✝) (offset count : Word w✝) (heapLimit✝ : ℕ) (entry✝ : Source.State w✝) (h : LocalCompiler.Function.Halts (max 1 functions.registers) functions.program functionIndex.sumSlice function.sumSlice.params heapLimit✝ (arguments.sumSlice xs offset count) entry✝) :

                    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]
                      noncomputable abbrev Ram.Examples.ArraySlice.functions.bodyTime.Sum.sum {w✝ : ℕ} (xs : ArrayRef 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]
                        noncomputable abbrev Ram.Examples.ArraySlice.functions.bodyTime.slice {w✝ : ℕ} (xs : ArrayRef w✝) (offset count : 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]

                          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]
                            abbrev Ram.Examples.ArraySlice.functions.arguments.slice {w✝ : ℕ} (xs : ArrayRef w✝) (offset count : Word w✝) :
                            List (Word w✝)
                            Equations
                            Instances For
                              @[reducible, inline]
                              abbrev Ram.Examples.ArraySlice.functions.apply.sumSlice {w✝ : ℕ} (xs : ArrayRef w✝) (offset count : Word w✝) (heapLimit✝ : ℕ) (entry✝ : Source.State w✝) :

                              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]
                                abbrev Ram.Examples.ArraySlice.functions.arguments.sumSlice {w✝ : ℕ} (xs : ArrayRef w✝) (offset count : Word w✝) :
                                List (Word w✝)
                                Equations
                                Instances For
                                  @[reducible, inline]
                                  abbrev Ram.Examples.ArraySlice.functions.apply.slice {w✝ : ℕ} (xs : ArrayRef w✝) (offset count : Word w✝) (heapLimit✝ : ℕ) (entry✝ : Source.State w✝) :

                                  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]
                                    abbrev Ram.Examples.ArraySlice.functions.runTotal.slice {w✝ : ℕ} (xs : ArrayRef w✝) (offset count : Word w✝) (heapLimit✝ : ℕ) (entry✝ : Source.State w✝) (h : LocalCompiler.Function.Halts (max 1 functions.registers) functions.program functionIndex.slice function.slice.params heapLimit✝ (arguments.slice xs offset count) entry✝) :

                                    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]
                                      abbrev Ram.Examples.ArraySlice.functions.run.slice {w✝ : ℕ} (xs : ArrayRef w✝) (offset count : 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
                                        theorem Ram.Examples.ArraySlice.functions.arguments_eq.slice {w✝ : ℕ} (xs : ArrayRef w✝) (offset count : Word w✝) :
                                        arguments.slice xs offset count = [xs.base, xs.length, offset, count]

                                        The declared parameters' actual by-value word fields.

                                        @[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]
                                          abbrev Ram.Examples.ArraySlice.functions.run.Sum.sumPair {w✝ : ℕ} (left right : ArrayRef 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
                                            @[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]
                                              Equations
                                              Instances For
                                                @[reducible, inline]
                                                noncomputable abbrev Ram.Examples.ArraySlice.functions.bodyTime.Sum.sumPair {w✝ : ℕ} (left right : ArrayRef 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

                                                  A returned array value is passed directly to a previously proved function.

                                                  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.ArraySlice.functions.eval.Sum.sum {w✝ : ℕ} (xs : ArrayRef 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]
                                                        noncomputable abbrev Ram.Examples.ArraySlice.functions.eval.slice {w✝ : ℕ} (xs : ArrayRef w✝) (offset count : 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]
                                                          Equations
                                                          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.ArraySlice.functions.arguments_eq.sumSlice {w✝ : ℕ} (xs : ArrayRef w✝) (offset count : Word w✝) :
                                                              arguments.sumSlice xs offset count = [xs.base, xs.length, offset, count]

                                                              The declared parameters' actual by-value word fields.

                                                              theorem Ram.Examples.ArraySlice.slice_timeBound {w control heapLimit depth : ℕ} {P : List (Word w) → Source.State w → Prop} :
                                                              Source.FunctionTimeBound control functions.program heapLimit depth functions.function.slice P fun (x : List (Word w)) (x_1 : Source.State w) => 0

                                                              The slice body itself is empty. Its address arithmetic and two-field return are part of the actual calling convention, not an uncounted host computation.

                                                              theorem Ram.Examples.ArraySlice.slice_contract {w heapLimit depth : ℕ} {array : ArrayRef w} {offset count : Word w} {xs : List (Word w)} (span : BitVec.toNat offset + BitVec.toNat count ≤ BitVec.toNat array.length) :
                                                              Source.TypedFunctionContract functions.program heapLimit depth functions.function.slice DSL.ValueKind.array (fun (input : ArrayRef w × Word w × Word w) => functions.arguments.slice input.1 input.2.1 input.2.2) (fun (input : ArrayRef w × Word w × Word w) (entry : Source.State w) => input = (array, offset, count) ∧ array.Rep heapLimit xs entry) fun (x : ArrayRef w × Word w × Word w) (entry : Source.State w) (value : DSL.ValueKind.array.Value w) (finish : Source.State w) => value = array.subslice offset count ∧ ArrayRef.Rep value heapLimit (List.take (BitVec.toNat count) (List.drop (BitVec.toNat offset) xs)) finish ∧ finish = entry

                                                              The actual returned reference represents the mathematical list slice. The postcondition carries that representation directly to its next client.

                                                              theorem Ram.Examples.ArraySlice.slice_eval_eq {w heapLimit : ℕ} {array : ArrayRef w} {offset count : Word w} {xs : List (Word w)} {entry : Source.State w} (span : BitVec.toNat offset + BitVec.toNat count ≤ BitVec.toNat array.length) (represented : array.Rep heapLimit xs entry) :
                                                              functions.eval.slice array offset count heapLimit entry = Part.some (array.subslice offset count, entry)

                                                              The reusable slice function returns an ordinary typed reference, separately from any caller or stream driver. Its shared state is unchanged.

                                                              theorem Ram.Examples.ArraySlice.function_contract {w heapLimit : ℕ} {array : ArrayRef w} {offset count : Word w} {xs : List (Word w)} (hw : 0 < w) (fit : BitVec.toNat array.base + xs.length < 2 ^ w) (span : BitVec.toNat offset + BitVec.toNat count ≤ BitVec.toNat array.length) :
                                                              Source.TypedFunctionContract functions.program heapLimit 1 functions.function.sumSlice DSL.ValueKind.word (fun (input : ArrayRef w × Word w × Word w) => functions.arguments.sumSlice input.1 input.2.1 input.2.2) (fun (input : ArrayRef w × Word w × Word w) (entry : Source.State w) => input = (array, offset, count) ∧ array.Rep heapLimit xs entry) fun (x : ArrayRef w × Word w × Word w) (entry : Source.State w) (value : DSL.ValueKind.word.Value w) (finish : Source.State w) => value = Source.Array.wordSum (List.take (BitVec.toNat count) (List.drop (BitVec.toNat offset) xs)) ∧ finish = entry

                                                              Borrowing and summing a contained slice returns its mathematical list sum and preserves every caller state field. No time bound enters this contract.

                                                              theorem Ram.Examples.ArraySlice.eval_eq {w heapLimit : ℕ} {array : ArrayRef w} {offset count : Word w} {xs : List (Word w)} {entry : Source.State w} (hw : 0 < w) (fit : BitVec.toNat array.base + xs.length < 2 ^ w) (span : BitVec.toNat offset + BitVec.toNat count ≤ BitVec.toNat array.length) (represented : array.Rep heapLimit xs entry) :
                                                              functions.eval.sumSlice array offset count heapLimit entry = Part.some (Source.Array.wordSum (List.take (BitVec.toNat count) (List.drop (BitVec.toNat offset) xs)), entry)

                                                              The semantic value uses the same declared implementation and ordinary list slice.

                                                              theorem Ram.Examples.ArraySlice.sumSlice_halts {array : ArrayRef 32} {offset count : Word 32} {heapLimit : ℕ} {entry : Source.State 32} (safe : ∃ (xs : List (Word 32)), array.Rep heapLimit xs entry ∧ BitVec.toNat array.base + xs.length < 2 ^ 32) (span : BitVec.toNat offset + BitVec.toNat count ≤ BitVec.toNat array.length) (hstack : heapLimit + 2 * ABI.frameSize functions.registers < 2 ^ 32) :

                                                              The compiled invocation halts from represented data and sufficient stack space. The argument uses only the budget-free function contract.

                                                              def Ram.Examples.ArraySlice.sumSlice (array : ArrayRef 32) (offset count : Word 32) (heapLimit : ℕ) (entry : Source.State 32) (safe : ∃ (xs : List (Word 32)), array.Rep heapLimit xs entry ∧ BitVec.toNat array.base + xs.length < 2 ^ 32) (span : BitVec.toNat offset + BitVec.toNat count ≤ BitVec.toNat array.length) (hstack : heapLimit + 2 * ABI.frameSize functions.registers < 2 ^ 32) :

                                                              An ordinary executable value from the compiled slice-and-sum function. All proofs are erased; the runtime inputs are the reference, slice bounds, heap boundary and existing state, not the mathematical list or its sum.

                                                              Equations
                                                              Instances For
                                                                theorem Ram.Examples.ArraySlice.sumSlice_eq {heapLimit : ℕ} {array : ArrayRef 32} {xs : List (Word 32)} {entry : Source.State 32} (offset count : Word 32) (fit : BitVec.toNat array.base + xs.length < 2 ^ 32) (span : BitVec.toNat offset + BitVec.toNat count ≤ BitVec.toNat array.length) (hstack : heapLimit + 2 * ABI.frameSize functions.registers < 2 ^ 32) (represented : array.Rep heapLimit xs entry) :
                                                                sumSlice array offset count heapLimit entry ⋯ span hstack = (List.map BitVec.toNat (List.take (BitVec.toNat count) (List.drop (BitVec.toNat offset) xs))).sum % 2 ^ 32

                                                                Correctness is a value equation using standard list operations. The modular sum is retained for all legal inputs, not just those whose sum fits a word.

                                                                theorem Ram.Examples.ArraySlice.function_timeBound {w control heapLimit : ℕ} {array : ArrayRef w} {offset count : Word w} {xs : List (Word w)} (hw : 0 < w) (fit : BitVec.toNat array.base + xs.length < 2 ^ w) (span : BitVec.toNat offset + BitVec.toNat count ≤ BitVec.toNat array.length) :
                                                                Source.FunctionTimeBound control functions.program heapLimit 1 functions.function.sumSlice (fun (args : List (Word w)) (entry : Source.State w) => args = functions.arguments.sumSlice array offset count ∧ array.Rep heapLimit xs entry) fun (x : List (Word w)) (x_1 : Source.State w) => 18 * BitVec.toNat count + 119

                                                                The actual slice call precedes the sum call. The slice body costs zero, but its argument handling, address calculation and two-field return are all charged.

                                                                theorem Ram.Examples.ArraySlice.runTotal_steps_le {array : ArrayRef 32} {offset count : Word 32} {heapLimit : ℕ} {entry : Source.State 32} {xs : List (Word 32)} (fit : BitVec.toNat array.base + xs.length < 2 ^ 32) (span : BitVec.toNat offset + BitVec.toNat count ≤ BitVec.toNat array.length) (hstack : heapLimit + 2 * ABI.frameSize functions.registers < 2 ^ 32) (represented : array.Rep heapLimit xs entry) :
                                                                (functions.runTotal.sumSlice array offset count heapLimit entry ⋯).steps ≤ 18 * BitVec.toNat count + 189

                                                                The same compiled invocation additionally charges its outer call, return and halt. The slice list and the proposed bound are not runtime arguments.