Skip to content

Python API

opulse.World gives Python access to an operator set, its expressions, traces, benchmark tasks and fill in the blank problems. Evaluation uses the reference interpreter. Complete traces are checked against it. Generation and task building use the same components and seeds as the command line. Undefined values (NaN) are returned as None.

Every example on this page was run to produce the output shown below it (tools/api_examples.py writes them).

Grow a world

World.grow generates operators layer by layer. layers gives the number of new operators per layer; the same arguments always give the same world, and the same operators as opulse operators --layers 20 10 5 --seed 1.

import opulse

world = opulse.World.grow(layers=[20, 10, 5], seed=1)
print(world)
print(world.summary()["layers"])
<World 35 operators (L0:20 L1:10 L2:5) over 10 atoms, seed=1>
{0: {'simple': 11, 'branch': 9}, 1: {'recursive': 2, 'simple': 4, 'branch': 4}, 2: {'recursive': 1, 'simple': 2, 'branch': 2}}

atoms chooses the starting set: "integer" (the default), "atomic", a path to an operator JSONL file, or a list of atom names, including atoms you registered (Atoms). config takes a YAML path, a dict or a Settings object. Keyword arguments change single settings: a section as a dict (recursion={"style": "direct"}) or one key as section__key (semantics__bound=None).

world = opulse.World.grow(layers=[20, 10], seed=3, atoms="atomic")
world = opulse.World.grow(layers=[30], seed=0, operators={"symbol_sets": ["greek"]})

Operators

world.operators lists the defined operators in the order they were made, world.atoms the atomic operations and world.layer(i) one layer. world["⊕"] finds an operator by its symbol; a symbol with several roles takes the role as a second key (world["-", "prefix"]).

op = world["□₃₆"]
print(op.definition)
print(op.kind, op.layer, op.position)
print([callee.symbol for callee in op.callees])
print(op.evaluate(3, 4), op(10, 2))
print(op.result(3, 4))
a □₃₆ b = { b, if a % 3 == 0; (((a % 3) - 1) □₃₆ b) - a, if a % 3 > 0; (((a % 3) + 1) □₃₆ b) - a, if a % 3 < 0 }
recursive 1 binary
['+', '-', '%']
4 -8
CallResult(value=4, base_ops=1, calls=1)

evaluate (or calling the operator) returns the value; result also returns the number of base operations and calls, the cost measures of Operators and definitions. table evaluates a small grid of inputs:

print(op.table([0, 1, 2, 3, 10]))
  a\b   0   1   2   3  10
    0   0   1   2   3  10
    1  -1   0   1   2   9
    2  -3  -2  -1   0   7
    3   0   1   2   3  10
   10 -10  -9  -8  -7   0

Traces

op.trace(*args) and expression.trace() reduce step by step, unfolding definitions leftmost innermost, as in Step by step solutions. Each step is checked against the reference interpreter before the trace is returned. depth, known and known_layers set the granularity; trace.narrate("en") gives the prose solution of datasets.

trace = op.trace(2, 2)
print(len(trace), "steps, value", trace.value)
print("\n= ".join(trace.lines()[:6]))
9 steps, value -1
2 □₃₆ 2
= (2 % 3 - 1) □₃₆ 2 - 2
= (2 - 1) □₃₆ 2 - 2
= 1 □₃₆ 2 - 2
= ((1 % 3 - 1) □₃₆ 2 - 1) - 2
= ((1 - 1) □₃₆ 2 - 1) - 2

Expressions

world.expression(text) parses with the operators' priorities, like the expressions of generated datasets. Variables are allowed and bound with evaluate(a=..., b=...).

expr = world.expression("2 * 3 ■₄₁ 4 + 1")
print(expr.text, "=", expr.value)
print(expr.full_text)
print(expr.trace().value == expr.value)
free = world.expression("a ■₄₁ b")
print(free.variables, free.evaluate(a=6, b=4))
2 * 3 ■₄₁ 4 + 1 = 40
2 * ((3 ■₄₁ 4) + 1)
True
('a', 'b') 19

world.sample_expressions samples exactly like opulse expressions, with the same options (depth, frequency, max_layer, keep_undefined, cot). Each result keeps its full dataset record in expr.record.

for expr in world.sample_expressions(4, depth=2, frequency="zipf:1.5", seed=0):
    print(expr.text, "=", expr.value, " expansion", expr.record["expansion"])
(50 ■₁₇ 30) ★₄₇ = 19  expansion 40
(12 □₁₁) ■₄₁ (18 ●₁₀) = 19  expansion 58
(- 20) ▲₂₄ (61 ★₄₇) = 19  expansion 48
1 ■₁₇ (♤₅₅ 85) = 188  expansion 7

Benchmark tasks and problems

world.tasks(family, n, **options) builds tasks like opulse bench build and accepts the builder's options (context, distractors, shuffle, max_depth, naming, answer_format, operator_frequency, and the others). By default it rederives every answer from its prompt text, as opulse bench check does, and fails if one does not follow. tasks.to_jsonl(path) writes the file the benchmark commands read.

tasks = world.tasks("compute", n=20, seed=0, max_depth=2)
task = min(tasks, key=lambda t: len(t.definitions))
print(task.expression, "=", task.answer_text)
print("\n".join(task.definitions))
print(tasks.check())
(# 32) ♥₁₂ 18 = -169
a ♥₁₂ b = { (a $) - (# b) }
[]

world.holes makes fill in the blank problems with checked solution sets (Solving for unknowns). world.equivalent checks whether two expressions agree on every input. A proved verdict covers all inputs; a bounded verdict covers only tested inputs. world.equivalences() lists candidate operator pairs and their verdicts (Laws and equivalence).

for hole in world.holes(2, depth=2, unique_only=True, seed=0, timeout_ms=500, domain=50):
    print(hole.text, " x =", hole.solutions)
(50 □₃₆ x) □₁₁ = -63  x = [30]
(# 32) ♥₁₂ x = -169  x = [18]
print(world.equivalent("a + b", "b + a").status)
verdict = world.equivalent("(a + b) - b", "a")
print(verdict.status, verdict.counterexample)
proved
refuted {'a': 1, 'b': 2147483647}

Save and load

world.save(path) writes a directory with operators.jsonl (the file opulse operators writes), config.yaml and world.json. Every command line tool reads the first two. World.load reads such a directory or a plain operator JSONL file.

path = world.save("my_world")
print(sorted(p.name for p in path.iterdir()))
again = opulse.World.load(path)
print(again.fingerprint == world.fingerprint)
['config.yaml', 'operators.jsonl', 'world.json']
True

Dependencies

world.dependencies(op) lists the operators a definition calls, transitive=True all of them; op.callers lists the operators that call it. world.graph() draws the dependency graph, and world.graph(focus=op) only what op needs.

print([dep.symbol for dep in world.dependencies(op, transitive=True)])
print([caller.symbol for caller in op.callers])
graph = world.graph(focus=op)
print(graph)
['+', '-', '%']
['♣₄₉']
<Graph 4 operators, 4 edges>

In a notebook

Worlds, operators, expressions, traces, tasks and graphs display themselves in Jupyter. The views use inline styles only and follow the colours of these pages: cyan for composition, amber for branching, magenta for recursion. They are shown below exactly as a notebook renders them.

A world shows its operators per layer and kind; the layers open to list the definitions:

World 35 operators over 10 atoms, seed 1, atoms integer
simplebranchrecursivetotal
atoms10
layer 011920
layer 144210
layer 22215
all layers1715335
atoms: + - * / % - @ # $ &
layer 0: 20 operators
a ▼₁ = { ((- a) + (a @)) + ((& a) $) }
a ♡₃ b = { (# b) * (a $) }
a ◇₅ = { (a $) $ }
a ○₇ = { a ♡₃ a }
a ●₁₀ = { (26 + a) * 5 }
a □₁₁ = { (a + a) + a }
a ♥₁₂ b = { (a $) - (# b) }
a ♡₁₅ b = { (b ○₇) - (a + 16) }
a ♡₁₆ b = { # b, if a >= 74 and b < 17; a % 58, if a < 20 and b >= 17 and b < 35; # a, if a >= 27 and a < 31 and b >= 35 and b < 51; a * b, if a < 20 and b >= 35 and b < 51; b ♥₁₂ b, if a >= 20 and a < 27 and b >= 35 and b < 51; a, if a >= 20 and a < 27 and b >= 17 and b < 35; 0, if a >= 27 and a < 31 and b < 17; - a, if a >= 74 and b >= 17 and b < 35; 37 ♡₃ a, if a >= 31 and a < 60 and b >= 35 and b < 51; & b, else }
a ■₁₇ b = { (- a) ♡₃ (- b), if a >= 40 and b >= 92; a - b, if a < 28 and b < 58; (- 1) ♡₃ (12 $), if a >= 28 and a < 31 and b < 58; (b ♥₁₂ b) % (- a), if a < 28 and b >= 92; (b ●₁₀) - (b $), if a >= 40 and b < 58; (a ◇₅) ♡₁₆ a, if a < 28 and b >= 84 and b < 92; (b ♥₁₂ a) ♡₃ (& a), if a >= 31 and a < 40 and b >= 92; b, else }
a ●₁₈ b = { (b □₁₁) + a, if a >= 97 and b >= 38 and b < 42; (a □₁₁) ♡₁₅ (# b), if a < 97 and b >= 68 and b < 86; a ♡₁₅ (- a), if a >= 97 and b >= 59 and b < 61; (57 ▼₁) + b, if a < 97 and b >= 61 and b < 68; (b ♥₁₂ 60) @, if a < 97 and b < 15; a - (- a), if a >= 97 and b >= 31 and b < 38; a ♡₁₅ (b ■₁₇ b), if a < 97 and b >= 59 and b < 61; # (a % b), else }
a ○₁₉ = { 17 ♡₃ a, if a >= 65; a ●₁₀, else }
a □₂₀ b = { (b ♡₁₆ a) ○₇, if a >= 55 and b < 7; (b ♥₁₂ b) ●₁₈ (b + b), else }
a ♥₂₂ b = { # b, if a < 32 and b >= 5 and b < 22; & 22, if a >= 32 and a < 94 and b >= 83; b / a, if a >= 94 and b >= 43 and b < 58; a ♥₁₂ b, if a >= 94 and b >= 61 and b < 83; & 6, if a < 32 and b >= 83; & 5, if a >= 32 and a < 94 and b >= 5 and b < 22; a ♡₁₅ b, if a >= 32 and a < 94 and b >= 43 and b < 58; a @, if a >= 94 and b >= 83; 0 ■₁₇ b, if a >= 94 and b >= 5 and b < 22; a + 8, else }
a ♣₂₃ b = { (# b) / (- b), if a >= 85 and b >= 97; - (a - b), if a >= 53 and a < 85 and b >= 97; (a ●₁₈ b) ♡₃ (b / a), if a >= 46 and a < 48 and b >= 22 and b < 97; (a ●₁₀) % (- b), if a < 36 and b < 22; (a % 9) ♡₃ (- a), else }
a ▲₂₄ b = { b ○₇, if a >= 79 and b >= 96; 25, if a >= 79 and b >= 43 and b < 72; b * 59, if a >= 25 and a < 79 and b >= 72 and b < 96; b ◇₅, if a < 25 and b >= 41 and b < 43; 0 - 0, if a < 25 and b >= 72 and b < 96; 19, else }
a ♣₂₆ b = { (& a) ♡₁₅ (b ○₇) }
□₂₇ a = { (a ▼₁) * (a ♡₁₅ a) }
♥₃₁ a = { (□₂₇ a) □₂₀ (a ○₇), if a < 9; # (a * a), if a >= 42 and a < 92; a ■₁₇ (a □₂₀ a), else }
a ☆₃₄ = { ((a $) ♡₃ a) ■₁₇ (& (a ○₁₉)) }
layer 1: 10 operators
a □₃₆ b = { b, if a % 3 == 0; (((a % 3) - 1) □₃₆ b) - a, if a % 3 > 0; (((a % 3) + 1) □₃₆ b) - a, if a % 3 < 0 }
○₃₇ a = { a, if a % 8 == 0; (○₃₇ ((a % 8) - 1)) ●₁₀, if a % 8 > 0; (○₃₇ ((a % 8) + 1)) ●₁₀, if a % 8 < 0 }
a ■₄₁ b = { (b ●₁₀) ▲₂₄ ((○₃₇ a) ●₁₈ (○₃₇ a)) }
a ○₄₂ b = { ○₃₇ (a ▲₂₄ b) }
a ○₄₃ = { (- (♥₃₁ a)) ■₄₁ (a ▼₁) }
a ★₄₇ = { (# a) ■₄₁ ((a ○₁₉) ●₁₈ 24) }
a ♣₄₉ b = { (○₃₇ b) □₃₆ 13, if a < 22 and b >= 95; b - (○₃₇ a), if a >= 22 and a < 25 and b < 84; (23 ♡₁₆ b) ●₁₀, if a >= 86 and b >= 95; b ■₁₇ (○₃₇ b), if a < 22 and b >= 84 and b < 95; ♥₃₁ (b □₃₆ b), if a >= 72 and a < 86 and b < 84; (13 ♡₁₅ a) ★₄₇, if a >= 28 and a < 38 and b < 84; a ♣₂₃ (♥₃₁ a), if a >= 49 and a < 72 and b >= 95; ♥₃₁ (a $), if a >= 49 and a < 72 and b >= 84 and b < 95; (& 10) ○₄₂ (2 ★₄₇), if a >= 48 and a < 49 and b >= 84 and b < 95; (- 15) - (a / b), else }
a ♣₅₁ b = { (♥₃₁ b) ■₄₁ (32 ♡₁₆ a), if a < 8 and b >= 34 and b < 56; ○₃₇ (○₃₇ a), if a >= 96 and b >= 56; - (a ●₁₈ a), if a >= 96 and b < 34; ○₃₇ (a ♣₂₆ a), if a >= 68 and a < 80 and b >= 34 and b < 56; b, if a >= 11 and a < 68 and b >= 34 and b < 56; □₂₇ (a % 7), else }
♤₅₅ a = { ○₃₇ (♥₃₁ (a ▼₁)), if a >= 11 and a < 20; ♥₃₁ ((♥₃₁ a) ○₄₂ (a ▼₁)), if a >= 30 and a < 41; 41, if a >= 20 and a < 24; (a ○₇) ♣₄₉ a, if a >= 80 and a < 95; (○₃₇ (○₃₇ a)) + ((# a) ♥₂₂ (- a)), if a >= 98; a ♣₄₉ (# (□₂₇ a)), if a < 5; 1 ●₁₀, if a >= 95 and a < 98; (○₃₇ (○₃₇ 9)) + ((♥₃₁ 5) ★₄₇), if a >= 5 and a < 11; (& (a ■₁₇ a)) ○₄₃, else }
a ◆₅₇ b = { b, if a < 4 and b >= 62; b - a, if a >= 4 and a < 80 and b < 19; a ▲₂₄ a, if a >= 80 and a < 92 and b >= 46 and b < 61; a ♡₃ a, if a >= 4 and a < 80 and b >= 62; 35, if a >= 4 and a < 80 and b >= 61 and b < 62; b ♣₅₁ b, if a < 4 and b >= 19 and b < 46; ♥₃₁ a, if a < 4 and b >= 61 and b < 62; a ■₄₁ 61, if a >= 92 and b >= 62; b * b, if a >= 92 and b < 19; # 29, else }
layer 2: 5 operators
a ◆₅₈ b = { a, if b % 9 == 0; (a ◆₅₈ ((b % 9) - 1)) ○₄₂ a, if b % 9 > 0; (a ◆₅₈ ((b % 9) + 1)) ○₄₂ a, if b % 9 < 0 }
a ★₅₉ = { a ◆₅₈ a }
♡₆₀ a = { a ♡₁₆ ((48 / a) ★₅₉) }
♦₆₁ a = { (# a) ◆₅₈ (# a), if a >= 19 and a < 50; - (a + a), if a >= 56 and a < 60; & (14 * a), if a >= 7 and a < 19; (11 ■₁₇ 22) ▼₁, if a < 7; (a □₁₁) ●₁₈ a, if a >= 88; (○₃₇ a) ●₁₈ (2 ▲₂₄ 33), else }
a ★₆₂ b = { (& (♦₆₁ b)) ♣₅₁ ((a □₂₀ a) ♡₃ (□₂₇ a)), if a >= 70 and b < 2; ♤₅₅ ((b ○₄₃) ♡₁₆ (a ◆₅₈ a)), if a >= 24 and a < 70 and b < 2; & ((a □₂₀ b) ♡₃ (a @)), if a >= 24 and a < 70 and b >= 2 and b < 31; ((b ♣₂₆ b) ♣₄₉ a) @, else }

An operator shows its definition, kind, layer, the operators it calls and a table of values:

□₃₆recursivelayer 1binarypriority 7
a □₃₆ b = { b, if a % 3 == 0; (((a % 3) - 1) □₃₆ b) - a, if a % 3 > 0; (((a % 3) + 1) □₃₆ b) - a, if a % 3 < 0 }
calls +-% and itself
a \ b0123510
00123510
1-101249
2-3-2-1027
30123510
5-6-5-4-3-14
10-10-9-8-7-50

A trace marks the subexpression each step rewrites, in the colour of its operator, and says why:

Trace 5 steps
11 □₃₆ 1unfold 1 □₃₆ 1 branch 2, since 1 % 3 > 0
2(1 % 3 - 1) □₃₆ 1 - 1compute 1 % 3 = 1
3(1 - 1) □₃₆ 1 - 1compute 1 - 1 = 0
40 □₃₆ 1 - 1unfold 0 □₃₆ 1 branch 1, since 0 % 3 == 0
51 - 1compute 1 - 1 = 0
= 0

A task shows its definitions, expression and answer; the full prompt opens below them:

computecontext fullfreecompute-000001
definitions
a ♥₁₂ b = { (a $) - (# b) }
expression (# 32) ♥₁₂ 18 answer -169
prompt
You are given operators defined from a few base operations. Use the rules and the definitions exactly as written; do not assume any familiar meaning of the symbols.

Rules:
Values are integers or NaN (undefined).
Base operations:
  - a - b is integer subtraction.
  - # a is sign(a) * |a| * (|a| + 1) / 2, computed exactly; only its result is checked against the bound (# 4 = 10, # -4 = -10).
  - a $ is the number of 1 bits in the binary form of |a| (7 $ = 3).
Any base operation whose result is larger than 2147483647 in absolute value gives NaN.
A base operation with a NaN argument gives NaN.
A definition `lhs = { e1, if c1; e2, if c2; ...; e, else }` tries its branches in order and returns the first branch whose condition holds; `else` always holds.
If no branch holds, the result is NaN. If a compared value is NaN, the result is NaN.
Unary operators bind tighter than binary ones; a postfix operator binds tighter than a prefix one.
A binary operation used inside another binary operation is always written in parentheses.

Definitions:
a ♥₁₂ b = { (a $) - (# b) }

What is the value of `(# 32) ♥₁₂ 18`?
Reason step by step if you wish, then finish with a line of the form "Answer: <integer or NaN>".

A graph places every operator one column right of its deepest callee. Arrows point from a caller to what it calls, and a loop marks recursion. Hover a node to read its definition.

Operator dependency graph: 4 operatorssimplebranchrecursiveatoma □₃₆ b = { b, if a % 3 == 0; (((a % 3) - 1) □₃₆ b) - a, if a % 3 > 0; (((a % 3) + 1) □₃₆ b) - a, if a % 3 < 0 } recursive, layer 1□₃₆a + b (atom)+a - b (atom)-a % b (atom)%

Syntax colouring

Opulse ships a Pygments lexer, registered under the name opulse. Any Pygments based tool colours Opulse code once the package is installed, including code blocks marked opulse in Markdown. These pages use it:

a ⊕ b = { a + (b @), if a >= 10 and not b == 0; NaN, else }

world.lexer() returns a lexer configured for one world: its parameter names, its spelling of NaN and its numeral base markers.

from pygments import highlight
from pygments.formatters import HtmlFormatter

html = highlight(op.definition, world.lexer(), HtmlFormatter())
for token, text in world.lexer().get_tokens("a ■₄₁ (2 + NaN)"):
    if text.strip():
        print(f"{text:6} {token}")
a      Token.Name.Variable
■₄₁    Token.Name.Function
(      Token.Punctuation
2      Token.Literal.Number.Integer
+      Token.Operator
NaN    Token.Keyword.Constant
)      Token.Punctuation

The lexer never fails: any run of symbol characters is one operator, and any character it does not know is plain text. Its options are parameters, nan, words (operator names that are words, such as benchmark aliases), markers and equals.

Reference

opulse.api.World

An operator set together with the settings it was made with.

Build one with :meth:grow (generate new operators), :meth:load (read a saved world or an operator JSONL file) or the constructor (wrap an existing :class:~opulse.core.registry.OperatorRegistry).

oracle cached property

oracle

The reference interpreter over this world (:class:opulse.verify.Oracle).

runtime cached property

runtime

Compiled operator code (used by the reducer and the dataset builder; checked against the oracle).

fingerprint property

fingerprint: str

A short hash of what every symbol means (the operator_set of dataset provenance).

token_style property

token_style

The colouring notation of this world (:class:opulse.display.tokens.TokenStyle).

operators property

operators: list[Op]

The defined (generated) operators, in the order they were made.

atoms property

atoms: list[Op]

The atomic operations of the starting set (numeral base markers excluded).

markers property

markers: list[Op]

Numeral base markers: prefix symbols that say which base a numeral is written in.

grow classmethod

grow(
    layers: Sequence[int],
    seed: int = 0,
    atoms: str | Path | Iterable[str] = "integer",
    config: str | Path | Settings | dict | None = None,
    *,
    proportions: tuple[float, float, float] = (
        0.4,
        0.4,
        0.2,
    ),
    check_variety: bool = False,
    max_priority_levels: int | None = None,
    **overrides: Any,
) -> World

Generate operators layer by layer.

Parameters:

Name Type Description Default
layers Sequence[int]

how many new operators each layer gets, e.g. [20, 10, 5].

required
seed int

random seed; the same arguments always give the same world.

0
atoms str | Path | Iterable[str]

the starting set. "integer" (the shipped arithmetic atoms), "atomic" (successor, predecessor and friends; grown with configs/atomic.yaml unless config is given), a path to an operator JSONL file, or a list of atom names (built in or registered with :func:opulse.core.primitives.register_primitive).

'integer'
config str | Path | Settings | dict | None

a YAML path, a settings dict or a :class:~opulse.config.Settings. Defaults to the configuration that belongs to atoms.

None
proportions tuple[float, float, float]

shares of simple, branch and recursive definitions per layer.

(0.4, 0.4, 0.2)
check_variety bool

also reject operators that are nearly constant.

False
max_priority_levels int | None

cap on the number of precedence levels.

None
**overrides Any

setting changes, see :func:make_settings.

{}

load classmethod

load(
    path: str | Path,
    config: str | Path | Settings | dict | None = None,
    **overrides: Any,
) -> World

Read a world written by :meth:save, or an operator JSONL file.

For a JSONL file the settings come from config (default: configs/default.yaml).

save

save(path: str | Path) -> Path

Write the world to directory path: operators.jsonl, config.yaml, world.json.

operators.jsonl is the same file opulse operators writes, so every command line tool can read it (--operators DIR/operators.jsonl --config DIR/config.yaml).

lexer

lexer(**options: Any)

A Pygments lexer configured with this world's notation and base markers.

layer

layer(index: int) -> list[Op]

The defined operators of one recursive layer.

operator

operator(
    symbol: str,
    position: str | None = None,
    arity: int | None = None,
) -> Op

The operator written symbol.

position ("binary", "prefix" or "postfix") or arity picks one when a symbol has several roles, like - (subtraction and negation). A func_id is accepted too.

dependencies

dependencies(
    op: Op | str, transitive: bool = False
) -> list[Op]

Operators that op's definition calls (itself excluded); all of them when transitive.

dependents

dependents(op: Op | str) -> list[Op]

Operators whose definitions call op directly.

graph

graph(
    focus: Op | str | None = None, max_nodes: int = 200
) -> Graph

The dependency graph (callers point to callees), shown as inline SVG in a notebook.

With focus only that operator and everything it depends on is drawn.

summary

summary() -> dict

Counts of defined operators per layer and kind, plus atoms and markers.

expression

expression(text: str) -> Expression

Parse text with this world's operators, priorities and notation.

Binary operators follow their priorities and associativity, as in generated datasets; parentheses group as usual. Variables are allowed (Expression.evaluate(a=1) then binds them).

sample_expressions

sample_expressions(
    n: int,
    depth: int | None = None,
    *,
    frequency: str = "uniform",
    frequency_seed: int = 0,
    seed: int = 0,
    max_layer: int | None = None,
    keep_undefined: bool = False,
    cot: str = "none",
    cot_depth: int | None = None,
    cot_known_layers: int = 0,
    cot_known: Sequence[str] = (),
) -> list[Expression]

Sample n evaluated expressions, exactly like opulse expressions.

Parameters:

Name Type Description Default
n int

how many expressions.

required
depth int | None

largest tree depth (default: expressions.max_depth of the settings).

None
frequency str

"uniform" or "zipf:ALPHA", how often each defined operator is drawn.

'uniform'
seed int

sampling seed (--seed).

0
cot str

"en" or "zh" adds a verified narrated solution to each record.

'none'

Each result keeps the full dataset record in :attr:Expression.record.

tasks

tasks(
    family: str = "compute",
    n: int = 100,
    *,
    seed: int = 0,
    check: bool = True,
    **options: Any,
) -> TaskSet

Benchmark tasks, exactly like opulse bench build.

Parameters:

Name Type Description Default
family str

"compute", "hole" or "contrast".

'compute'
n int

number of tasks.

100
seed int

sampling seed.

0
check bool

re-derive every answer from its prompt text (opulse bench check) and raise if one does not follow.

True
**options Any

:class:opulse.bench.tasks.BenchBuilder options, e.g. context, distractors, shuffle, max_depth, max_layer, max_definitions, naming, answer_format, allow_nan, max_abs_value, operator_frequency.

{}

holes

holes(
    n: int,
    depth: int | None = None,
    *,
    seed: int = 0,
    unique_only: bool = False,
    max_layer: int | None = None,
    timeout_ms: int = 3000,
    domain: int = 200,
    max_attempts: int = 200,
) -> list[Hole]

Fill in the blank problems with checked solution sets, like opulse holes.

equivalences

equivalences(
    timeout_ms: int = 3000,
    operators: Iterable[Op | str] | None = None,
) -> list[tuple]

Candidate pairs with matching sample values and no known counterexample.

Returns (f, g, verdict) triples. A "proved" verdict establishes equivalence for all inputs; "bounded" only establishes agreement on tested inputs. Refuted pairs are omitted by the underlying solver.

equivalent

equivalent(
    left: str | Expression,
    right: str | Expression,
    timeout_ms: int = 3000,
)

Whether two expressions (over variables) agree everywhere: an :class:Equivalence verdict.

opulse.api.Op

One operator of a :class:World: an atom, a numeral base marker, or a defined operator.

position property

position: str

"binary", "prefix" or "postfix".

kind property

kind: str

"simple", "branch", "recursive", "atom" or "marker".

layer property

layer: int

The recursive layer: how many recursion levels the operator stands on.

definition property

definition: str | None

The definition text (None for atoms).

signature property

signature: str

The operator applied to its parameters, e.g. a ⊕ b.

atom_name property

atom_name: str | None

For an atom: its name (add, succ, ...).

rule property

rule: str | None

For an atom: the sentence that defines it (as benchmark prompts state it).

callees property

callees: list[Op]

Operators this definition calls directly (itself excluded).

callers property

callers: list[Op]

Operators whose definitions call this one directly.

evaluate

evaluate(*args: Value) -> Value

The value of this operator at args, from the reference interpreter (None for NaN).

result

result(*args: Value) -> CallResult

Value and cost (base operations, calls) at args, from the reference interpreter.

trace

trace(
    *args: Value,
    depth: int | None = None,
    known: Iterable[str] = (),
    known_layers: int = 0,
) -> Trace

Step by step reduction of this operator applied to args (see :meth:Expression.trace).

table

table(values: Sequence[int] | None = None) -> ValueTable

Values on a small grid: values for each argument (default: a few small integers).

describe

describe(lang: str = 'en') -> str

A natural language description of the definition (opulse describe).

opulse.api.Expression

An expression tree over a world's operators.

text property

text: str

Brackets only where priorities need them (the expression field of datasets).

full_text property

full_text: str

Every application below the root in brackets.

value property

value: Value

The value from the reference interpreter (None for NaN). Needs no free variables.

operators property

operators: list[Op]

The operators applied, outermost first, each once.

evaluate

evaluate(**env: int) -> Value

The value with variables bound by keyword, e.g. evaluate(a=3, b=4).

trace

trace(
    depth: int | None = None,
    known: Iterable[str] = (),
    known_layers: int = 0,
    max_steps: int = 2000,
) -> Trace

Step by step reduction, leftmost innermost, unfolding definitions.

Parameters:

Name Type Description Default
depth int | None

unfold at most this many levels; deeper calls are computed in one step.

None
known Iterable[str]

operator symbols computed in one step instead of unfolded.

()
known_layers int

operators below this layer are computed in one step.

0
max_steps int

stop (truncated) after this many steps.

2000

Every step is checked against the reference interpreter.

opulse.api.Trace

A verified step by step reduction (wraps :class:opulse.trace.Trace).

steps property

steps: list

The :class:opulse.trace.TraceStep records: kind, operator, inputs, condition, ...

lines

lines() -> list[str]

The source expression, then the expression after every step.

narrate

narrate(lang: str = 'en') -> str

The trace as checkable prose (the cot field of datasets).