Recursive
opulse.generation.recursive ¶
Recursive operators built from one callee of the layer below.
Two templates (docs/reference/semantics.md section 4.1). v is the loop
variable, g the callee, f the new operator.
Template A, bounded_mod (m the modulus)::
f(..v..) = { base, if v % m == 0
g(args with R = f(..(v % m) - 1..)), if v % m > 0
g(args with R = f(..(v % m) + 1..)), if v % m < 0 }
% keeps the dividend's sign, so the recursion walks v % m toward 0
from either side and unfolds at most m - 1 times.
Template B, direct (T the bound)::
f(..v..) = { base, if v == 0
g(args with R = f(..v - 1..)), if v > 0 and v <= T
g(args with R = f(..v + 1..)), if v < 0 and v >= - T }
The direction follows the sign of v itself; beyond |v| > T no branch
matches and the result is undefined. It unfolds at most T times.
Style peano is template B written with the atoms v ⁻ / v ⁺
instead of v - 1 / v + 1 (same values), for operator sets that start
from the atomic domain. R marks the
recursive result inside the callee's arguments; the other argument slots hold
the operator's own parameters (taken from the current frame, exactly as the
definition reads).
Template D, primitive (the primitive-recursion schema, n the last
parameter, x the others, T the bound)::
h(x, n) = { base(x), if n == 0
g(x, n - 1, h(x, n - 1)), if n > 0 and n <= T
g(x, n + 1, h(x, n + 1)), if n < 0 and n >= - T }
The branch shape and bound are those of template B, so termination, the NaN
beyond |n| > T and the z3 case split are the same. What changes is the
body: base is an expression over the other parameters built from lower
layers, and the step g(...) is an expression of lower-layer operators that
reads the stepped counter and the previous result (and possibly parameters).
With only the atoms ⁻ ⁺ available (no binary - +) the counter steps
with them, as in template C.
The compute/count functions are produced by the ordinary code generator from
this definition, so they agree with it by construction. (The legacy generator
wrote a separate loop that disagreed with the definition for 54 of 127
parseable recursive operators in operator_1k.jsonl.)
RecursiveSpec
dataclass
¶
complexity ¶
complexity() -> tuple[int, int]
Worst case: m-1 (or T) unfoldings; two recursive slots double the calls per level.
PrimitiveSpec
dataclass
¶
h(x, n) = { base, if n == 0; step[C = n - 1], ...; step[C = n + 1], ... }.
step is an expression in which Var(COUNTER) stands for the stepped counter and
Var(REC) for the previous result h(x, C); the other variables are parameters.
counter_steps ¶
counter_steps(registry) -> tuple[str, str]
How template D writes n - 1 / n + 1: "arith" (binary -, +) or "peano"
(postfix ⁻, ⁺), whichever the starting set has (arith preferred).
random_primitive_spec ¶
random_primitive_spec(
symbol: str,
n_ary: int,
unary_position: str | None,
callee: Operator,
registry,
layer: int,
settings: RecursionSettings,
symbols: Symbols,
expressions,
rng: Random,
) -> PrimitiveSpec
A template-D operator whose step calls callee (layer layer - 1).
The step reads the counter and the previous result: callee gets both in two of its
argument slots (the other slots get parameters or the counter); a unary callee gets them
combined by a binary operator of a lower layer, g(R ⊙ C) or g(C ⊙ R). base is a
random expression over the other parameters built from operators below layer, or a
parameter, or a constant.
primitive_draft ¶
primitive_draft(
spec: PrimitiveSpec, symbols: Symbols, resolve=None
) -> Draft
The template-D definition; resolve (an application -> operator lookup for everything
but the new operator) gives the structural metrics, else they are derived later.