Skip to content

Operators and definitions

To read, evaluate or write an operator, you need its exact value on every input, including division by zero and overflow.

An operator has a symbol, an arity (unary prefix, unary postfix, or binary) and, unless it is a base operation, a definition. Opulse compiles the definition to Python (or Cython) and checks the compiled code against a reference interpreter that reads the definition itself.

Three kinds of definitions

Kind Shape
simple one expression over the parameters
branch conditions tried in order, the first that holds gives the result
recursive one of three templates that call the operator itself on a smaller value

Two of each kind, taken from a generated operator set:

a ♤₁₁ = { (a $) $ }
a ▼₁ b = { - (b / a) }
a ♥₃₅ = { (- a) ○₂₄ (4 % a), if a < 17; a, if a >= 92; (a ♡₂₁) ☆₁₅ (a ♡₂₆), else }
◆₄₅ a = { (a ♧₃₁) ♠₃₆ a, if a < 14; (- a) ♡₃₇ (a ♧₃₁), if a >= 57; (a @) + a, else }
a ♣₂₅ = { - 2, if a % 9 == 0; & (((a % 9) - 1) ♣₂₅), if a % 9 > 0; & (((a % 9) + 1) ♣₂₅), if a % 9 < 0 }
a ♡₃₇ b = { 73, if b % 7 == 0; (a ♡₃₇ ((b % 7) - 1)) ♡₂₆, if b % 7 > 0; (a ♡₃₇ ((b % 7) + 1)) ♡₂₆, if b % 7 < 0 }

Values: integers and NaN

Every value is an integer or NaN (undefined). NaN comes from exactly four situations:

  • division by zero, 7 / 0;
  • remainder by zero, 7 % 0;
  • a base-operation result larger than the bound B = 2³¹ − 1 in absolute value;
  • a definition in which no branch matches (a branch definition without else).

NaN propagates through base operations. If one side of an evaluated comparison in a condition is NaN, the whole call is NaN. Division and remainder truncate toward zero: -7 / 2 = -3, -7 % 2 = -1.

Watching a recursion unfold

One of the recursive operators above, applied to small inputs. The first condition that holds selects a branch; the recursive call receives a smaller control value, until the base case ends the recursion. Yellow marks the part rewritten in each step, green the result.

2 ♣₂₅
= & (((2 % 9) - 1) ♣₂₅)
= & ((2 - 1) ♣₂₅)
= & (1 ♣₂₅)
= & (& (((1 % 9) - 1) ♣₂₅))
= & (& ((1 - 1) ♣₂₅))
= & (& (0 ♣₂₅))
= & (& (- 2))
= & (& (-2))
= & (-2)
= (-2)

Press play, or use the arrows and slider to follow recursion.

Every recursion template unfolds a bounded number of times (at most m − 1 for v % m), so every operator terminates on every input.

Counting work

Each evaluation also returns base_ops, the number of base operations actually executed. Short-circuited conditions are not counted; a branch that returns a constant or a variable counts 0. The reference interpreter and the compiled code agree on this count too:

0 ♣₂₅ = -2    base_ops = 2
1 ♣₂₅ = -2    base_ops = 7
5 ♣₂₅ = -2    base_ops = 27
(-5) ♣₂₅ = -2    base_ops = 32
9 ♣₂₅ = -2    base_ops = 2
12 ♣₂₅ = -2    base_ops = 17
100 ♣₂₅ = -2    base_ops = 7
(-2147483647) ♣₂₅ = -2    base_ops = 8
2147483647 ♣₂₅ = -2    base_ops = 7

The operator record

Operators are stored one per JSON line:

{
  "func_id": "vSGEK9n4ny",
  "symbol": "♣₂₅",
  "n_ary": 1,
  "unary_position": "postfix",
  "recursive_layer": 1,
  "definition": "a ♣₂₅ = { - 2, if a % 9 == 0; & (((a % 9) - 1) ♣₂₅), if a % 9 > 0; & (((a % 9) + 1) ♣₂₅), if a % 9 < 0 }",
  "definition_type": "recursive_definition",
  "max_path_depth": 8,
  "max_edge_count": 8,
  "dependencies": ["0pxTvgPEIa", "1xW1ZG8bwv", "5ZZT1reNC3", "7UumhDVYmZ", "ooX1Ek07wh"]
}

op_compute_func and op_count_func hold the compiled source; dependencies lists the operators the definition calls. opulse analyze and opulse describe add solver and language fields (SMT-LIB export, descriptions).

Using operators from Python

from opulse.core import OperatorRegistry
from opulse.verify import Oracle

registry = OperatorRegistry.from_jsonl("outputs/operators.jsonl")
oracle = Oracle(registry)                       # the reference interpreter
op = next(op for op in registry if op.definition_type == "recursive_definition")
result = oracle.evaluate(op, (7, 7))
print(result.value, result.base_ops, result.calls)