# `ReductionBounds`
[🔗](https://github.com/zacky1972/reduction_bounds/blob/v0.1.0/lib/reduction_bounds.ex#L1)

Verifies Lean declarations and infers symbolic costs and integer sizes for supported code.

## Examples

Run these examples in `iex -S mix` from the project root. Define a
list search and a list length function, then retain the compiled BEAM binary.

```elixir
import Lynx.Lean

{:module, SearchDemo, beam, _} =
  defmodule SearchDemo do
    @compile :debug_info

    def contains(_key, []), do: false
    def contains(key, [head | _]) when head === key, do: true
    def contains(key, [_ | tail]), do: contains(key, tail)

    def list_length([]), do: 0
    def list_length([_ | tail]), do: 1 + list_length(tail)
    def forward_length(xs), do: list_length(xs)
  end

target = {SearchDemo, :contains, 2}
```

Use `verify/2` with an inline Lean postlude to check that searching an
empty list returns the Erlang atom `false`.

```elixir
{:ok, [{^target, {:ok, _reports}}]} =
  ReductionBounds.verify([target],
    beam_binaries: [beam],
    postlude: ~LEAN"""
    theorem containsEmpty :
        Elixir.SearchDemo.«contains/2» (.integer 7) .nil = .ok (.atom "false") := by
      rfl
    """
  )
```

Use `infer_search_bound/2` to obtain a bound for the symbolic cost model.

```elixir
{:ok, bound} = ReductionBounds.infer_search_bound(target, beam_binaries: [beam])
:symbolic_cost = bound.metric
{:add, {:max, :c_empty, :c_hit}, {:mul, :n, :c_step}} = bound.upper_bound
```

Here, `n` bounds the input list length. The symbolic parameters account
for an empty list, a hit at the head, and one step that continues with
the tail. Lean checks the bound on this model. Relating those parameters
to ERTS reduction charges requires a separate analysis.

Use `infer_list_bound/2` to apply the same analysis rules to the search
and the length function.

```elixir
{:ok, search} = ReductionBounds.infer_list_bound(target, beam_binaries: [beam])

{:ok, length} =
  ReductionBounds.infer_list_bound({SearchDemo, :list_length, 1}, beam_binaries: [beam])

:symbolic_cost = search.metric
:symbolic_cost = length.metric

{:add, base, {:mul, :n, step}} = length.upper_bound
{:add, {:charge, 0, :dispatch}, {:charge, 0, :value}} = base

{:add, {:charge, 1, :dispatch},
 {:add, {:charge, 1, :recursive_call}, {:charge, 1, :addition}}} = step
```

The bound for `list_length/1` has the form `base + n * step`. Each
`{:charge, clause_index, operation}` identifies a symbolic cost by the
clause's zero-based index and operation. The step includes the addition
performed after the recursive call returns.

The `refined_upper_bound` field distinguishes reaching the empty list
after at most `n` steps from stopping on a nonempty list after at most
`n - 1` steps. At `n = 0`, only the empty-list cost applies.
To use the symbolic bound for ERTS reductions, map the compiled paths
to the modeled charges and justify each charge under the allowed VM
states for the target build, including the available reduction budget.

### Derive a bound under an input contract

Continue in the same IEx session, using the compiled `beam` and the
`~LEAN` sigil from `Lynx.Lean`. This contract admits lists of fewer than
100 positive integers.
Positivity is the invariant, and the sum of the entries is the size
measure. Removing a positive head decreases the measure by at least one.

```elixir
contract = ~LEAN"""
def inputInvariant (xs : List Int) : Prop := ∀ x ∈ xs, x > 0
def inputPrecondition (xs : List Int) : Prop :=
  xs.length < 100 ∧ inputInvariant xs
def inputSize (xs : List Int) : Nat := (xs.map Int.toNat).sum

theorem inputInvariantInit (xs : List Int) :
    inputPrecondition xs → inputInvariant xs := by
  intro precondition
  exact precondition.2

theorem inputInvariantTail (head : Int) (tail : List Int) :
    inputInvariant (head :: tail) → inputInvariant tail := by
  intro invariant x member
  exact invariant x (List.mem_cons_of_mem head member)

theorem inputSizeTail (head : Int) (tail : List Int) :
    inputInvariant (head :: tail) → inputSize tail + 1 ≤ inputSize (head :: tail) := by
  intro invariant
  have positive := invariant head (by simp)
  simp [inputSize]
  omega
"""

options = [beam_binaries: [beam], input_contract: contract]
callee = {SearchDemo, :list_length, 1}

{:ok, %{size_measure: :input_size, refined_upper_bound: callee_bound}} =
  ReductionBounds.infer_list_bound(callee, options)
```

Lean checks the three contract lemmas and derives that the list length
is at most the custom measure. In the returned formula, `n` denotes
`inputSize xs` for inputs satisfying `inputPrecondition xs`. For example,
`[2, 5, 1]` gives `n = 8`.

### Reuse the callee's bound in a wrapper

Pass the wrapper and callee as separate targets, with the same contract.
The wrapper forwards its argument unchanged within the same module.

```elixir
caller = {SearchDemo, :forward_length, 1}

{:ok, %{size_measure: :input_size, upper_bound: {:add, :caller_cost, ^callee_bound}}} =
  ReductionBounds.infer_list_call_bound(caller, callee, options)
```

The pinned pattern checks that the wrapper formula contains the same
refined bound as the preceding call. `infer_list_call_bound/3` checks the
callee theorem again and applies it in the same Lean run. `caller_cost`
represents the wrapper's own symbolic cost.

## Inspect integer ranges and intermediate widths

Define an integer function and retain its BEAM binary, then supply an
independent size description for each argument.

```elixir
alias ReductionBounds.IntegerSize

{:module, SizeDemo, integer_beam, _} =
  defmodule SizeDemo do
    def update(x) do
      y = x * 3
      if y < 0, do: y - 1, else: y + 1
    end
  end

{:ok, sizes} =
  ReductionBounds.infer_integer_sizes({SizeDemo, :update, 1},
    beam_binaries: [integer_beam],
    input_sizes: [%IntegerSize{lower: -2, upper: 4, magnitude_bits: 3}]
  )

sizes.result_size
#=> %IntegerSize{lower: -7, upper: 13, magnitude_bits: 6}
sizes.intermediates
sizes.unresolved_sizes
```

Lean checks the final and intermediate descriptions against the shared
integer interpreter and proves its size rule under the input conditions.
Branch rows expose the conditions used to narrow their descriptions.
Unknown widths appear in `unresolved_sizes`. This is a non-recursive
mathematical integer model: the source decoder remains trusted, agreement
with a Lynx-translated target is not checked, and no runtime cost bound
is asserted. See `infer_integer_sizes/2` for the supported fragment.

## Derive costs from integer sizes

The following example includes a growing accumulator. Supply a finite range
for the initial counter and retain the compiled BEAM binary.

```elixir
{:module, IntegerCostDemo, cost_beam, _} =
  defmodule IntegerCostDemo do
    def sum(counter, accumulator) do
      if counter > 0,
        do: sum(counter - 1, accumulator + counter),
        else: accumulator
    end

    def triple(x), do: x * 3
    def caller(x), do: __MODULE__.triple(x) + 1
  end

alias ReductionBounds.IntegerSize

{:ok, sum_bound} =
  ReductionBounds.infer_integer_cost_bound({IntegerCostDemo, :sum, 2},
    beam_binaries: [cost_beam],
    input_sizes: [
      %IntegerSize{lower: 0, upper: 4, magnitude_bits: 3},
      IntegerSize.from_integer(0)
    ]
  )

sum_bound.result_size
#=> %IntegerSize{lower: 0, upper: 10, magnitude_bits: 7}
sum_bound.upper_bound
sum_bound.conditions.inferred_recursive_inputs

input = %IntegerSize{lower: -4, upper: 4, magnitude_bits: 3}
{:ok, triple_bound} =
  ReductionBounds.infer_integer_cost_bound({IntegerCostDemo, :triple, 1},
    beam_binaries: [cost_beam], input_sizes: [input])
{:ok, caller_bound} =
  ReductionBounds.infer_integer_cost_bound({IntegerCostDemo, :caller, 1},
    beam_binaries: [cost_beam], input_sizes: [input],
    call_contracts: %{{IntegerCostDemo, :triple, 1} => triple_bound})
caller_bound.conditions.calls
```

A leaf `{:cost, :addition, [a, b]}` means the symbolic addition cost
evaluated at magnitude bounds `a` and `b`. Sequential work uses `:add`,
and alternatives use `:max`. All cost functions must be nondecreasing
in their size parameters. Shift and mask leaves use the constant amount
or width as their second parameter. The result is not an ERTS reduction
count or an execution-time guarantee.

For a modeled external unary call, supply a contract with `input_size`,
`result_size`, and `upper_bound` instead of a checked result. These are
assumptions about the normally returning external model. For example:

```elixir
absolute_contract = %{
  input_size: input,
  result_size: %IntegerSize{lower: 0, upper: 4, magnitude_bits: 3},
  upper_bound: {:cost, :absolute, [3, 0]}
}
# Pass call_contracts: %{{:erlang, :abs, 1} => absolute_contract} to a caller.
```

For a recursive CRC-style function that masks the accumulator to eight
bits after each update, an optional invariant can be supplied as:

```elixir
recursive_invariant = [
  %IntegerSize{lower: 0, upper: 4},
  %IntegerSize{lower: 0, upper: 255, magnitude_bits: 8}
]
```

Pass it as `recursive_invariant: recursive_invariant` alongside the
initial input descriptions. The result distinguishes this supplied
condition from the automatically inferred argument descriptions.
Lean checks the invariant at every recursive call in the derivation.
Exhausting `max_unfolding` or `max_nodes` reports an error; neither
setting is an assumption that recursion terminates.

## Identify an operation cost contract

Use a `CostContract` to label the cost unit and operation-cost declaration.
This is separate from the contracts for calls to other functions.

```elixir
alias ReductionBounds.CostContract

{:ok, contract} = CostContract.new(id: :analysis, cost_unit: :symbolic_work)

{:ok, declared_bound} =
  ReductionBounds.infer_integer_cost_bound({IntegerCostDemo, :triple, 1},
    beam_binaries: [cost_beam],
    input_sizes: [input],
    cost_contract: contract
  )

declared_bound.cost_contract.id
#=> :analysis
declared_bound.applicability.parameters.mask
#=> {:input_magnitude_bits, :mask_width}
declared_bound.applicability.contract_instantiation
#=> :not_checked
```

A declaration may also contain natural-valued expressions; see
`ReductionBounds.CostContract`. The current Lean proof remains parametric
in nondecreasing cost functions. Recording or evaluating a declaration does
not check a concrete instantiation or a comparison between contracts.
Labels such as `:jit` do not establish correspondence with that runtime.
To check declarative function and budget ordering separately, use
`compare_cost_contracts/3`. That check does not transfer the target's bound.

## Compare operation cost contracts

Compare two declarative contracts in the smaller-to-larger direction.
This example needs no target module or BEAM binary.

```elixir
alias ReductionBounds.CostContract

functions_a = Map.new(CostContract.operations(), &{&1, {:add, 1, :left}})
functions_b = Map.new(CostContract.operations(), &{&1, {:add, 2, :left}})
{:ok, a} = CostContract.new(id: :a, functions: functions_a)
{:ok, b} = CostContract.new(id: :b, functions: functions_b)

{:ok, comparison} =
  ReductionBounds.compare_cost_contracts(a, b,
    budget: {:add, {:cost, :addition, [8, 9]}, {:cost, :binding, [0, 0]}}
  )

comparison.budget_values
#=> %{smaller: 10, larger: 12}
comparison.checked_theorems
comparison.bound_transfer
#=> :not_checked
```

Lean checks the ordering for all natural parameters and every composed
budget. The optional budget's evaluations are also checked. An unresolved
structural comparison is not a proof that the inequality is false.
A conditional theorem also compares finite executions of the same integer
program, preserving the returned value. Its assumptions require every static
call to have an admitted argument, matching returned values, and ordered actual
costs. These assumptions are recorded in `execution_conditions`; they are not
discharged for a target by this API. Use `transfer_integer_bound/3` to
recheck a target bound and apply the comparison under explicit call conditions.

## Transfer an integer bound

Pass an integer bound carrying a concrete source `cost_contract`, together
with a smaller destination contract:

```elixir
{:ok, transferred} = ReductionBounds.transfer_integer_bound(bound, destination)
transferred.upper_bound_contract
transferred.upper_bound_value
transferred.checked_theorems
```

The target and any reused callee bounds are analyzed again and both proofs
are rechecked. The numeric bound is evaluated under the source contract.
`input_sizes` may narrow the original input domains. For contracted calls,
supply `callee_correspondence: :shared` for a common mathematical external
model, or a Lean proof of the two external models' value and actual-cost
correspondence. Source call contracts remain explicit assumptions.

## Verification scope and diagnostics

Successful contract comparison and integer bound transfer include
`verification_scope`. Its `checked` field lists completed checks;
`assumptions` records conditions required by the theorem; `trusted` names
processing outside the Lean proof; and `unverified` identifies remaining
claims, including concrete runtime correspondence and wall-clock timing.
`checked_theorems` remains the list of audited Lean declarations.

Comparison reports the contracts as `smaller` and `larger`. Transfer
reports `from` (the source contract B) and `to` (the destination A).
Input conditions and callee assumptions must hold before applying the
mathematical guarantee. A checked result does not certify an ERTS build.

These two APIs report unknown options as `{:error, {:unknown_options, keys}}`.
Invalid values retain `{:error, {:invalid_option, name, value}}`. Comparison
accepts `budget`, `timeout`, `max_concurrency`, and `lean_options`; it does
not accept `beam_binaries` because it selects no target. Transfer accepts
`beam_binaries` to repeat source extraction. Other diagnostics, such as
`unresolved_cost_order`, `insufficient_transfer_inputs`, and
`unresolved_callee_correspondence`, remain errors rather than successful
transfers. Lean proof or axiom-audit failures report `verification_failed`.

Reproduce the public workflow examples and rejection checks with:

```sh
REDUCTION_BOUNDS_REPORT=1 mix test --only cost_contract_workflow --trace
```

# `execution_inference_option`

```elixir
@type execution_inference_option() ::
  struct_inference_option()
  | {:call_contracts, %{optional(target()) =&gt; non_neg_integer()}}
```

# `list_inference_option`

```elixir
@type list_inference_option() ::
  verification_option() | {:input_contract, Lynx.Lean.t()}
```

# `periodic_inference_option`

```elixir
@type periodic_inference_option() ::
  struct_inference_option() | {:periodic_contract, Lynx.Lean.t()}
```

# `setup_error`

```elixir
@type setup_error() ::
  :no_mix_project
  | :invalid_lean_path
  | {:expected_target_list, term()}
  | {:invalid_targets, [term()]}
  | :invalid_options
  | :missing_postlude
  | {:invalid_option, atom(), term()}
  | {:read_lean, Path.t(), File.posix()}
  | {:priv_dir, :bad_name}
  | {:beam_registration,
     %CompileError{
       __exception__: term(),
       description: term(),
       file: term(),
       line: term()
     }}
```

# `struct_inference_option`

```elixir
@type struct_inference_option() ::
  {:max_concurrency, pos_integer()}
  | {:timeout, non_neg_integer()}
  | {:beam_binaries, [binary()]}
  | {:lean_options, [{atom(), integer() | boolean() | String.t()}]}
```

# `target`

```elixir
@type target() :: {module(), atom(), arity()}
```

# `target_result`

```elixir
@type target_result() ::
  {:ok, [Lynx.Laws.report()]}
  | {:error,
     {:verification_failed, [Lynx.Laws.report()]}
     | {:lynx, Exception.t()}
     | :timeout
     | {:task_exit, term()}}
```

# `verification_option`

```elixir
@type verification_option() ::
  {:max_concurrency, pos_integer()}
  | {:timeout, non_neg_integer()}
  | {:beam_binaries, [binary()]}
  | {:termination_suffixes, %{optional(target()) =&gt; Lynx.Lean.t()}}
  | {:lean_options, [{atom(), integer() | boolean() | String.t()}]}
```

# `verification_result`

```elixir
@type verification_result() ::
  {:ok, [{target(), target_result()}]} | {:error, setup_error()}
```

# `compare_cost_contracts`

```elixir
@spec compare_cost_contracts(
  ReductionBounds.CostContract.t(),
  ReductionBounds.CostContract.t(),
  keyword()
) :: {:ok, map()} | {:error, term()}
```

Checks pointwise ordering of two declarative operation cost contracts in Lean.

Arguments are ordered from smaller to larger. The contracts must share the
cost unit, semantics, event profile, and parameter meanings. Both must have
declarative functions. The comparison is universal over natural parameters,
using a conservative structural certificate; an unresolved comparison is
not a proof that the inequality is false.

Lean checks expression monotonicity, each operation's ordering, and ordering
of every existing symbolic budget under the two function assignments.
Optionally pass `budget: bound.upper_bound` to check its two evaluations.
Auxiliary callee cost names must be declared on both sides.

Lean also checks a generic execution comparison for the existing integer
semantics: for the same program and input, every admitted finite execution
under the smaller assignment has a same-value execution under the larger
assignment with at least its cost. This covers bindings, arithmetic, branches,
static calls, and finite recursion. It needs neither a VM model nor a new
definition of program execution.

Static calls require explicit value agreement and actual-cost ordering on
their admitted input domains. Ordered callee budgets alone are insufficient.
The existing size derivation can establish that each call's argument meets
its input contract; the theorem retains the assumptions needed by that derivation.

The result records `proof_scope: :cost_functions_budgets_and_executions`,
`execution_cost_order: :checked`, and the required `execution_conditions`.
This checks the conditional generic theorem without selecting a target or
proving that its callees meet those conditions. Accordingly,
`execution_applicability` is `:not_checked` and `callee_correspondence` is
`:assumed`. No caller-supplied label counts as evidence for these assumptions.
This comparison alone leaves bound transfer and concrete integer contract
instantiation unchecked; `transfer_integer_bound/3` performs those checks
separately. Correspondence with an ERTS implementation remains unchecked.

```elixir
alias ReductionBounds.CostContract

smaller_functions = Map.new(CostContract.operations(), &{&1, {:add, 1, :left}})
larger_functions = Map.new(CostContract.operations(), &{&1, {:add, 2, :left}})
{:ok, smaller} = CostContract.new(id: :smaller, functions: smaller_functions)
{:ok, larger} = CostContract.new(id: :larger, functions: larger_functions)

{:ok, comparison} =
  ReductionBounds.compare_cost_contracts(smaller, larger,
    budget: {:add, {:cost, :addition, [8, 9]}, {:cost, :binding, [0, 0]}}
  )

comparison.budget_values
#=> %{smaller: 10, larger: 12}
```

`verification_scope` separates the conditional theorem from its assumptions,
trusted metadata and serialization, and unverified target or runtime applicability.
`timeout`, `max_concurrency`, and `lean_options` work as for verification.
Unknown options, including `beam_binaries`, report `unknown_options`.

# `infer_circular_buffer_bound`

```elixir
@spec infer_circular_buffer_bound([verification_option()]) ::
  {:ok, map()} | {:error, term()}
```

Infers a symbolic upper bound for one CircularBuffer.insert/2 call.

The buffer must have proper integer lists and a valid count and capacity,
and the item must be an integer. The size parameter n is the capacity.
Lean checks the cost and invariant of the state model for the recognized
Core Erlang clauses. This analysis uses the list contract for Enum.reverse/1.

# `infer_circular_buffer_sequence_bound`

```elixir
@spec infer_circular_buffer_sequence_bound([verification_option()]) ::
  {:ok, map()} | {:error, term()}
```

Infers a symbolic upper bound for consecutive insertions from CircularBuffer.new/1.

The size parameters m and n denote capacity and number of insertions.
The cost covers the calls to insert/2 with integer items. Lean verifies
the exact rotation count for the state model and the resulting cost bound.

# `infer_integer_cost_bound`

```elixir
@spec infer_integer_cost_bound(target(), keyword()) :: {:ok, map()} | {:error, term()}
```

Derives a size-dependent symbolic cost bound for the supported Core Erlang integer fragment.

Supply independent `input_sizes` in argument order. Operation costs remain
arbitrary natural-valued functions nondecreasing in their size parameters.
The result records `result_size`, `upper_bound`, `intermediates`, and `conditions`.
This is a symbolic model, with no ERTS reduction or timing claim.

The optional `cost_contract` is a `ReductionBounds.CostContract` declaration
identifying operation costs, the cost unit, and the integer semantics profile.
Omission records the existing symbolic contract. The result's `cost_contract`
and `applicability` fields identify the declaration, parameter meanings,
input domains, recursive conditions, and callee assumptions actually used.
This operation contract is separate from a callee's `call_contracts` entry.

Declarative cost functions can be recorded and evaluated in Elixir. The Lean
bound remains quantified over arbitrary nondecreasing cost functions;
`applicability.contract_instantiation: :not_checked` means a concrete
declaration has not been instantiated and checked in Lean. A contract named
`:jit` does not establish JIT applicability. Runtime correspondence remains
`:not_checked`, and comparisons and bound transfers between contracts are
not implemented by this option.

```elixir
alias ReductionBounds.CostContract

{:ok, contract} = CostContract.new(id: :analysis, cost_unit: :symbolic_work)

{:ok, bound} =
  ReductionBounds.infer_integer_cost_bound({MyModule, :sum, 2},
    input_sizes: [
      %ReductionBounds.IntegerSize{lower: 0, upper: 4, magnitude_bits: 3},
      ReductionBounds.IntegerSize.from_integer(0)
    ],
    cost_contract: contract
  )

bound.applicability.parameters.shift_left
# {:input_magnitude_bits, :shift_amount}
```

Static unary calls use `call_contracts`, keyed by module, function, and arity.
Each contract has `input_size`, `result_size`, and `upper_bound`. A checked
result from this API for a unary function can instead
be supplied directly; its derivation is checked again. External call models
must return normally and obey the supplied contracts.

Two-argument self recursion is expanded into a finite derivation under the
supplied input ranges. `max_unfolding` (default 32) limits nested self calls;
`max_nodes` (default 10_000) limits analysis nodes. Exhaustion is an error,
never a termination assumption. An optional two-element `recursive_invariant`
is checked at entry and at every reachable self call.

Lean checks the derivation, intermediate summaries, source lowering certificate,
and any supplied invariant. It proves the cost bound for every modeled run
and constructs a finite normal run under the call contracts. Source agreement
is with the reified supported Core fragment and its stated mathematical
primitive semantics. BEAM extraction and reification remain trusted.

    ReductionBounds.infer_integer_cost_bound({MyModule, :sum, 2},
      input_sizes: [
        %ReductionBounds.IntegerSize{lower: 0, upper: 4, magnitude_bits: 3},
        ReductionBounds.IntegerSize.from_integer(0)
      ]
    )

Unknown operand widths, missing or inapplicable contracts, unsupported syntax,
failed invariants, and unresolved recursion produce contextual errors.
`beam_binaries`, `lean_options`, `timeout`, and `max_concurrency` work as for
the other verification APIs.

# `infer_integer_sizes`

```elixir
@spec infer_integer_sizes(target(), keyword()) :: {:ok, map()} | {:error, term()}
```

Infers integer ranges and magnitude widths from a supported Core Erlang function.

The non-recursive fragment supports integer constants and variables, bindings,
addition, subtraction, multiplication, XOR, constant nonnegative shifts,
low-bit masks, and Boolean cases comparing an integer expression with a constant.
All arguments and results are mathematical integers. Input descriptions are
independent; relational input assumptions are not supported.

Pass `input_sizes: [size, ...]` in argument order. Omission uses unknown sizes.
`beam_binaries` supports modules compiled in memory. Other verification options
include `timeout`, `max_concurrency`, and `lean_options`.

```elixir
alias ReductionBounds.IntegerSize

{:module, SizeExample, beam, _} =
  defmodule SizeExample do
    def update(x) do
      y = x * 3
      if y < 0, do: y - 1, else: y + 1
    end
  end

ReductionBounds.infer_integer_sizes({SizeExample, :update, 1},
  beam_binaries: [beam],
  input_sizes: [%IntegerSize{lower: -2, upper: 4, magnitude_bits: 3}]
)
```

The result contains `result_size`, `intermediates`, branch conditions, and
`unresolved_sizes`. Lean checks the candidate against the abstract interpreter
and proves that every evaluation of the reified fragment satisfies the result
description under the input conditions. Source extraction and reification are
trusted. `proof_scope: :reified_integer_fragment` and `value_agreement:
:not_checked` distinguish this from agreement with a Lynx-translated target.
This entry point derives sizes, not reduction or execution-time bounds.
Calls outside the fragment and unsupported conditions return contextual errors.

# `infer_list_bound`

```elixir
@spec infer_list_bound(target(), [list_inference_option()]) ::
  {:ok, map()} | {:error, term()}
```

Infers a symbolic cost bound for a supported traversal of an integer list.

The traversal may take a key before the list and may test for equality.
The analysis handles clause dispatch, values, integer addition, sequential
bindings, and at most one self-call on the tail per branch.

Results include the branch recurrence and two Lean-checked upper bounds
for its cost model. The refined_upper_bound field accounts for the
different maximum step counts of empty-list and early-stop paths. Each
charge names a clause and an operation. A dispatch
charge includes the tests that led to the clause. ERTS reduction charges
are outside this model. Accepts the same options as file-based verification.

The value_model field identifies a recognized membership, length, reversal,
or composed model.
For those models, Lean also proves that the costed model returns the same
value as the translated target on integer lists and that its cost obeys
the upper bound. A nil value_model means only the symbolic cost rules
were checked.

The target {:lists, :member, 2} uses Lynx's built-in value model for an
integer key and a proper integer list. Its charges describe the empty,
hit, and continue cases of the symbolic traversal. In the continue
case, recursive_call denotes a step to the next list cell in the model.

The target {:lists, :reverse, 2} uses Lynx's built-in reversal model.
Both arguments must be proper integer lists. The size measure applies
to the first argument, and the result is its reversal followed by
the accumulator. The symbolic bound does not depend on accumulator length.

The composed model analyzes Core Erlang functions without recursion that
use static calls, single-value bindings, and Boolean branches. The
input is a proper integer list, optionally preceded by an integer key.
When the derivation branches, path_bounds reports separate bounds for
the first Boolean decision's true and false paths. The upper_bound
covers both paths. A call_bound names the callee and carries its bound
specialized to the sizes of the actual arguments.

For a recognized value model, pass input_contract as a Lean block to
prove the bounds under an input precondition with a tail invariant and a
natural-valued size measure. See the module examples.

# `infer_list_call_bound`

```elixir
@spec infer_list_call_bound(target(), target(), [list_inference_option()]) ::
  {:ok, map()} | {:error, term()}
```

Infers a symbolic bound for a local wrapper that forwards every argument
unchanged to a list traversal. The callee must have a recognized value
model. Lean checks the callee theorem and reuses it to prove the wrapper
bound, which adds the symbolic caller_cost to the callee's bound.

An optional input_contract Lean block can supply a precondition, a
tail invariant, and a size measure. See the module examples.

# `infer_periodic_bound`

```elixir
@spec infer_periodic_bound(target(), [periodic_inference_option()]) ::
  {:ok, map()} | {:error, term()}
```

Infers the event count and symbolic cost for repeated calls to a
counter with fixed progress and a reset at its threshold.

The target takes a pending count and a period and returns the
updated pending count and a Boolean event flag. The initial count
must be below the period. Both are natural numbers and the period
must be at least the positive increment recovered from the code.

Pass a Lean block as :periodic_contract to add an invariant and a
measure for the initial state. Define periodicInvariant and
periodicMeasure and prove periodicInvariantStep and
periodicMeasureBounds using periodicDomain and periodicNext.
The result reports the derived and supplied conditions and includes
a separate upper bound expressed in the supplied measure.

# `infer_port_bound`

```elixir
@spec infer_port_bound(target(), ReductionBounds.ProtocolCost.t(), [
  struct_inference_option() | {:calls, map()}
]) :: {:ok, map()} | {:error, term()}
```

Checks a finite Port-client Core fragment against the supplied protocol profile.

The target's first argument is the Port and its second is preparation input.
Supply `calls: %{prepare: {Helpers, :prepare, 1}, reply: {Helpers, :await_reply, 1},
decode: {Helpers, :decode, 1}}`. These helper contracts remain assumptions.
The fragment requires `port_command/3` with `[:nosuspend]`, Boolean outcome
cases, and reply patterns `{:data, payload}`, `:closed`, and `:timeout`.

The caller bound adds `source_node_bound * source_node` to the protocol
budget. The symbolic source-node charge covers supported local evaluation,
bindings, argument retrieval, and finite case dispatch. Event charges cover
contracted helper and primitive work. This is not concrete ERTS accounting.

Only after Lean verifies the connection, semantics, and returned formula is
`source_correspondence` set to `:checked`. The claim covers finite normally
returning executions under the declared call, size and event-count contracts.
Helper implementation correctness, exceptions, progress, and runtime charging
remain unverified. Native receive and recursive clients are unsupported.
Standard timeout, concurrency, Lean, and BEAM-binary options apply.

```elixir
{:ok, result} = ReductionBounds.infer_port_bound({Client, :request, 2}, profile,
  calls: %{prepare: {Helpers, :prepare, 1}, reply: {Helpers, :await_reply, 1},
    decode: {Helpers, :decode, 1}})
result.verification_scope.kind
#=> :core_connected_port_client
result.source_correspondence
#=> :checked
result.completion.theorem_status
#=> :not_established
```

# `infer_rpc_bound`

```elixir
@spec infer_rpc_bound(target(), map(), keyword()) :: {:ok, map()} | {:error, term()}
```

Verifies a restricted helper-contracted RPC source fragment and conditional bound.

The target has server, reference, integer payload and timeout arguments. Supply
`calls: %{reply: {Helpers, :await_reply, 3}, decode: {Helpers, :decode, 1}}`.
`beam_binaries`, timeout, concurrency and Lean options use the existing workflow.
The verified claim covers finite normal reified-Core executions under input,
primitive/helper and mailbox/data association contracts. Native receive is
unsupported. Helper bodies, source termination and runtime charging stay unverified.
See the RPC source guide for the exact accepted Core forms and applicability.

# `infer_search_bound`

```elixir
@spec infer_search_bound(target(), [verification_option()]) ::
  {:ok, map()} | {:error, term()}
```

Derives a symbolic cost bound for a two-argument integer list search.

The supported function returns false for an empty list, returns true
when the head strictly equals the key, and otherwise calls itself with
the same key and the tail. The input size n bounds the list length.
The three branch costs are nonnegative symbolic parameters.

The result includes the recurrence, the bound expression, and Lean
verification reports for the cost model. ERTS reduction charges are
outside this model. Accepts the same options as file-based verification.

# `infer_struct_bound`

```elixir
@spec infer_struct_bound(target(), [struct_inference_option()]) ::
  {:ok, map()} | {:error, term()}
```

Derives a symbolic cost upper bound for a supported Core Erlang function
with map or struct operations. Pass the target as a module, function,
and arity tuple. Use beam_binaries for in-memory modules. The timeout,
max_concurrency, and lean_options settings match those for verification.

    ReductionBounds.infer_struct_bound({MyCounter, :debit, 2})

Lean checks the bound for every path in the original fragment's cost
model. Each opaque path has a separate symbolic cost in the result.
The result includes the normalized fragment and certificates. For the
supported unguarded map cases and variable aliases, value_agreement is
:checked after Lean verifies decoding and normalization from the
extracted Core fragment. Lean derives the required field facts in
scope and uses the same interpretation of opaque branches on both
sides. A value_agreement of :unsupported means Lean checked individual
rewrite certificates under their listed field assumptions.

# `infer_verified_bound`

```elixir
@spec infer_verified_bound(target(), [execution_inference_option()]) ::
  {:ok, map()} | {:error, term()}
```

Checks a symbolic cost bound for every modeled execution of a supported
unary Core Erlang function.

The input must be a nonnegative integer. The fragment supports a zero
clause and a default clause, sequential bindings, zero tests, integer
addition, static unary calls, and self calls on the input minus one.
The result gives the zero and successor equations for the upper bound.
In a successor equation, :previous denotes the bound at the preceding input.

Supply :call_contracts as a map from static targets to nonnegative
symbolic cost limits, excluding the caller's own call charge. Each
contract assumes a natural result and normal termination for reachable
calls with a natural input. Lean checks the cost bound under the limits
and proves termination of the modeled target under the totality
assumptions. A missing contract is reported with the callee's target.
Unsupported expressions or invalid recursive calls prevent verification.

# `transfer_integer_bound`

```elixir
@spec transfer_integer_bound(map(), ReductionBounds.CostContract.t(), keyword()) ::
  {:ok, map()} | {:error, term()}
```

Rechecks an integer bound and transfers it to a smaller operation-cost contract.

Pass a result of `infer_integer_cost_bound/2` whose `cost_contract` contains
concrete declarative functions. The source contract belongs to `bound`;
`destination` is the smaller contract. The target and any reused callees are
extracted again from BEAM. Stored derivations, formulas, and translation
certificates must match fresh analysis, and both proofs are checked again in Lean.
Result flags such as `checked_theorems` are not accepted as proof evidence.

The reused formula is evaluated under the source contract. The result's
`upper_bound_contract` identifies that contract and `upper_bound_value` is
the checked numeric bound. Do not evaluate the formula under the destination
contract and assume that smaller value is also a bound.

`input_sizes` may retain or narrow the original input domains. Lean checks
coverage of the original precondition. Recursive invariants and call contracts
remain those of the original derivation. `max_unfolding`, `max_nodes`,
`beam_binaries`, and the standard verification options are supported.

When call contracts are present, supply `callee_correspondence: :shared` to
state that both mathematical executions use the same external value-and-cost
model. Without this choice or a Lean proof, transfer reports an unresolved
callee correspondence. This is not a claim about two concrete runtime modes.

Alternatively, pass a `Lynx.Lean` block as `callee_correspondence`. It must
define `transferExternalA`, `transferExternalB`, and prove
`transferCalleeAgreement`, with type:

```lean
ReductionBounds.BoundTransfer.ExternalAgreement
  (fun key argument => (SourceBound.costContracts key).input.Holds argument)
  transferExternalA transferExternalB
```

A denotes the destination and B the source. The proof must establish equal
values and ordered actual callee costs on the source input contracts.
Source callee output and cost contracts remain explicit assumptions of the
generated theorem. Their smaller-contract budgets are not assumed to hold.

```elixir
alias ReductionBounds.{CostContract, IntegerSize}

functions = Map.new(CostContract.operations(), &{&1, {:add, 2, :left}})
{:ok, source} = CostContract.new(id: :source, functions: functions)
functions = Map.new(CostContract.operations(), &{&1, {:add, 1, :left}})
{:ok, destination} = CostContract.new(id: :destination, functions: functions)

{:ok, bound} =
  ReductionBounds.infer_integer_cost_bound({MyModule, :square, 1},
    input_sizes: [%IntegerSize{lower: -4, upper: 4, magnitude_bits: 3}],
    cost_contract: source
  )

{:ok, transferred} = ReductionBounds.transfer_integer_bound(bound, destination)
transferred.upper_bound_value
transferred.upper_bound_contract
#=> :source
```

The generated theorem bounds every destination execution covered by the
preconditions. Its termination theorem retains the finite derivation and
normally returning source callee assumptions. Concrete runtime correspondence
remains `:not_checked`. The returned `verification_scope` records these
assumptions, the checked steps, and the trusted extraction and serialization.
Unknown option names report `unknown_options`.

# `verify`

```elixir
@spec verify(Path.t(), [target()]) :: verification_result()
@spec verify([target()], [verification_option() | {:postlude, Lynx.Lean.t()}]) ::
  verification_result()
```

Checks a Lean file or an inline Lean postlude against each target.

Pass a filename relative to the Mix project's lean/ directory and
a list of targets, or pass the targets first with a required
postlude option containing a Lynx.Lean value, such as one produced
by the ~LEAN sigil. Target tuples have the form {module, function, arity}.

# `verify`

```elixir
@spec verify(Path.t(), [target()], [verification_option()]) :: verification_result()
```

Checks a Lean file against each target independently.

Give a filename relative to the current Mix project's lean/ directory
and a list of {module, function, arity} tuples. The file is loaded as a
Lynx postlude in each verification, so it must elaborate with the
definitions available for that target. Target modules are resolved
from registered BEAM binaries or through the BEAM code path.

Options

  * beam_binaries (default []) registers BEAM binaries for target
    modules and dependencies in this invocation. The binaries must
    contain debug information from which Lynx can obtain Core Erlang.
    If several binaries define the same module, the last one is used.
    Registration failures are returned before verification starts.

  * termination_suffixes (default %{}) attaches Lean termination
    declarations to translated functions, keyed by {module, function,
    arity}. A suffix can specify how Lean handles a function that may
    diverge on some inputs. Each supplied function must be translated
    in every verification job.
  * max_concurrency (default 2) limits the number of concurrent tasks
    in this invocation. Set it to 1 for sequential verification.
  * timeout (default 60,000 milliseconds) limits each task from
    translation through Lean verification. A timed-out task is killed
    and reported as {:error, :timeout}. This limits the caller's wait.
    Termination of the external Lake/Lean process tree is outside
    the timeout's guarantee.
  * lean_options (default []) passes options such as maxHeartbeats
    and maxRecDepth to the Lean runner.

Each target is verified once. Results follow the order in which
targets first appear. Lynx checks that the module can be translated and
that the function exists in it. Translation, runner, and task failures
are reported for the affected target. Malformed values produce
{:invalid_targets, values} before verification starts. Each offending
value is listed once in input order.
This function checks the supplied declarations. It does not derive bounds
or apply Lynx's law-specific axiom audit to postlude declarations.

# `verify_port_model`

```elixir
@spec verify_port_model(ReductionBounds.ProtocolCost.t(), list(), keyword()) ::
  {:ok, map()} | {:error, term()}
```

Verifies supplied Port-model traces, universal cost safety and optional completion conditions.

`events` accepts registered event names or protocol event structs. Supply
`observations: %{index => observation}` from `ProtocolConditions.observe/2`.
Optional `completion: [limits: ..., progress: ..., goal: :termination]`
declares the earlier model's completion conditions. Missing progress keeps
a checked cost bound and reports diagnostics; it does not establish completion.

The result reports `verification_scope`, `checked_theorems`, assumptions,
trusted processing and unresolved obligations. Conditional completion is
scoped to the supplied model. This API does not analyze a target function.
See the Port interactions guide under the examples documentation.

```elixir
names = [:prepare, :submit_accepted]
{:ok, observation} = ReductionBounds.ProtocolConditions.observe(%{request: []})
observations = %{0 => observation, 1 => observation}
{:ok, result} = ReductionBounds.verify_port_model(profile, names, observations: observations)
result.verification_scope.kind
#=> :supplied_port_model
result.completion.theorem_status
#=> :not_established
```

Construct `profile` with `PortProtocol.conditions/1` and `ProtocolCost.new/2`;
the examples guide supplies the complete size, count and cost declarations.

# `verify_rpc_conditions`

```elixir
@spec verify_rpc_conditions(map(), list(), list(), keyword()) ::
  {:ok, map()} | {:error, term()}
```

Verifies RPC mailbox conditions and conditional completion without a cost profile.

Uses a `RpcConditions` profile and the same completion and verification options
as `verify_rpc_model/4`. A checked model trace is not source correspondence.

# `verify_rpc_model`

```elixir
@spec verify_rpc_model(map(), list(), list(), keyword()) ::
  {:ok, map()} | {:error, term()}
```

Verifies a supplied RPC mailbox timeline and assesses conditional completion.

Accepts a `RpcCost` profile. `completion: [progress: [...], goal: :termination | :success]`
declares universal model progress, never inferred from costs or a successful example.
Missing progress leaves verified conditional cost safety usable. Standard `timeout`,
`max_concurrency` and `lean_options` apply through the existing verification boundary.
Assumed contracts remain assumed; this does not verify runtime progress or source code.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
