Documentation

Examples.Language.Remainder

Remainder from mathematical division, multiplication and subtraction #

The nested source expression is compiled into actual primitive bindings. Its result follows from the ordinary Nat division identity, including divisor zero. All intermediate values fit whenever the two inputs fit; subtraction retains its saturating Nat meaning. The separate count concerns this division-based implementation, not the backend's single remainder instruction. Generated correspondence transfers the native function's result to source execution and preserves every initial heap.

@[reducible, inline]

Apply a predicate, postcondition or bound with ordinary source parameters to the actual argument environment. No new contract or execution is introduced.

Equations
Instances For
    theorem Complexity.Language.Examples.Remainder.Implementation.remainder_total_iff (pre✝ : ℕ → ℕ → Heap → Prop) (post✝ : ℕ → ℕ → Heap → ℕ → Heap → Prop) :
    remainder_contract pre✝ post✝ ↔ ∀ (n d : ℕ) (initialHeap : Heap), pre✝ n d initialHeap → ∃ (value : ℕ) (finalHeap : Heap), remainder_action n d initialHeap = Part.some (Except.ok value, finalHeap) ∧ post✝ n d initialHeap value finalHeap

    The source contract is equivalent to ordinary curried preconditions and successful result/heap postconditions, including termination and absence of faults.

    Executable total value function generated from the same buffer-free source block.

    Equations
    Instances For
      theorem Complexity.Language.Examples.Remainder.Implementation.remainder_spec {pre✝ : Env [Ty.nat, Ty.nat] → Heap → Prop} {post✝ : Env [Ty.nat, Ty.nat] → Heap → ℕ → Heap → Prop} :
      FunctionTotal program remainderId pre✝ post✝ → ∀ (n d : ℕ) (continuation : Std.Do.PostCond ℕ (Std.Do.PostShape.except Fault (Std.Do.PostShape.arg Heap Std.Do.PostShape.pure))), ⦃fun (initialHeap : Heap) => { down := pre✝ (Env.cons n (Env.cons d Env.empty)) initialHeap ∧ ∀ (value : ℕ) (finalHeap : Heap), post✝ (Env.cons n (Env.cons d Env.empty)) initialHeap value finalHeap → (continuation.1 value finalHeap).down }⦄ remainder_action n d ⦃continuation⦄

      Apply a supplied source contract with ordinary named arguments and its actual returned value and final heap. No callee body or new contract is inferred.

      The finite table of actual named source bodies.

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

        The named function's actual independently interpreted source body.

        Equations
        • One or more equations did not get rendered due to their size.
        Instances For
          @[reducible, inline]

          Budget-free total correctness with ordinary source parameters and relational initial/final heap postconditions. This is the existing source function contract.

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

            Fold an actual source invocation back to its named ordinary-argument observation.

            @[reducible, inline]

            The actual declared argument environment, constructed from ordinary source parameters. This is proof-side parameter transport, not an additional runtime operation.

            Equations
            Instances For
              theorem Complexity.Language.Examples.Remainder.Implementation.remainder_contract_iff_triple (pre✝ : ℕ → ℕ → Heap → Prop) (post✝ : ℕ → ℕ → Heap → ℕ → Heap → Prop) :
              remainder_contract pre✝ post✝ ↔ ∀ (n d : ℕ) (initialHeap : Heap), ⦃fun (currentHeap : Heap) => { down := currentHeap = initialHeap ∧ pre✝ n d initialHeap }⦄ remainder_action n d ⦃(fun (value : ℕ) (finalHeap : Heap) => { down := post✝ n d initialHeap value finalHeap }, fun (x : Fault) (x_1 : Heap) => { down := False }, PUnit.unit)⦄

              Prove the ordinary-parameter source contract with a total-correctness triple for the same action. The ghost initial heap equals the actual entry heap; successful results use the actual final heap, and faults are excluded.

              theorem Complexity.Language.Examples.Remainder.Implementation.remainder_total :
              FunctionTotal program remainderId (fun (x : Env signatures[remainderId].params) (x_1 : Heap) => True) fun (arguments : Env signatures[remainderId].params) (initialHeap : Heap) (value : Value signatures[remainderId].result) (finalHeap : Heap) => value = remainder arguments.head arguments.tail.head ∧ finalHeap = initialHeap

              Exact total source contract inherited from the generated native function.

              The actual source action terminates with the generated native result and preserves every initial heap. Lean's native recursion principle proves this once.

              theorem Complexity.Language.Examples.Remainder.Implementation.remainder_action_eq (n d : ℕ) :
              remainder_action n d = have operand := n.div d; have operand := operand.mul d; pure (n.sub operand)

              One source-body equation in ordinary monadic notation; named callees remain opaque.

              The named function's actual partial source action, with ordinary typed arguments and an explicitly supplied shared heap.

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

                The source implementation has Lean's remainder semantics, also at zero.

                An ordinary mathematical specification of the same source function.

                Neither a positive divisor nor a nonnegative machine subtraction premise is needed. The emitted code implements all mathematical inputs that fit.

                The straight-line body bound includes division, multiplication, saturating subtraction, the return and the function-body wrapper.

                theorem Complexity.Language.Examples.Remainder.remainder_functionExec {w heapLimit : ℕ} (hw : 0 < w) (n d : ℕ) (_sourceHeap : Heap) (hn : n < 2 ^ w) (hd : d < 2 ^ w) (entry : Ram.Source.State w) :

                Generic lowering supplies the actual callable result without a register proof.