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

Declares operation costs and their applicability for the integer cost fragment.

An operation cost contract is distinct from a callee's `call_contracts` entry.
It identifies a cost unit and the modeled semantics and charge events. Cost
functions take two natural parameters whose meanings depend on the operation.
For arithmetic they are magnitude widths; for shifts and masks the second
parameter is the constant shift amount or mask width. Local fixed charges
use two unused parameters, both zero.

Functions are either symbolic, or declarative expressions built from natural
constants, `:left`, `:right`, and `{:add, a, b}`, `{:mul, a, b}`, or
`{:max, a, b}`. Every declared operation needs a function when expressions
are supplied. Auxiliary cost names in callee budgets need their own parameter
declarations. Elixir functions and arbitrary Lean source are not expressions.

This module validates declarations and evaluates expressions. Use
`ReductionBounds.compare_cost_contracts/3` for separate Lean checks of
declarative function monotonicity, pointwise ordering, and budget ordering.
The comparison also checks a conditional execution-cost theorem under
explicit callee assumptions. It does not select a target or transfer a bound.
Use `ReductionBounds.transfer_integer_bound/3` to recheck a target and its
original bound, instantiate concrete contracts, and apply the ordering.
Neither API establishes applicability to a concrete VM implementation.
Integer inference continues to check a bound quantified
over arbitrary nondecreasing cost functions. A supplied declaration is reported
separately with `contract_instantiation: :not_checked`; evaluating an expression
in Elixir is not a Lean certificate for that instance.

## Examples

    iex> alias ReductionBounds.CostContract
    iex> {:ok, contract} = CostContract.new(id: :analysis)
    iex> contract.functions
    :symbolic
    iex> CostContract.evaluate(contract, :addition, [8, 8])
    {:error, {:unresolved_cost_function, :addition}}

    iex> alias ReductionBounds.CostContract
    iex> expr = {:add, 1, {:mul, :left, :right}}
    iex> functions = Map.new(CostContract.operations(), &{&1, expr})
    iex> {:ok, contract} = CostContract.new(id: :example, functions: functions)
    iex> CostContract.evaluate(contract, :multiplication, [8, 9])
    {:ok, 73}

Names such as `:jit` are labels only. A successful declaration does not mean
that a VM implementation satisfies it, or that its unit is ERTS reductions.

# `expression`

```elixir
@type expression() ::
  non_neg_integer()
  | :left
  | :right
  | {:add | :mul | :max, expression(), expression()}
```

An expression denoting a natural-valued operation cost.

# `label`

```elixir
@type label() :: atom() | String.t()
```

# `parameter_names`

```elixir
@type parameter_names() :: {atom(), atom()}
```

# `t`

```elixir
@type t() :: %ReductionBounds.CostContract{
  auxiliary_parameters: %{optional(atom()) =&gt; parameter_names()},
  cost_unit: label(),
  events: :integer_cost_v1,
  functions: :symbolic | %{optional(atom()) =&gt; expression()},
  id: label(),
  parameter_domain: :all_natural_pairs,
  semantics: :core_integer_v1
}
```

# `evaluate`

```elixir
@spec evaluate(t(), atom(), [non_neg_integer()]) ::
  {:ok, non_neg_integer()} | {:error, term()}
```

Evaluates a declared function; this does not produce proof evidence.

# `execution_operations`

```elixir
@spec execution_operations() :: [atom()]
```

Operation names in the numeric order used by the integer execution semantics.

# `new`

```elixir
@spec new(keyword()) :: {:ok, t()} | {:error, term()}
```

Builds a validated declaration for the supported integer cost profile.

# `operations`

```elixir
@spec operations() :: [atom()]
```

Returns the names of the built-in cost events.

# `parameters`

```elixir
@spec parameters(t()) :: %{required(atom()) =&gt; parameter_names()}
```

Returns the meanings of both parameters for every declared cost event.

# `validate`

```elixir
@spec validate(term()) :: :ok | {:error, term()}
```

Validates a declaration, including structs constructed without new/1.

---

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