Documentation

Examples.Ram.FactorialFunction

Mathematical reasoning about the implemented factorial function #

eval and bodyTime observe the one function defined in Ram.Examples.Factorial.functions. They do not execute its optional stream driver. The definitions mention neither mathematical factorial nor a proposed cost; the equations below establish those properties of the implementation.

The initial state is hidden once, then eval_eq_of_execution proves that this view agrees with the returned value at every actual caller state. The example uses mathlib's Part: these are noncomputable proof views, not a new interpreter or ordinary Lean code accepted by the RAM compiler. Word overflow remains explicit, and the time observation excludes the enclosing call overhead.

noncomputable def Ram.Examples.FactorialFunction.eval {w : ℕ} (n : Word w) :

Observe the implemented function's decoded return value, without an I/O main.

Equations
  • One or more equations did not get rendered due to their size.
Instances For
    noncomputable def Ram.Examples.FactorialFunction.bodyTime {w : ℕ} (n : Word w) :

    Observe its actual body count, independently of any bound or specification.

    Equations
    Instances For

      Every word argument terminates with factorial modulo the word range.

      When the result fits, an ordinary function-value equation states correctness.

      theorem Ram.Examples.FactorialFunction.eval_pos {w : ℕ} (n : Word w) (hfit : (BitVec.toNat n).factorial < 2 ^ w) {result : ℕ} (hresult : result ∈ eval n) :
      0 < result

      Mathematical properties of the observed value reuse mathlib directly.

      theorem Ram.Examples.FactorialFunction.eval_eq_of_execution {w heapLimit depth : ℕ} {n result : Word w} {entry finish : Source.State w} (execution : Source.FunctionExec Factorial.functions.program heapLimit depth Factorial.factorial (Factorial.functions.arguments.factorial n) entry [result] finish) :
      eval n = Part.some (BitVec.toNat result) ∧ finish = entry

      Hiding the initial state did not change the function's meaning: the view agrees with a call in any shared state, and that state is preserved on return.

      The time equation is proved from the existing exact body execution, separately from the function's result theorem.

      theorem Ram.Examples.FactorialFunction.bodyTime_eq_of_execution {w control heapLimit depth steps : ℕ} {n result : Word w} {entry finish : Source.State w} (execution : Source.FunctionMeasuredExec control Factorial.functions.program heapLimit depth Factorial.factorial (Factorial.functions.arguments.factorial n) steps entry [result] finish) :

      The time observation agrees with every actual invocation, not just the canonical initial state hidden by bodyTime.