Documentation

Examples.Ram.FunctionComposition

Composing imported functions in one executable source #

functions imports the existing copy and sum implementations and calls them from a new function. Copy returns Unit and is called for its shared-memory effect, without a dummy result field or destination. No callee body is duplicated. The copy's mathematical postcondition supplies the destination representation needed by sum, and their previously proved contracts are transported through the generated embeddings.

This is one compiled RAM invocation, not two host-level runner calls. The arrays are preloaded and disjoint; allocation and loading are not implemented here. Correctness and termination do not depend on a proposed execution bound.

@[reducible, inline]
noncomputable abbrev Ram.Examples.FunctionComposition.functions.bodyTime.Copy.copy {w✝ : ℕ} (source destination remaining : 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'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]

      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]
        abbrev Ram.Examples.FunctionComposition.functions.applyState.Copy.copy {w✝ : ℕ} (source destination remaining : 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
          theorem Ram.Examples.FunctionComposition.functions.arguments_length.Copy.copy {w✝ : ℕ} (source destination remaining : Word w✝) :
          (arguments.Copy.copy source destination remaining).length = function.Copy.copy.params

          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]

            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.FunctionComposition.functions.eval.copyThenSum {w✝ : ℕ} (source destination : 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

                Copy an existing array and sum the destination through imported functions.

                Equations
                • One or more equations did not get rendered due to their size.
                Instances For
                  @[reducible, inline]
                  abbrev Ram.Examples.FunctionComposition.functions.arguments.copyThenSum {w✝ : ℕ} (source destination : ArrayRef w✝) :
                  List (Word w✝)
                  Equations
                  Instances For
                    @[reducible, inline]
                    noncomputable abbrev Ram.Examples.FunctionComposition.functions.eval.Copy.copy {w✝ : ℕ} (source destination remaining : 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.FunctionComposition.functions.bodyTime.copyThenSum {w✝ : ℕ} (source destination : 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]

                        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.FunctionComposition.functions.apply.Copy.copy {w✝ : ℕ} (source destination remaining : 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]
                            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]
                                abbrev Ram.Examples.FunctionComposition.functions.arguments.Copy.copy {w✝ : ℕ} (source destination remaining : Word w✝) :
                                List (Word w✝)
                                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]

                                    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.FunctionComposition.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]
                                        Equations
                                        • One or more equations did not get rendered due to their size.
                                        Instances For
                                          @[reducible, inline]
                                          Equations
                                          Instances For
                                            @[reducible, inline]
                                            abbrev Ram.Examples.FunctionComposition.functions.run.Copy.copy {w✝ : ℕ} (source destination remaining : 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]

                                              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
                                                  @[reducible, inline]
                                                  abbrev Ram.Examples.FunctionComposition.functions.run.copyThenSum {w✝ : ℕ} (source destination : 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]

                                                    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]

                                                      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.FunctionComposition.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

                                                          The declared parameters' actual by-value word fields.

                                                          theorem Ram.Examples.FunctionComposition.functions.arguments_eq.copyThenSum {w✝ : ℕ} (source destination : ArrayRef w✝) :
                                                          arguments.copyThenSum source destination = [source.base, source.length, destination.base, destination.length]

                                                          The declared parameters' actual by-value word fields.

                                                          @[reducible, inline]
                                                          abbrev Ram.Examples.FunctionComposition.functions.runTotal.Copy.copy {w✝ : ℕ} (source destination remaining : Word w✝) (heapLimit✝ : ℕ) (entry✝ : Source.State w✝) (h : LocalCompiler.Function.Halts (max 1 functions.registers) functions.program functionIndex.Copy.copy function.Copy.copy.params heapLimit✝ (arguments.Copy.copy source destination remaining) 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
                                                            theorem Ram.Examples.FunctionComposition.functions.arguments_eq.Copy.copy {w✝ : ℕ} (source destination remaining : Word w✝) :
                                                            arguments.Copy.copy source destination remaining = [source, destination, remaining]

                                                            The declared parameters' actual by-value word fields.

                                                            @[reducible, inline]
                                                            noncomputable abbrev Ram.Examples.FunctionComposition.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
                                                              theorem Ram.Examples.FunctionComposition.function_contract {w heapLimit : ℕ} {source destination : ArrayRef w} {xs ys : List (Word w)} (hw : 0 < w) (sameLength : ys.length = xs.length) (fit : BitVec.toNat destination.base + xs.length < 2 ^ w) (disjoint : ArraysDisjoint source.base xs.length destination.base xs.length) :
                                                              Source.FunctionContract functions.program heapLimit 1 functions.function.copyThenSum (fun (args : List (Word w)) (entry : Source.State w) => args = functions.arguments.copyThenSum source destination ∧ source.Rep heapLimit xs entry ∧ destination.Rep heapLimit ys entry) fun (x : List (Word w)) (entry : Source.State w) (value : List (Word w)) (finish : Source.State w) => value = [Source.Array.wordSum xs] ∧ Source.ArrayAt heapLimit source.base xs finish ∧ Source.ArrayAt heapLimit destination.base xs finish ∧ ArrayFrame destination.base xs.length entry.mem finish.mem ∧ finish.input = entry.input ∧ finish.outputRev = entry.outputRev

                                                              Mathematical contents and framing describe the effect of the composed implementation. No register index, stack layout or callee loop enters this proof.

                                                              theorem Ram.Examples.FunctionComposition.eval_eq {w heapLimit : ℕ} {source destination : ArrayRef w} {xs ys : List (Word w)} {entry : Source.State w} (hw : 0 < w) (sameLength : ys.length = xs.length) (fit : BitVec.toNat destination.base + xs.length < 2 ^ w) (disjoint : ArraysDisjoint source.base xs.length destination.base xs.length) (sourceArray : source.Rep heapLimit xs entry) (destinationArray : destination.Rep heapLimit ys entry) :
                                                              ∃ (finish : Source.State w), functions.eval.copyThenSum source destination heapLimit entry = Part.some (Source.Array.wordSum xs, finish) ∧ arrayContents finish.mem destination.base xs.length = xs

                                                              The same compiled composition admits an ordinary mathematical return equation, while retaining the actual copied shared state.

                                                              theorem Ram.Examples.FunctionComposition.imported_sumPair_eval {w heapLimit : ℕ} {left right : ArrayRef w} {xs ys : List (Word w)} {entry : Source.State w} (hw : 0 < w) (leftFit : BitVec.toNat left.base + xs.length < 2 ^ w) (rightFit : BitVec.toNat right.base + ys.length < 2 ^ w) (leftArray : left.Rep heapLimit xs entry) (rightArray : right.Rep heapLimit ys entry) :
                                                              functions.eval.Sum.sumPair left right heapLimit entry = Part.some (Source.Array.wordSum (xs ++ ys), entry)

                                                              An imported function's own calls are relocated as well. The original two-array theorem transfers without reopening either sum call or its loop.

                                                              def Ram.Examples.FunctionComposition.Safe (source destination : ArrayRef 32) (heapLimit : ℕ) (entry : Source.State 32) :

                                                              Existing disjoint arrays with matching lengths. Their contents are logical witnesses, not arguments used to execute the source program.

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

                                                                Fixed code for the imported functions and the new composed call.

                                                                Equations
                                                                • One or more equations did not get rendered due to their size.
                                                                Instances For
                                                                  theorem Ram.Examples.FunctionComposition.halts {source destination : ArrayRef 32} {heapLimit : ℕ} {entry : Source.State 32} (safe : Safe source destination heapLimit entry) (hstack : heapLimit + 2 * ABI.frameSize functions.registers < 2 ^ 32) :

                                                                  Correctness of the source composition supplies normal termination of its compiled invocation. This proof does not use the independent time bound.

                                                                  def Ram.Examples.FunctionComposition.copyThenSum (source destination : ArrayRef 32) (heapLimit : ℕ) (entry : Source.State 32) (safe : Safe source destination heapLimit entry) (hstack : heapLimit + 2 * ABI.frameSize functions.registers < 2 ^ 32) :

                                                                  Ordinary execution returns both the computed sum and the copied shared state from one compiled call. All proof arguments are erased at runtime.

                                                                  Equations
                                                                  Instances For
                                                                    theorem Ram.Examples.FunctionComposition.copyThenSum_spec {source destination : ArrayRef 32} {heapLimit : ℕ} {entry : Source.State 32} (safe : Safe source destination heapLimit entry) (hstack : heapLimit + 2 * ABI.frameSize functions.registers < 2 ^ 32) {xs ys : List (Word 32)} (sameLength : ys.length = xs.length) (sourceArray : source.Rep heapLimit xs entry) (destinationArray : destination.Rep heapLimit ys entry) (disjoint : ArraysDisjoint source.base xs.length destination.base xs.length) :
                                                                    have result := copyThenSum source destination heapLimit entry safe hstack; BitVec.toNat result.1 = (List.map BitVec.toNat xs).sum % 2 ^ 32 ∧ arrayContents result.2.mem destination.base xs.length = xs ∧ ArrayFrame destination.base xs.length entry.mem result.2.mem ∧ result.2.input = entry.input ∧ result.2.outputRev = entry.outputRev

                                                                    A mathematical postcondition of the actual executable result: the scalar is the list sum and the destination contains the copied list.