Documentation

Examples.Ram.Factorial

A standalone recursive factorial function #

The function is declared once with ram_functions%, independently of the input and word width. Its correctness theorem returns a word and preserves arbitrary caller state; it requires neither a stream driver nor a proposed time budget. Separate time theorems measure the same recursive body and its actual calls.

The default local-frame compiler saves and restores the callee's declared locals on every invocation. factorialNat names mathlib's Nat.factorial as the mathematical specification: it never appears in a runtime instruction. The result is factorial modulo the word range, so multiplication may wrap. Exact natural-number results require the stated no-overflow premise.

Examples.Ram.FactorialFunction exposes function-value equations, and Examples.Ram.FunctionRun runs this function directly. The optional input/output adapter and its whole-program time proofs are in Examples.Ram.FactorialStream.

The declared parameters' actual by-value word fields.

Both function names and local register names are resolved automatically. The source does not maintain a numeric function table or a self index.

Equations
  • One or more equations did not get rendered due to their size.
Instances For
    @[reducible, inline]
    noncomputable abbrev Ram.Examples.Factorial.functions.bodyTime.factorial {w✝ : ℕ} (n : 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]

        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]

              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.Factorial.functions.eval.factorial {w✝ : ℕ} (n : 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

                  Inspect the resolved declaration for the semantic proof below.

                  Equations
                  Instances For

                    This resolved index is used only in proofs, never in the named source.

                    Equations
                    Instances For
                      @[reducible, inline]

                      The standard mathematical factorial, not a machine operation.

                      Equations
                      Instances For
                        theorem Ram.Examples.Factorial.leave_answer {w : ℕ} (s : Source.State w) (args : List (Word w)) (answer : Word w) :
                        s.leave ((s.enter args).setReg 1 answer) [1] [Expr.var 1] = s.setReg 1 answer

                        The callee's local registers are genuinely discarded on return. In particular, the caller's original n survives its recursive call.

                        theorem Ram.Examples.Factorial.function_contract {w : ℕ} (H k : ℕ) (hk : k < 2 ^ w) :
                        Source.FunctionContract program H k factorial (fun (args : List (Word w)) (x : Source.State w) => args = functions.arguments.factorial (BitVec.ofNat w k)) fun (x : List (Word w)) (entry : Source.State w) (result : List (Word w)) (finish : Source.State w) => result = [value w k] ∧ finish = entry

                        The independently callable function returns factorial and preserves all caller state. Ordinary natural-number induction supplies the recursive call's argument/result contract; generated parameter binding and return equations handle local state. No stream adapter or time bound enters this proof.

                        Apply factorial directly to a word argument, without a main or I/O.

                        theorem Ram.Examples.Factorial.function_result {w H depth k : ℕ} {entry finish : Source.State w} {result : Word w} (hk : k < 2 ^ w) (hresult : k.factorial < 2 ^ w) (execution : Source.FunctionExec program H depth factorial (functions.arguments.factorial (BitVec.ofNat w k)) entry [result] finish) :
                        BitVec.toNat result = k.factorial ∧ finish = entry

                        Any completed invocation has the mathematical factorial as its returned natural number when that result fits. This is a property of the implementation, not the definition of its return value.

                        Length calculation on the actual generated setup and return instruction lists. This includes all individual saves, restores, and jumps.

                        Functional recursion is specified without an instruction budget.

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

                          The ghost argument records the mathematical input. Neither its proposed time bound nor the mathematical factorial is part of the runtime program.

                          Equations
                          Instances For

                            Simplify recursive invocation budgets using the generated-code length calculation above, so continuation proofs need not expand ABI internals.

                            Recover the full body-state specification for clients that inspect local effects. This unfolds one body step and reuses the callable factorial theorem; the stronger local endpoint is proved explicitly, not inferred from return-state restoration or from a time bound.

                            A single mathematical induction proves the exact recurrence T (k + 1) = T k + 37. Public cost rules handle the measured branches, recursive call and straight-line continuation; no callee state is reconstructed.

                            The independent upper bound follows from the exact cost equation, without another recursive proof or a proposed budget in the functional specification.

                            theorem Ram.Examples.Factorial.function_timeBound {w : ℕ} (H k : ℕ) (hk : k < 2 ^ w) :
                            Source.FunctionTimeBound 2 program H k factorial (fun (args : List (Word w)) (x : Source.State w) => args = functions.arguments.factorial (BitVec.ofNat w k)) fun (x : List (Word w)) (x_1 : Source.State w) => 37 * k + 4

                            The function-body time bound is independent of its result specification. The enclosing call's argument and frame costs are accounted for separately.

                            Combine independently proved behavior and time for the recursive body.

                            theorem Ram.Examples.Factorial.call_refines {w : ℕ} (H depth : ℕ) (caller : Source.State w) :
                            Source.Refines program H depth (Stmt.call [1] self [Expr.var 0]) (fun (k : ℕ) (s : Source.State w) => s = caller ∧ k < 2 ^ w ∧ k + 1 ≤ depth ∧ s.regs 0 = BitVec.ofNat w k) (fun (result : ℕ) (t : Source.State w) => t = caller.setReg 1 (BitVec.ofNat w result)) Nat.factorial

                            The call implements ordinary Nat.factorial through its word encoding. The captured caller is only a representation ghost: all other registers, memory and I/O are preserved, while factorial itself is allowed to wrap.

                            theorem Ram.Examples.Factorial.body_contract {w : ℕ} (H k : ℕ) :
                            Source.RelContract 2 program H k factorial.body (fun (s : Source.State w) => k < 2 ^ w ∧ s.regs 0 = BitVec.ofNat w k) (fun (entry finish : Source.State w) => finish = entry.setReg 1 (value w k)) fun (x : Source.State w) => 37 * k + 4

                            The recursive contract exposes the complete body-state postcondition, not merely the final return value. Its proof does not use the exact trace.

                            theorem Ram.Examples.Factorial.body_measured {w : ℕ} (H k : ℕ) (s : Source.State w) (hk : k < 2 ^ w) (hn : s.regs 0 = BitVec.ofNat w k) :

                            Every input in range terminates. The exact endpoint preserves the entire caller state except its answer register, not just the final numeric result.

                            theorem Ram.Examples.Factorial.call_measured {w : ℕ} (H k : ℕ) (s : Source.State w) (hk : k < 2 ^ w) (hn : s.regs 0 = BitVec.ofNat w k) :
                            Source.LocalMeasuredExec 2 program H (k + 1) (Stmt.call [1] self [Expr.var 0]) (37 * k + 32) s (s.setReg 1 (value w k))

                            The public call restores every caller local other than its destination.