ReductionBounds.CostContract (ReductionBounds v0.1.0)

Copy Markdown View Source

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.

Summary

Types

An expression denoting a natural-valued operation cost.

t()

Functions

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

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

Builds a validated declaration for the supported integer cost profile.

Returns the names of the built-in cost events.

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

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

Types

expression()

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

An expression denoting a natural-valued operation cost.

label()

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

parameter_names()

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

t()

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

Functions

evaluate(contract, operation, args)

@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()

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

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

new(options \\ [])

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

Builds a validated declaration for the supported integer cost profile.

operations()

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

Returns the names of the built-in cost events.

parameters(contract)

@spec parameters(t()) :: %{required(atom()) => parameter_names()}

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

validate(contract)

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

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