Skip to content

Context variables

Every rule is evaluated against a context: a set of variables describing the candidate step. This page is the authoritative list. For a gentler introduction see the guide.

Base variables

Supplied directly for every evaluation.

VariableTypeDefinition
prev_sumnumberThe running total from all previous steps, before the candidate is applied.
current_valuenumberThe signed value of the candidate step. Positive is addition, negative is subtraction. Never 0.
step_indexnumberZero-based position of the current step. 0 is the first step.
last_step_indexnumberIndex of the final step of the exercise — that is, total steps minus one.

Derived variables

Computed from the base variables. You never set these; they are always consistent with the values above.

VariableTypeDefinition
current_sumnumberprev_sum + current_value — the total that results if the candidate is accepted.
isLastStepbooleantrue when step_index = last_step_index.
placenumber | nullThe single decimal place current_value affects (1, 10, 100, …), or null if it affects more than one.
formulanumber | nullabs(current_value) / place, a digit 1–9. null when place is null.
digitBeforenumber | nullfloor(abs(prev_sum) / place) % 10 — the digit of the running total at the affected place. null when place is null.

How place is determined

A value is atomic when exactly one of its decimal digits is non-zero. place is the positional value of that digit:

current_valueplaceformula
717
-717
40104
3001003
47nullnull
-89nullnull
105nullnull

Sign is ignored throughout: place, formula, and digitBefore are all computed from absolute values. Use current_value > 0 / current_value < 0 to test direction.

The null contract

WARNING

When place, formula, or digitBefore is null, comparisons against them still evaluate. They do not skip the rule, and they do not uniformly fail.

Null behaves as 0 in ordering comparisons and is unequal to every number in equality comparisons. The practical result, for a composite candidate such as +47:

FormExampleResult
Equalityformula = 8fails
Membershipplace IN (1, 10)fails
Lower boundformula >= 1fails
Strictly greaterformula > 0fails
Inequalityformula != 8passes
Negative membershipplace NOT IN (1, 10)passes
Upper boundformula <= 9passes
Strictly lessformula < 1passes

So inclusive tests reject composite steps and exclusive tests admit them. This is the single most common source of surprising rule behavior — see the null trap for what it means in practice.

The upside of the inclusive half: a standard pattern rule of the form current_value > 0 AND formula = N AND digitBefore IN (…) automatically fails on composite steps with no special handling. Pattern rules are about atomic techniques, and they self-exclude correctly.

Scope and which variables apply

A rule declares a scope, which decides when it runs:

ScopeRuns
stepAgainst every candidate step.
last-stepAgainst the final step only.
sequenceOnce, after a complete candidate sequence has been assembled.

All the variables above are available in step and last-step scope. A sequence-scope rule is evaluated once with the final step's values in scope, plus whole-sequence values describing the assembled exercise.

INFO

Whole-sequence variables are supplied by the generator rather than by the base context, and the editor's linter does not currently recognize them as known identifiers — expect an "unknown variable" diagnostic even where they work. Prefer step-scope rules unless you specifically need to reason about a finished sequence.

Evaluation outcomes

Every rule evaluates to one of three outcomes:

OutcomeMeaning
PassedThe condition held.
FailedThe condition did not hold. For a constraint, this rejects the candidate.
SkippedThe rule's IF guard was false, so the rule did not apply. A skipped rule never blocks a candidate, and never counts as a pattern match.

The distinction between failed and skipped matters for pattern rules: only a passed rule counts as a match toward a recipe's count quotas.

See also

Reference and cookbook for mental-math rule and recipe authoring.