Documentation

Examples.Ram.FactorialStream

An optional stream driver for factorial #

This program reads one word, calls the independently defined factorial function, and writes its result. It reuses that function's correctness and time theorems without changing its declaration or requiring a second implementation.

The whole-program execution includes the input header, input/output instructions, call overhead and halt. Its result is factorial modulo the word range; an exact natural-number result additionally requires that factorial fits in one word. The linear time bound uses the numeric input, not its binary encoding length.

An optional stream adapter around the independently defined function.

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

        The actual input/call/output program uses the callable mathematical model without an instruction budget. The word result retains its modular meaning.

        theorem Ram.Examples.FactorialStream.main_contract {w : ℕ} (H k : ℕ) (hk : k < 2 ^ w) :
        Source.Contract 2 Factorial.program H (k + 1) main (fun (s : Source.State w) => s = Source.State.initial [BitVec.ofNat w k]) (fun (t : Source.State w) => t = sourceFinal w k) fun (x : Source.State w) => 37 * k + 35

        Verify the complete input/call/output workflow using only the recursive contract and WP rules. No measured-execution constructor is used here.

        The complete source program consumes one input and emits one result.

        The user-facing named entry point emits exactly the verified local-frame executable used by the full execution and complexity theorems below.

        The code contains one recursive function body, not k unrolled copies.

        theorem Ram.Examples.FactorialStream.runs_via_contract {w : ℕ} (H k : ℕ) (hk : k < 2 ^ w) (hcodefit : 60 < 2 ^ w) (hstackfit : H + (k + 1) * 3 < 2 ^ w) :
        ∃ (finish : State w), TerminatesWithin code (37 * k + 37) (State.initial [BitVec.ofNat w H, BitVec.ofNat w k]) finish ∧ finish.output = [Factorial.value w k] ∧ finish.input = []

        The independent contract proof compiles to the same halted executable and whole-program budget as the exact trace proof below.

        theorem Ram.Examples.FactorialStream.runs {w : ℕ} (H k : ℕ) (hk : k < 2 ^ w) (hcodefit : 60 < 2 ^ w) (hstackfit : H + (k + 1) * 3 < 2 ^ w) :
        ∃ (finish : State w), Exec code (37 * k + 37) (State.initial [BitVec.ofNat w H, BitVec.ofNat w k]) finish ∧ finish.status = Status.halted ∧ finish.output = [Factorial.value w k] ∧ finish.input = []

        Exact whole-program execution, including argument evaluation, individual frame saves/restores, return jumps, the stack-header read, and the final halt. The input starts with the ABI stack header H, followed by the user's k. There are no heap reads in this program, so any nonnegative boundary works.

        theorem Ram.Examples.FactorialStream.runs_mod_factorial {w : ℕ} (H k : ℕ) (hk : k < 2 ^ w) (hcodefit : 60 < 2 ^ w) (hstackfit : H + (k + 1) * 3 < 2 ^ w) :

        The emitted word decodes to exactly factorial modulo 2^w, for every legal input, including inputs whose factorial does not fit in one word.

        theorem Ram.Examples.FactorialStream.runs_factorial_of_lt {w : ℕ} (H k : ℕ) (hk : k < 2 ^ w) (hcodefit : 60 < 2 ^ w) (hstackfit : H + (k + 1) * 3 < 2 ^ w) (hresult : Factorial.factorialNat k < 2 ^ w) :

        If the mathematical factorial fits as well, the same execution returns that exact natural number, without a modular reduction in its specification.

        External input representation: an empty-heap stack header, then the input number as one unsigned word. Encoding is not an extra machine instruction.

        Equations
        Instances For

          Space for the fixed code and all simultaneously live recursive frames. This also ensures that the input number itself has an exact word encoding.

          Equations
          Instances For
            Equations
            Instances For

              A uniform bound for the same code at every admissible input and width, with the ordinary terminating RAM execution judgment as its meaning.

              Linear time in the numeric value k, uniformly over admissible widths. This is not linear time in the binary encoding length of k (nor in the number of input words, which is constant for this particular interface).