Documentation

Examples.Ram.LocalBindings

Composing functions with local bindings #

The function squaredNorm calls square twice and keeps both returned values in lexical locals. Neither function needs a main program, input/output streams or a hand-written register table. Their correctness proofs use the same source declarations and the reusable contract for square.

The addSquare helper reuses square for one accumulator update. The sumSquares array traversal calls this helper at each element; its complete list specification and separate cost proof are in Examples.Ram.ArrayFold.

The result equation observes the implementation through Ram.Func.eval. Arithmetic is machine-word arithmetic: the natural-number corollary encodes the sum of squares modulo the word range, without claiming that overflow is absent. These are semantic observations, not a second executable algorithm.

The independent cost proof reads the lengths of the two compiled calls: each invocation of square takes 23 transitions, including its multiplication in the return expression and its calling-convention code. Thus squaredNorm has body time 46. Its own final addition and enclosing call/return are outside Ram.Func.bodyTime; they must be charged when this function is called.

@[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.LocalBindings.functions.run.addSquare {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
      @[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 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.LocalBindings.functions.arguments_eq.addSquare {w✝ : ℕ} (accumulator x : Word w✝) :
          arguments.addSquare accumulator x = [accumulator, x]

          The declared parameters' actual by-value word fields.

          @[reducible, inline]
          noncomputable abbrev Ram.Examples.LocalBindings.functions.eval.squaredNorm {w✝ : ℕ} (x y : 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

            Local slots are inferred from parameters and lexical value bindings.

            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 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'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]

                    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]

                        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'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 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]

                              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.LocalBindings.functions.bodyTime.addSquare {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]
                                  noncomputable abbrev Ram.Examples.LocalBindings.functions.bodyTime.squaredNorm {w✝ : ℕ} (x y : 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 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]
                                        noncomputable abbrev Ram.Examples.LocalBindings.functions.bodyTime.square {w✝ : ℕ} (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]
                                          noncomputable abbrev Ram.Examples.LocalBindings.functions.bodyTime.sumSquares {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]

                                            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.LocalBindings.functions.arguments.addSquare {w✝ : ℕ} (accumulator x : Word w✝) :
                                              List (Word w✝)
                                              Equations
                                              Instances For
                                                @[reducible, inline]
                                                noncomputable abbrev Ram.Examples.LocalBindings.functions.eval.addSquare {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

                                                  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]
                                                    noncomputable abbrev Ram.Examples.LocalBindings.functions.eval.square {w✝ : ℕ} (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
                                                        theorem Ram.Examples.LocalBindings.square_contract {w : ℕ} (heapLimit : ℕ) (x : Word w) :
                                                        Source.FunctionContract functions.program heapLimit 0 functions.function.square (fun (args : List (Word w)) (x_1 : Source.State w) => args = functions.arguments.square x) fun (x_1 : List (Word w)) (entry : Source.State w) (value : List (Word w)) (finish : Source.State w) => value = [x * x] ∧ finish = entry

                                                        Squaring returns a word and leaves all caller state unchanged.

                                                        theorem Ram.Examples.LocalBindings.squaredNorm_contract {w : ℕ} (heapLimit : ℕ) (x y : Word w) :
                                                        Source.FunctionContract functions.program heapLimit 1 functions.function.squaredNorm (fun (args : List (Word w)) (x_1 : Source.State w) => args = functions.arguments.squaredNorm x y) fun (x_1 : List (Word w)) (entry : Source.State w) (value : List (Word w)) (finish : Source.State w) => value = [x * x + y * y] ∧ finish = entry

                                                        The two calls compose through their returned values. Their private local frames do not appear in the continuation proof, and no separate specification of intermediate local-variable states is needed.

                                                        theorem Ram.Examples.LocalBindings.addSquare_contract {w : ℕ} (heapLimit : ℕ) (accumulator x : Word w) :
                                                        Source.FunctionContract functions.program heapLimit 1 functions.function.addSquare (fun (args : List (Word w)) (x_1 : Source.State w) => args = functions.arguments.addSquare accumulator x) fun (x_1 : List (Word w)) (entry : Source.State w) (value : List (Word w)) (finish : Source.State w) => value = [accumulator + x * x] ∧ finish = entry

                                                        One array-fold step reuses the same square function through its contract. The accumulator and returned value are words; shared state is unchanged.

                                                        theorem Ram.Examples.LocalBindings.squaredNorm_eval {w : ℕ} (heapLimit : ℕ) (x y : Word w) (entry : Source.State w) :
                                                        functions.eval.squaredNorm x y heapLimit entry = Part.some (x * x + y * y, entry)

                                                        The program's semantic value is the sum of squares, independently of the caller's registers, memory and streams. The equation includes termination.

                                                        theorem Ram.Examples.LocalBindings.squaredNorm_eval_ofNat {w : ℕ} (heapLimit x y : ℕ) (entry : Source.State w) :
                                                        functions.eval.squaredNorm (BitVec.ofNat w x) (BitVec.ofNat w y) heapLimit entry = Part.some (BitVec.ofNat w (x * x + y * y), entry)

                                                        For encoded natural arguments the result is the encoded mathematical sum of squares; encoding retains the word model's modular arithmetic.

                                                        theorem Ram.Examples.LocalBindings.addSquare_body_steps {w control heapLimit depth steps : ℕ} {entry finish : Source.State w} (execution : Source.LocalMeasuredExec control functions.program heapLimit depth functions.function.addSquare.body steps entry finish) :
                                                        steps = 23

                                                        The fold step's body makes one actual square call. Its final addition and the enclosing call are counted by its caller, independently of this body equation.

                                                        theorem Ram.Examples.LocalBindings.squaredNorm_body_steps {w control heapLimit depth steps : ℕ} {entry finish : Source.State w} (execution : Source.LocalMeasuredExec control functions.program heapLimit depth functions.function.squaredNorm.body steps entry finish) :
                                                        steps = 46

                                                        Every completed body execution has the same count, obtained by adding the actual generated call blocks. No result specification enters this proof.

                                                        theorem Ram.Examples.LocalBindings.squaredNorm_timeBound {w : ℕ} (control heapLimit depth : ℕ) (P : List (Word w) → Source.State w → Prop) :
                                                        Source.FunctionTimeBound control functions.program heapLimit depth functions.function.squaredNorm P fun (x : List (Word w)) (x_1 : Source.State w) => 46

                                                        The exact count yields an independent conditional bound on invocations. Termination is supplied separately by squaredNorm_contract.

                                                        Functional correctness supplies the invocation; the separate count proof identifies its cost without replaying the value computation.

                                                        theorem Ram.Examples.LocalBindings.squaredNorm_bodyTime {w : ℕ} (heapLimit : ℕ) (x y : Word w) (entry : Source.State w) :

                                                        Body time is a property of the same invocation as squaredNorm_eval, not a supplied annotation. Each of its two completed calls takes 23 steps.