Documentation

ComplexityDocs.Verification.Loops

Source loops and recursion #

Correctness guide · Manual

While contracts and termination #

The typed core also has an effectful-guard Stmt.while. Its guard is an actual Boolean-producing block: it runs in the current state on every iteration, and even its false exit retains local and heap updates. A normally completing body repeats the loop; a return exits the enclosing function. A guard that falls through without returning a Boolean faults. TotalWP.while_wellFounded and while_variant require mathematical descent only after a full normal cycle, not on a false exit or early return. These are source rules, without a time budget. The native loop interface gives a one-step equation for the same Stmt.action and Stmt.while_spec for strict Std.Do reasoning. Its guard and body are actual source blocks, not a second host loop. The ordinary-local view represents the complete lexical environment by ordinary values, retaining the final locals on every exit. Its loop rules take an invariant and well-founded relation, or a natural-valued variant, on those values and the current heap. The continuation bridge runs the remaining function only on normal completion. These are proved changes of view of the same source execution, not another implementation. Named while and bounded for generate actual guard/body/loop observations, one-step equations and mathematical block contracts. A guard_contract describes the Boolean decision and actual state after testing; a separate body_contract describes normal completion or early return. Their predicates take ordinary mutable arguments and initial/final heaps. They are native strict triples, packaged by BlockSpec, not a second operational semantics.

Use variant_contract to compose these facts with a natural-valued measure, or wellFounded_contract with an ordinary Lean well-founded relation. The body starts at the actual post-guard state; a normal iteration must decrease relative to the state before the guard. False exits and early returns need no descent. Generated guard_spec, body_spec and spec apply a chosen contract to a native continuation. Immutable captures are restored internally from proved execution frames; preserving a buffer descriptor does not preserve its contents. The older variant_spec and wellFounded_spec rules remain available for direct native round proofs. None of these source rules asks for fuel or an instruction budget.

For a traversal with a natural count increasing by one, count_frame_contract composes the existing guard/body contracts with a transitive heap relation. Supply the count, its fixed upper bound, the invariant, the per-step frame and the exit consequence. The shared rule proves descent by the remaining count and accumulates the frame from the supplied initial heap, which need not be the current loop-entry heap. The guard contract must preserve the count and heap and establish the invariant at its actual output; other mutable locals may change. The body contract must exclude early returns. The borrowed traversal and buffer copy use this entry without manual guard/body consequence rules. Array updates, bounds and disjointness remain their mathematical obligations; no effects or cost bounds are inferred.

Mathematical locals for represented rounds #

When a normal loop round has checked mathematical operation contracts, the frontend also generates Model, modelGuard, modelStep, model_contract and model_spec under the same named source loop. mkModel accepts source-variable parameter names; model_<name> reads a mathematical local. The representation of that local at the actual heap is available through <name>_representation and model_rel_<name>. Complete source coordinates remain in the connection proof, not in the author's invariant.

The array-record loop uses, for example,

let invariant : Model → Prop := fun model =>
  (model_state model).copies + model_remaining model = initialValue.copies + count

The author proves preservation under modelStep, descent when modelGuard is true, and the desired result when it is false. model_spec then supplies the actual loop contract to mvcgen. The generated round proofs compose existing operation correspondence and preservation contracts at the actual intermediate heaps; no Part.bind or raw environment transport is supplied by this consumer.

This pure-round interface requires justified preservation of captured array observations. It needs no pure model of the complete loop, adds no source helper calls and does not infer a RAM bound.

For local-return rounds, the same source-field representations generate Model, mkModel and field observations without requiring a pure round trace. guard_model_contract and body_model_contract lift supplied source contracts to mathematical locals at the actual heaps. An author chooses a mathematical index and encode : Index → Model; captures can remain ordinary parameters. model_completion_contract then combines the guard/body contracts, a heap-dependent invariant, a mathematical progress relation and its well-founded decrease, and the normal/completed result predicates.

The scoped worker loop uses its remaining count as the index and mkModel's source-variable names to select its state. Its proof reuses the worker's real contents contract and ordinary invariant lemmas, without tuple, Part or hidden-entry transport. A completed body needs no next invariant or decrease. The rule requires the supplied guard contract to preserve the mathematical index and actual heap; it may use the current invariant to justify its accesses. No heap-preservation restriction is imposed on the body. For a guard that changes locals or the heap, use model_completion_effects_contract. Its supplied prepared before initial tested after relates the round's input to the guard's actual output. The body contract starts at tested, after; only a continuing body must reestablish the invariant and decrease relative to before. A false guard supplies the exit condition at its own final heap. The simpler model_completion_contract is a specialization of this rule. This composition does not infer the guard's effects or a frame for aliased observations. A raw Buffer field observes its handle, not unchanged contents, and this interface does not turn aliased mutation into an assumed pure transition. The completed result remains the actual source return value; its contents are proved by the result predicate, not automatically converted into a mathematical Array or List.

For complete identity-represented locals, guard_model_contract_iff and body_model_contract_iff turn these contracts into native triples over Model. Use the corresponding actual guard/body equation and mvcgen with the real operation contracts. The generated modelEquiv only rearranges source fields; guard_model_action and body_model_action map the result of the existing action, preserving its control and heap. They are proof observations, not additional source programs. The scoped loop proves its guard/body facts this way and reuses them for both loop correctness and resource composition; its older visible-local theorems are short compatibility consequences. This direct triple entry currently requires a complete identity representation. General Array/List observations retain their heap-indexed relational contracts.

Finite ranges and resource rules #

Represented finite ranges with normally continuing, fully modeled bodies have an ordinary List.foldl view, including allocating List iterations inside a typed local do value block. The generated proof retains the real loop's pending-controlled guard and increment and proves the result slot stays empty; authors do not expose that slot in their mathematical proof. The existing List range keeps its replicate equation in this form.

Fully modeled if/Option rounds that may return from the local value block, including nested mixed return/continue branches, use a control-sensitive Option payload × state summary and Lean forIn instead of a fold. Its heap-indexed correspondence observes the same actual body result and final locals: the active pending slot may change, while ancestor slots and fixed captures remain preserved. The payload type is the local block's result type, independent of the enclosing function's return type. Continuing rounds advance the cursor; completed rounds exit through the real masked guard without running another body or inventing a decrease. The remaining statements inside the local block run only when no result was saved. This adds no second source loop or allocation for proof-side state; actual body allocations and control instructions remain part of the compiled program. Branch summaries carry only mutable lexical slots, including shadowed bindings; immutable observations use proved heap preservation. The shared proof boundary for supported typed do, if and Option/List match value bindings lets finite-range/Option models carry updated outer mutable bindings alongside the terminal payload. The code after the binding consumes both through their actual heap-indexed relation, retaining the original raw return markers and joins; this is not an unchanged-heap exact equation or a general while model.

The prepared completion-range mathematical forIn carries Option Result × Mutable: the iteration index remains a callback argument, but the cursor is erased from the accumulator and immutable captures are closed over. Stmt.forIn_range_step_completion_eq reuses forIn_range_hom twice to connect this view to the full state, requiring fixed-field preservation on both continuing and completing rounds. Its step still projects the full mathematical body result. For functions whose trace contains a completion range, Family.f_model_eq exposes a locally reduced body of Family.f_model, or Family.f in (native) mode. Use the equation explicitly; it is not registered as a global simp rule. It moves Prod.map through Option.elim/if using Option.elim_comp and apply_ite, and reduces Id pure/bind and concrete tuples. This removes the per-field callback connection proof in the checked List consumer while retaining its mathematical induction; nested branches are also checked. It does not promise a simplest normal form for arbitrary callbacks. The same source loop, actual heap and body trace remain the basis of correspondence and resource proofs.

The default represented frontend also gives fully modeled finite-range bodies with mixed if/Option branches a mathematical model and checked correspondence for direct function returns. This uses the actual Control.returned/whileReturn path, not an added local value block or pending-result slot. A returned (some) round keeps the real payload and endpoint heap without advancing the cursor or running an extra false guard. Raw lowering is unchanged; the mathematical model does not select another returned value or executable loop.

Automatic action correspondence and refinement are also checked for a two-level finite-range nest with an inner function return, retaining actual payloads, locals and heaps. A completed forIn result may be shared in the proof when composing the continuation; this adds no executable binding or changed cost. Normal and function-returning finite ranges also compose enclosing self-calls with descent from fixed captures or the actual range bound. Correspondence uses actual fixed slots and representation functionality at the current heap to recover those captures; arrays still need contents frames. Broader nested-loop combinations, mixed local/function completion and full models of arbitrary heap mutation remain open. None of these mathematical range views infers a RAM bound for its body or control.

For pure finite Nat ranges, the shared while_range_encoded_invariant rule reuses generated native correspondence and retains the supplied mathematical invariant at normal exit. It supplies range termination, leaving the actual guard/body word conditions and invariant preservation to the client. The structured accumulator uses it to connect ordinary fold/sum correctness and its independent full invocation bound to the same halted RAM result. In addition to launch capacity, its range conditions include the final sum, computed stride and last actual cursor, which may exceed the stopping endpoint. Its connection proof still selects generated local views; that remaining transport is not a new mathematical proof of the algorithm.

The loop-coordinate tactic normalizes generated local views without repeating their projection lemmas:

ram_source_locals StructuredRange.sum_loop1
ram_source_locals Implementation.boundedMap_loop1 at bound

The loop namespace selects its registered coordinate, native-encoding and frozen range-endpoint equations. Standard locations also support at h ⊢ and at *; without a location only the target is rewritten. The tactic does not unfold program bodies, callees, invariants, value-range predicates or costs, and does not search for arithmetic proofs. Both the structured accumulator and mutable traversal use this pass. Selecting the loop contracts and supplying their mathematical obligations remain separate from coordinate normalization.

For a statement goal retaining its generated loop Code, the named-loop resource tactics also select the capture view and its checked guard/body preservation proofs:

ram_source_loop_cost (remaining := fun locals _ => contents.size - locals.1)
  using (guard_contract xs limit contents), (body_contract xs limit contents)
  costs guard_costBound, body_costBound

ram_source_loop_realize
  using (guard_contract xs limit contents), (body_contract xs limit contents)
  total loop_contract xs limit contents heap

The supplied block contracts determine the invariant and actual guard/body relations. The cost form applies uniform guard/body certificates; the author still proves entry into the body, invariant preservation, decrease, positivity and the initial invariant. The realization form converts the source loop contract to totality, keeping its precondition separate from the resource invariant. For example, the traversal's source precondition additionally carries its outside-buffer frame. Routine true/false postcondition consequences are discharged; other consequences, actual word/nesting proofs and invariant preservation remain goals. The enterBody case names the true-guard implication. The compiled traversal uses both entries without writing capture views, frame theorem names, environment repacking or its own BlockSpec → TotalWP conversion. Mathematical mutable-coordinate patterns remain in its proof leaves.

Apply these tactics before unfolding the named Code. Already expanded loops and arbitrary potential functions can still use the public loop theorems below; the cost tactic is the uniform-cost, linear-iteration specialization, not a replacement for those general interfaces or automatic invariant discovery.

For a named local-return loop with mathematical guard/body contracts and an existing finite execution, use

ram_source_loop_arena_model (encode := loopModel out count n value)
  using (fun left heap _ =>
    guard_model_spec out count n value left (fun current => current = heap)),
    (fun left heap current active => body_model_spec out count n value
      (remaining := left) (heap := heap) current (of_decide_eq_true active))

This entry selects the actual named loop and reuses its source contracts through ArenaReady.while_completion_model_of_exec. The supplied contracts determine the invariant, test, transition and completed result relation. There is no second invariant-preservation or termination proof. The remaining leaves are the real guard/body readiness, saved-result readiness, entry invariant and successful-control condition. For a complete identity representation, a proved correspondence removes raw entry coordinates from guard/body leaves. A general heap-indexed representation remains a relation; there is no inferred inverse from mathematical array contents to a unique handle.

The general ArenaReady.while_completion_model_effects_of_exec accepts the same effectful guard/body contracts and adds readiness at the actual post-guard model and heap. It reuses the given finite execution without requiring descent again. The preserving-guard rule above is a specialization. This is still a fixed-cursor rule: heap mutation and reclaimed scratch are allowed, but retained allocation cannot silently reset the cursor. The named tactic above selects the preserving specialization; the general rule is currently used directly.

The lower-level visible-local entry remains available:

ram_source_loop_arena (stateRel := invariantRelation)
  (prepared := bodyPrecondition) (completed := resultPostcondition)

The three relations concern visible source locals and actual heaps. This entry selects the registered completion coordinates and execution frames, then applies ArenaReady.while_completion_of_exec. Guard/body source contracts, their real resource readiness, readiness of the final stopped guard, and the initial facts remain obligations. Neither entry reconstructs private completion slots in the author's proof or asks for loop termination again. Both preserve the same arena boundary between rounds, not arbitrary growing allocation, and does not infer a time budget. For the final stopped guard, RealizationWP.localReturn_guard_some uses the saved result's actual word-range proof. It follows the option match and false return without evaluating the original test; .arenaReady then applies this fact to the same finite execution. The scoped consumer needs no private proof about the compiler's payload slot for this branch.

For the actual named Guard or Body, ram_source_fragment_realize normalizes registered source coordinates and runs the existing structural realization pass. For a standalone call followed by a fixed-placement continuation, use:

ram_source_fragment_arena_call using (by
  intro finish returned called
  cases returned
  exact work_ready out n value heap hw outRooted outFits nFits valueFits capacity called)
all_goals omega

This is the scoped consumer's actual worker certificate; the cases only eliminates its Unit result, not an execution tree. The shared ArenaReady.call_seq_of_exec handles the call and sequence, keeping the callee's real final heap and restoring caller locals. Argument ranges are normalized into ordinary goals. The tactic retains the callee's arena cursor through the continuation; allocating continuations use the general rule directly. It does not infer callee contracts, capacity, loop invariants or time bounds.

Reuse a traversal invariant #

The read/helper/branch/write traversal writes for i in [:xs.length]. Its invariant describes the processed prefix and unread suffix of an ordinary array. Independent guard/body contracts feed the loop contract; the author proves array identities, actual frames and descent rather than constructing a nested triple for a whole iteration. The generated spec then composes that contract with the function's continuation. Selecting these contracts and mathematical contents remains explicit. The compiled traversal reuses this source proof to establish the same array result and an independent linear instruction bound for the actual halted invocation. Its word ranges, preloaded heap representation and code/stack capacity are explicit. The realization loop rules reuse source termination without a second decreasing measure, and the ordinary-local cost rules compose actual guard/body bounds and the source invariant. Their fixed-capture rules reuse generated lexical preservation, leaving only mutable locals and the heap in the author's invariant and potential. StmtCostBound.while_contract_fixed and RealizationWP.while_contract_fixed_of_total consume the same independent guard/body contracts. The library extracts their mathematical facts at the actual execution points using BlockSpec.post_of_eq; authors do not reconstruct operational observation equations. They supply the logical connection from a true guard to the body's precondition, range evidence and numerical potential inequalities. The source invariant and termination proof are reused. The BlockSpec.mono consequence rule strengthens the input and weakens either successful relation, while retaining the original precondition as a usable fact. Mathematical invariants, frame composition and potential inequalities remain explicit; correctness contracts themselves contain no instruction budget.

When an actual Exec is already available, BlockSpec.post_of_exec applies the same contract directly at that execution's endpoint. Its input map may install fixed captures or a local block's empty result slot; the result still observes the complete actual final locals and heap. No client-side Part equation or second execution witness is needed.

The mathematical arena loop rule reuses source guard and normal-body BlockSpec contracts at actual intermediate heaps. The guard preserves the mathematical model, not necessarily the heap. The fold cost proof uses its public mathematical round contracts, without repeating execution transport; its program, signature and remainingCost + 17 bound are unchanged. Authors supply component bounds, an invariant and potential inequalities; the rule bounds existing executions without another termination proof.

For finite ranges, while_range_rel reuses the source guardRel/bodyRel for normal rounds and direct function returns. Direct returns pay the existing +17 control charge without running a later guard. The body budget includes the whole actual iteration, including any cursor increment executed. Local-pending completion instead uses while_range_completion_rel: the saved result leaves the body normally and exits through the real false guard. The bound includes that guard's cost plus the normal-round and false-exit overheads 10 + 11, rather than treating the result as a function return. For uniform guard and whole-body certificates, use while_range_rel_linear for normal/direct-return ranges, or while_range_completion_rel_linear for saved local completion. Both use positive stride, Std.Legacy.Range.size and the existing whileLinearBound to discharge the linear potential inequalities. The completion variant's guard certificate covers running and stopped paths; component bounds may be supplied by ram_source_arena_cost. These are bounds on the actual loop, not complete function RAM theorems; readiness and capacity remain separate obligations. The existing allocating prependRange consumer combines its generated round contracts with inferred guard/body bounds this way; it supplies neither a second loop induction nor register-level proofs. Eligible nonrecursive represented range sites expose stateRel, entry_rel and guard_rel/body_rel, or their _preserving variants, under the actual source loop. The entry and round facts come from the same checked correspondence proofs; correspondence reuses them without regenerating the body proof. Lean's closure retains captures, the initial heap, representation premises and actual pending-slot conditions. The generated Site.pending names the real slot for the running premise, without a handwritten lexical position. Recursive and shared-continuation proofs remain local; eligibility is conservative, including after value branches. No new source-program syntax is required. The named cost entry ram_source_range_arena_cost using related costs guardCost, bodyCost selects the same published contract from the goal's actual loop Code and related's registered stateRel; it does not switch to a preserving variant. Normal and direct-function-return ranges use this form without running; saved local completion requires using related, running instead. Optional facts [inputObserved] before costs supplies captured source observations needed by that contract. Such facts cannot be recovered from a runtime handle. Guard/body cost certificates remain independent proved bounds. At the actual initial state, use ram_source_range_arena_cost entry with the same optional facts [inputObserved] and costs guardCost, bodyCost. It applies the ordinary published entry_rel to infer the start, mathematical model and captures from the real state and supplied observations. Preserving variants and arbitrary represented states continue to use explicit using related or using related, running. ram_source_arena_cost preserves the named loop goal when it stops there, so the entry form can compose with that pass into an inferred enclosing-function cost certificate without restating the entry relation. The existing prependRange_costBound uses this composition for its unchanged function, with initialization added once. Its body envelope is affine in the range count by prependRangeBodyBound_eq. This entry composes uniform component bounds only; nonuniform rounds use the existing mathematical contract rules. Recursive and shared-continuation sites without published contracts are not covered. Positivity or nontrivial budget comparisons may remain goals. The range rule bounds only its loop; enclosing cost composition does not establish readiness, capacity or an actual whole-function RAM execution.

For nonuniform costs, the unchanged ArrayRangeNative.repeatAppend copies a growing array on each round. Its function_costBound reuses generated guard_model/body_model, while_model and the actual BufferCopy.append_costBound. The model_rel_state and model_rel_chunk observations concern each real heap, without inverting handles or requiring disjoint inputs or another termination proof. The finite-sum identity describes the body envelope, not an exact runtime count. Its all-input bound has form F + r * (G + D + 10 + A * (s + r * k)), with compiler-derived constants, where r is the iteration count, s the initial array size and k the chunk size. The mathlib asymptotic corollary is O(1 + r + r*s + r²*k), with no constraints on relative input sizes. These existing APIs need no new tactic, metadata or mathematical framework for this proof. These cost theorems alone establish a conditional certificate, not readiness, capacity, Program.TimeO or an actual halted-runner result.

Separately, ArenaReady.while_model_of_exec lifts the same finite normal-body loop through its existing guard/body contracts, allowing actual heaps and arena cursors to change. Fragment readiness and resource invariants remain supplied; no second loop induction or termination proof is needed. ArenaReady.while_model_of_measured delegates to this rule, handling view coordinates and at_exec witness unpacking. Measured guard/body postconditions can use the source contract's stateRel at the actual final heap to establish the resource invariant; no inverse of a handle or unchanged heap/cursor is required. The guard must preserve the mathematical model and the body must complete normally: this is not general early-return or model-changing-guard automation. ArenaMeasured.of_totalWP combines an existing TotalWP with a resource proof for that same Exec, taking its count from ready.exists_cost, not a new semantics, price or termination proof. ArenaMeasured.at_exec transports the same measured readiness, count and observations to a given source execution. The array-record loop's loop_ready proves finalCursor = initialCursor + reserve, where reserve = Σ (i < r), (s + (i + 1) * k). Its invariant retains word/element ranges and sufficient cumulative capacity; input aliases are allowed. Its measured rounds use mono_post to prove resource preservation; the reserve recurrence, invariant_step and zero reserve at the false exit remain the author's mathematics, not a new time bound. Each round allocates a fresh array without reclaiming old arrays, so this is cumulative allocation, not peak-live storage. The same function's function_resources bounds a supplied execution using this readiness. function_measured uses ram_source_arena_step using total as loopTotal on the original repeatAppend_copies.wp; of_totalWP then combines loopTotal with loop_ready. In general, ram_source_arena_step using total as currentTotal transports the existing TotalWP through primitive bindings and sequences, stopping at another statement with its matching proof named currentTotal. ArenaMeasured.of_totalWP_post retains the source postcondition on the same measured outcome, readiness and count. For sequences, normal completion supplies the continuation's totality at the actual intermediate state; early return requires neither execution nor termination of the suffix and adds no cost for it. This transport does not yet cross branches, calls or scopes. The ordinary structural pass composes the final return, and body_ready follows by at_exec; no handwritten environments, execution-case analysis or fixed-shape totality stripping are needed in this consumer. Its program, public contracts, ranges, capacity and counts are unchanged. The mathematical record certificate invariant_state_fits now composes the shared RepresentationFits rules and applies to actual entry, round and return observations; the chunk's descriptor uses the array rule. ram_source_arena_step consumes the resulting ValueFits facts without repeated descriptor/field projections. Scalar-update equations, algorithmic ranges, element bounds and capacity still need their original proofs. execute combines them with the existing cost certificate through ArenaMeasured.execute_le. It retains FunctionArenaLaunch word, code, stack, rooted-input and ArenaRep premises, plus the cumulative capacity condition, and bounds the actual halted runUntil result. Initialization, outer call and halt are counted once. The same outcome has exact cursor growth reserve and a represented output with output.copies = initialValue.copies + r; this is the original counter contract, not a complete expected-array formula. Input loading, Program.TimeO and peak-live space are not established by this theorem. General source-totality transport, range certificates for further layouts, allocating-loop setup and source-facing space contracts remain work.

Buffer.Disjoint permits different objects or disjoint Set.Ico intervals of the same object. Buffer.PreservesOutside xs initial finish says that every initially valid disjoint view retains its ordinary contents. Its write, trans and mono rules derive and compose this fact from actual successful writes; overlapping aliases remain allowed and observe their real updates. This is a contents-preservation relation, not whole-heap equality or handle ownership. The traversal's boundedMap_total_frame returns both the array-map result and this frame at the same final heap. Its compiled boundedMap_execute exposes that frame, the mathematical array result and the instruction bound against one typed FunctionExecution outcome. The outcome retains the actual halted runner and complete final machine memory; callers do not reconstruct the old witness tuple. The two-call and imported clients use the corresponding boundedMapPair_execute entry points. Neither array correctness nor register-level simulation is proved again. The original runUntil theorems remain available as lower-level compatibility interfaces.

Recursive calls #

For recursion, the source contract rule FunctionTotal.verify_wellFounded supplies complete callable specifications at smaller mathematical indices through Lean's WellFounded.induction. A fixed function selector gives ordinary recursion; a varying selector supports mutually recursive functions with different signatures. The index is mathematical proof data, not runtime fuel, an instruction budget or a stack-depth bound.

The default represented frontend checks self-recursion from normal and function-returning finite ranges using fixed captures or the actual range bound. Shared termination preprocessing uses Lean's wf_preprocess to expose index < stop to the author's decreasing proof; both index-recursive consumers retain decreasing_by omega. Generated correspondence reuses that proof at actual intermediate heaps. Ordinary equations remain foldl/forIn, without attached proof arguments or changed source control, heap effects or costs. The checked normal-range List result uses ordinary induction and fold equations, not a second source proof. General mixed local/function completion remains open, and no RAM cost bound is inferred. Generated model equations may still require deliberate simplification; unrestricted simp_all is not a guaranteed fast substitute.

For the supported pure fragment, the named factorial uses Lean's native termination machinery:

source_program (pure) Recursive where
  def factorial (n : Nat) : Nat := do
    if n == 0 then
      return 1
    else
      let previous ← factorial (n - 1)
      return n * previous
    termination_by n
    decreasing_by simp_wf; simp_all +zetaDelta; omega

The author supplies this decreasing argument once. Generated correspondence reuses the native recursion principle to prove successful source execution with the same result and unchanged initial heap, without a second implementation induction. Ordinary Nat induction separately proves the mathematical equation Recursive.factorial n = Nat.factorial n; the generated _total contract then transfers that result to the actual source program. The compiled factorial reuses that same source theorem. Its separate realization proof uses ram_source_realize (input) with the recursive realizability and source contracts. It bounds intermediate values by the representable factorial result and bounds nested calls by n; parameter environments and statement-level target conversion are internal. Its independent cost induction uses ram_source_cost (input) and, in the successor case, ram_source_cost (input) using ih, with actual generated call overhead. Argument environments are opened internally; ordinary induction, recursive-input facts and the final arithmetic inequality remain the author's proof. The resulting halted-runner theorem returns factorial, preserves shared entry memory and retains the generated code and stack-capacity premises. The linear word-RAM instruction bound is in the numeric argument n, not its binary bit length or the cost of arbitrary-precision multiplication. These backend range, nesting and cost arguments do not repeat the source termination proof. Pure mutually recursive families and a convenient named proof interface for effectful recursion remain open; automatic pure correspondence is not a promise of automatic termination transport for arbitrary effectful actions.