# DS → native-Rete / IR lowering

How DS source text compiles
to the native Rete network: schemas to schema-metadata
atoms and alpha nodes, rules to terminal nodes, patterns and
`where` clauses to alpha/beta nodes and join tests, actions
to the terminal's actions and `Expr` calls ([RDSC.4a.8], [RDSC.4a.9]),
series and companion slots to compile-time registrations plus
aggregation calls. Covers the synthetic-name
convention, dialect-driven per-tier differences, compile-time
validation and error shape, and string-interpolation lowering.
The Rete network is the program.

## What this is not

- Not a language-semantics spec — see language docs.
- Not a compiler-internals guide (AST, pass
  ordering, IR choices are implementation).
- Not the Rete-engine internals spec — see
  [IRET].
- Not an artefact-format spec — `ruleset_hash`
  references the artefact; its internal structure
  is implementation.

---

(RLOW-1)=
## Lowering-contract commitments [RLOW.1]

(RLOW-1-1)=
- [RLOW.1.1] **One engine instance per tier instance.** All
  of a tier's rules and schemas live in one
  engine; cross-tier boundaries are bridged by the
  integrator ([RARC.3.3.3]), not in-engine module
  boundaries.

*see also:* [RARC.3.3.3](#RARC-3-3-3)

(RLOW-1-2)=
- [RLOW.1.2] **Synthetic names are `$`-prefixed** ([LLEX.10.6.1]). No author
  name can equal one ([LGRA.15.2]); the compiler uses them for generated
  bindings and auxiliary constructs, and for engine-internal facts
  ([RARC.12.1]).

*see also:* [LGRA.15.2](#LGRA-15-2), [LLEX.10.6.1](#LLEX-10-6-1), [RARC.12.1](#RARC-12-1)

(RLOW-1-3)=
- [RLOW.1.3] **The terminal ordinal is the rule's source position.**
  The compiler emits terminals in canonical source order
  ([RLOW.14.3]) and the loader assigns each its zero-based
  index; the agenda reads that index as the first ordering
  key ([LDET.5.4]).
  Nothing is carried on the `TerminalNode` atom for ordering — the
  order IS the emission order, so there is no value to keep in
  step with it.

*checks:* `k`  —  *see also:* [LDET.5.4](#LDET-5-4), [RLOW.14.3](#RLOW-14-3)

(RLOW-1-4)=
- [RLOW.1.4] **Compile-time validation is strict.** Every referenceable name must resolve; every form must be
  dialect-enabled; every typed slot access must match the declared slot
  type. Nothing defers to runtime discovery — the service set is fixed
  at compile time from the imported libraries ([RARC.9.3.4]), so a name
  that does not resolve during lowering does not resolve later either.

*see also:* [RARC.9.3.4](#RARC-9-3-4)

(RLOW-1-5)=
- [RLOW.1.5] **Source locations survive lowering.** Every beta and
  terminal node the compiler attributes to a pattern or rule carries a
  `LineSpan` atom naming the DS source line and column of that pattern or rule
  ([RDSC.14.3], [RDSC.14.3.6]), so a runtime diagnostic on such a node reports
  the author's source location. A node the compiler makes for itself carries
  none ([RDSC.14.3.6]).

*see also:* [RDSC.14.3](#RDSC-14-3), [RDSC.14.3.6](#RDSC-14-3-6)

(RLOW-1-6)=
- [RLOW.1.6] **A rule the compiler cannot lower is refused.** The compiler
  must not write an artefact without a rule its source declares. When it cannot
  lower a rule, it must refuse the compile with `DS_VALIDATE_RULE_NOT_LOWERED`:
  one diagnostic per such rule, at the rule's span, naming the rule and the
  construct the lowering could not handle. The code is the same for every
  construct; the reason travels in the message.

  An unfinished lowering is therefore a compile-time stop, never an artefact
  that silently does less than its source says.

---

*ratified*

(RLOW-2)=
## Compilation pipeline [RLOW.2]

(RLOW-2-2)=
[RLOW.2.2] **A compile is whole or nothing.** The compiler must pin every
diagnostic to a location in the DS source it compiles. A compile either
produces a complete, valid artefact, or produces no artefact and its errors;
the compiler must not write a partial artefact. A warning does not stop a
compile.

---

(RLOW-3)=
## Schemas [RLOW.3]

(RLOW-3-2)=
### Lowered form [RLOW.3.2]

(RLOW-3-2-1)=
[RLOW.3.2.1] A schema lowers to its schema group ([RDSC.8.1]): one
lifecycle atom (`SchemaTtl` or `SchemaLogical`), one `Slot` atom per declared
slot in declaration order, one `Hydration` atom per hydration-eligible slot
([RDSC.5.6]), then `SchemaSource` and `SchemaDef`. Each `Slot` atom carries the
slot's name and type. The runtime registers the schema by name into its schema
registry; the Rete alpha nodes that match on the schema reference it by that
name.

*see also:* [RDSC.5.6](#RDSC-5-6), [RDSC.8.1](#RDSC-8-1)

(RLOW-3-3)=
### Compiled artefact additions [RLOW.3.3]

(RLOW-3-3-1)=
[RLOW.3.3.1] For each schema the compiler produces:

(RLOW-3-3-2)=
- [RLOW.3.3.2] The schema's protobuf definition, as `.proto` text in its
  `SchemaDef` atom ([RDSC.5.8.1]). It types the `fact` bytes of a
  `FactAssert` body (`proto/fact_assert.proto`) wherever one is encoded — the
  recorded event trace, and any integrator transport that carries facts.

*defined in:* [proto/fact_assert.proto#FactAssert](#proto-fact_assert-proto-FactAssert)  —  *see also:* [RDSC.5.8.1](#RDSC-5-8-1)

(RLOW-3-3-3)=
- [RLOW.3.3.3] The schema's metadata, carried by its lifecycle atom and its
  `Slot` atoms rather than by a record of its own: the lifecycle and, for a
  `ttl` schema, the default TTL ([RDSC.5.3], [RDSC.5.4]); per slot, its
  identity role on a `logical` schema ([RLOW.3.6.1]), its tolerance
  ([RLOW.3.7.1]) and its `sampled` / `retained` markers ([RLOW.3.5])
  ([RDSC.5.5]).

*see also:* [RDSC.5.3](#RDSC-5-3), [RDSC.5.4](#RDSC-5-4), [RDSC.5.5](#RDSC-5-5), [RLOW.3.5](#RLOW-3-5), [RLOW.3.6.1](#RLOW-3-6-1), [RLOW.3.7.1](#RLOW-3-7-1)

(RLOW-3-4)=
### Lifecycle is semantic, not structural [RLOW.3.4]

(RLOW-3-4-1)=
[RLOW.3.4.1] The lifecycle does not change the schema's slot layout. It is
recorded in the lifecycle atom (`SchemaLogical` or `SchemaTtl`) and consumed by
the engine's fact-assert path. Each slot keeps its position and type whichever
lifecycle the schema has; a `logical` schema's `Slot` atoms additionally carry
each slot's identity role and tolerance ([RLOW.3.6.1], [RLOW.3.7.1]).

*see also:* [RLOW.3.6.1](#RLOW-3-6-1), [RLOW.3.7.1](#RLOW-3-7-1)

(RLOW-3-5)=
### Sampled and retained slot markers [RLOW.3.5]

(RLOW-3-5-1)=
[RLOW.3.5.1] `sampled` is a compile-time marker. The compiler records it in
the `sampled` field of the slot's `Slot` atom (`proto/slot.proto#Slot`); the
slot otherwise lowers as any other. What the marker asks of the engine is
[LSTO.3.3.2]'s.

*see also:* [LSTO.3.3.2](#LSTO-3-3-2)

(RLOW-3-5-2)=
[RLOW.3.5.2] `retained` is a compile-time marker too. The compiler records it
in the `retained` field of the slot's `Slot` atom (`proto/slot.proto#Slot`); the
slot otherwise lowers as any other.

(RLOW-3-6)=
### Identity-key slots [RLOW.3.6]

(RLOW-3-6-1)=
[RLOW.3.6.1] A `logical` schema's identity key is the set of slots its author
marks `key` ([LFAC.1.7]); every other slot is marked `mutable`, and there is no
default ([LGRA.5.10]). The compiler records each slot's role in the
`identity_role` field of its `Slot` atom (`proto/slot.proto#Slot`).

*see also:* [LFAC.1.7](#LFAC-1-7), [LGRA.5.10](#LGRA-5-10)

(RLOW-3-7)=
### Slot tolerance [RLOW.3.7]

(RLOW-3-7-1)=
[RLOW.3.7.1] The `tolerance` modifier records a per-slot epsilon
in the schema metadata (the `Slot` atom). The slot
descriptor itself is unchanged.

(RLOW-3-7-2)=
[RLOW.3.7.2] At fact-assert time, the engine's dedup path ([LFAC.2.5.1])
consults each compared slot's `Slot` atom:

*see also:* [LFAC.2.5.1](#LFAC-2-5-1)

(RLOW-3-7-3)=
- [RLOW.3.7.3] Slots without `tolerance`: exact equality
  comparison.

(RLOW-3-7-4)=
- [RLOW.3.7.4] Slots with `tolerance T`: two values are equal if
  they are exactly equal or `abs(new - old) <= T` ([LFAC.2.5.1]). `T` is not
  negative ([RDSC.9.11]).

*see also:* [LFAC.2.5.1](#LFAC-2-5-1), [RDSC.9.11](#RDSC-9-11)

(RLOW-3-7-5)=
- [RLOW.3.7.5] `Json` slots: deep structural equality (tolerance
  is not legal on `Json`; compile-time rejected).

(RLOW-3-7-6)=
[RLOW.3.7.6] When every tolerance-considered slot compares equal
under its per-slot rule, the incoming assertion is
a no-op and the engine suppresses the
retract-and-reassert cycle.

(RLOW-3-7-7)=
[RLOW.3.7.7] The compiler refuses, before lowering, every `tolerance` that
[LGRA.5.9] does not admit: one on a slot whose type [LGRA.5.9.4] does not
admit, one on a schema that is not `logical` ([LGRA.5.9.5]), and one on a `key`
slot ([LGRA.5.9.10]).

*see also:* [LGRA.5.9](#LGRA-5-9), [LGRA.5.9.10](#LGRA-5-9-10), [LGRA.5.9.4](#LGRA-5-9-4), [LGRA.5.9.5](#LGRA-5-9-5)

(RLOW-3-7-8)=
[RLOW.3.7.8] `tolerance 0` is explicit exact-equality — the
compiler emits no additional bookkeeping, and the
slot uses the same exact-equality path as an
unmodified slot. The declaration's value is in
making the discipline visible at the schema.

---

(RLOW-4)=
## Rules [RLOW.4]

(RLOW-4-2)=
### Lowered form [RLOW.4.2]

*checks:* `k`

(RLOW-4-2-1)=
[RLOW.4.2.1] **A rule lowers to a chain of Rete nodes ending in one
terminal.** The compiler lowers each pattern, in source order, to an
`AlphaNode` over its schema, shared with every pattern of the same schema and
tests ([IRET.6.6]), and a `BetaNode` joining it to the chain above it; a
negated pattern's beta is negative. The pattern's `where` becomes the alpha's
tests and the beta's join test, split per [RLOW.5.5]. The rule lowers to one
`TerminalNode` under the chain's last beta, carrying the RHS actions
([RDSC.4a.8]) and, where the rule has one, a fire guard ([RDSC.4a.8.2.1]).
How the chain is built is [RLOW.5]'s; expressions lower per [RLOW.6] and
actions per [RLOW.7].

*checks:* `idris×89`  —  *see also:* [IRET.6.6](#IRET-6-6), [RDSC.4a.8](#RDSC-4a-8), [RDSC.4a.8.2.1](#RDSC-4a-8-2-1), [RLOW.5](#RLOW-5), [RLOW.5.5](#RLOW-5-5), [RLOW.6](#RLOW-6), [RLOW.7](#RLOW-7)

(RLOW-4-4)=
### Binding scope [RLOW.4.4]

(RLOW-4-4-1)=
[RLOW.4.4.1] **Where a read sits decides which fact it reads.** A pattern
binding is in scope where [LGRA.8.6] places it, and a read of its slot lowers to
a `VarExpr` ([RLOW.6.3.1]). Inside the binding's own pattern's `where`, which is
that pattern's join test ([RLOW.4.2.1]), the read is of the candidate fact.
Everywhere else it is of the fact at the binding's position in the token, and
only positive patterns hold a token position, so a negated pattern's binding is
read only in its own `where`. A slot the rule never reads lowers to nothing.

*see also:* [LGRA.8.6](#LGRA-8-6), [RLOW.4.2.1](#RLOW-4-2-1), [RLOW.6.3.1](#RLOW-6-3-1)

(RLOW-4-5)=
### Let bindings [RLOW.4.5]

(RLOW-4-5-1)=
[RLOW.4.5.1] **Two lowering tiers, per [LGRA.9.8.4].** A `let`'s right-hand
side belongs to one of two purity tiers, **pure** or **sync-impure**, and the
tier decides how the compiler lowers it.

*checks:* `k`  —  *see also:* [LGRA.9.8.4](#LGRA-9-8-4)

(RLOW-4-5-2)=
#### Pure tier [RLOW.4.5.2]

(RLOW-4-5-2-1)=
[RLOW.4.5.2.1] **A pure `let` is inlined.** The compiler must substitute a
pure `let`'s right-hand side, with any earlier `let` it names already
substituted, at every use, and lower the result as any expression
([RLOW.6]). A pure `let` produces no atom, no node and no bind slot. A pure
expression used at N sites is evaluated N times: inlined, not memoised
([LLET.10.3]).

*checks:* `k`  —  *see also:* [LLET.10.3](#LLET-10-3), [RLOW.6](#RLOW-6)

(RLOW-4-5-3)=
#### Sync-impure tier [RLOW.4.5.3]

(RLOW-4-5-3-1)=
[RLOW.4.5.3.1] **A sync-impure `let` is bound once per firing.** For each
`let` of the tier [LGRA.9.8.4] names sync-impure, the compiler must emit, ahead
of the rule's other actions and in source order, an `ACTION_CALL` whose
expression is a `bind` (`proto/rete_expr.proto#BindExpr`) carrying the
`let`'s bind index and its right-hand side, and must lower every use of the
`let` to a `bind_ref` of that index. The terminal's `bind_count` is the number
of such `let`s (`proto/term.proto#TerminalNode`). Every use therefore reads one
evaluation, made once per firing ([IRET.9.4]). The clock
functions read the pump's held samples ([IRET.14.1]), which do not change within
a pump.

*checks:* `k`  —  *see also:* [IRET.14.1](#IRET-14-1), [IRET.9.4](#IRET-9-4), [LGRA.9.8.4](#LGRA-9-8-4)

(RLOW-4-5-5)=
#### Cross-tier rules [RLOW.4.5.5]

(RLOW-4-5-5-1)=
[RLOW.4.5.5.1] **Inlining moves no diagnostic.** A `let`'s type flows to
every use. The compiler type-checks a rule before it inlines any `let`, so a
type error at a use is reported at the use's own source location, and an error
in the `let`'s right-hand side at the `let`.

(RLOW-4-6)=
### Rule-header rate modifiers [RLOW.4.6]

(RLOW-4-6-1)=
[RLOW.4.6.1] **`cooldown T` lowers to a `RuleCooldown` atom and a rate gate.** For a
rule whose header carries `cooldown T`, the compiler must emit a `RuleCooldown` atom
carrying the window ([RDSC.6.5]) and a rate gate in the terminal's
`fire_guard` ([RDSC.4a.8.2.1]): a call of `FN_RATE_ALLOW` whose arguments are
the rule's name, the activation key's values ([RLOW.4.6.2.3]) in that key's
order, each a `VarExpr` read of its pattern's fact ([RLOW.6.3.1]), and the
window in nanoseconds. The runtime evaluates the gate once per firing
([RDSC.4a.8.2.3]) against the state [RLOW.4.6.2.1] defines.

The compiler emits the gate **only** when the rule carries `cooldown`. A
`cooldown` does not change how the clock re-drives the rule ([LTIM.2.4.2]).
Where the rule also has a condition in its fire guard, the guard is their
conjunction, the condition left and the rate gate right ([RDSC.4a.8.2.1]), so
the runtime evaluates the gate only once the condition holds.

*checks:* `k`  —  *see also:* [LTIM.2.4.2](#LTIM-2-4-2), [RDSC.4a.8.2.1](#RDSC-4a-8-2-1), [RDSC.4a.8.2.3](#RDSC-4a-8-2-3), [RDSC.6.5](#RDSC-6-5), [RLOW.4.6.2.1](#RLOW-4-6-2-1), [RLOW.4.6.2.3](#RLOW-4-6-2-3), [RLOW.6.3.1](#RLOW-6-3-1)

(RLOW-4-6-2)=
#### Cooldown [RLOW.4.6.2]

*checks:* `k`

(RLOW-4-6-2-1)=
[RLOW.4.6.2.1] State: `(rule_name, activation_key) → last_fire_ns`. The
activation key is the sequence of values [LCDB.3.3.3] composes, and **two keys
are the same key only if they have the same length and, position by position,
values of the same representation that are equal.** `1`, `"1"` and `true` are
three keys, not one. The representation is the value's tag, not its declared
type ([LNUL.4.9]), so a `Time` and an `Int` holding the same count are one key.

Equality within a representation is the language's `==` for a scalar — `0.0`
and `-0.0` are one key — deep structural equality for `Json` ([LFAC.2.5.1]),
and `null` equals only `null`. The comparison is **exact and whole**: nothing is
rounded, truncated or compared by prefix, and a key has no length limit of its
own. How a runtime stores a key is its own, provided the storage preserves this
identity.

*checks:* `k×2` `idris×44`  —  *see also:* [LCDB.3.3.3](#LCDB-3-3-3), [LFAC.2.5.1](#LFAC-2-5-1), [LNUL.4.9](#LNUL-4-9)

(RLOW-4-6-2-2)=
[RLOW.4.6.2.2] The compiler emits the gate per [RLOW.4.6.1] and a `RuleCooldown` atom
carrying `(rule_name, window_ns)` per [RDSC.6.5]. At gate evaluation, for a key
with a recorded `last_fire_ns`, if `mono_ns - last_fire_ns < window_ns` the
gate returns false (suppress); otherwise, and always for a key with no recorded
firing, it returns true and records `last_fire_ns = mono_ns`. Every instant,
zero included, is a valid `last_fire_ns`. `mono_ns` is the pump's monotonic
sample: `cooldown` reads the monotonic clock and no other ([LTIM.2.8.2]).

*checks:* `idris×27`  —  *see also:* [LTIM.2.8.2](#LTIM-2-8-2), [RDSC.6.5](#RDSC-6-5), [RLOW.4.6.1](#RLOW-4-6-1)

(RLOW-4-6-2-3)=
[RLOW.4.6.2.3] **Activation key.** The activation key is what the rule's
`by:` modifier names per [LCDB.3.3.4], or, with none, is composed per
[LCDB.3.3.3]. Different keys have independent cooldown state.

*checks:* `idris×3`  —  *see also:* [LCDB.3.3.3](#LCDB-3-3-3), [LCDB.3.3.4](#LCDB-3-3-4)

(RLOW-4-6-6)=
#### Per-call cooldown [RLOW.4.6.6]

(RLOW-4-6-6-1)=
[RLOW.4.6.6.1] `cooldown: <DUR>` on a `call` action lowers to a
**per-call rate-allow gate** wrapping just the call site, realized inline
at the call's `ACTION_CALL` as [IRET.25.5.1]'s `IF` over `FN_RATE_ALLOW`.
Its arguments are the call site's tracker name `<rule>#call<N>`, the key
[LCRS.8.2] names, and the window `<DUR>` in nanoseconds. `<N>` is the
0-based index of the call action within the rule, counted only for calls
with a window above zero: a `nocooldown` call, and one with `cooldown: 0_s`,
which is a no-op ([LCDB.5.6]), get no gate and don't increment it. A hydration call
carries `cooldown:` ([LHYD.9.3]), counts in `<N>`, and is gated the same
way ([LHYD.11.2]).

The gate carries its own window, so a call site has no rule-metadata atoms
of its own and `<rule>#call<N>` names nothing but the tracker. Two calls in
the same rule get independent trackers; other actions in the rule fire
normally.

*ratified*  —  *checks:* `idris×13`  —  *see also:* [IRET.25.5.1](#IRET-25-5-1), [LCDB.5.6](#LCDB-5-6), [LCRS.8.2](#LCRS-8-2), [LHYD.11.2](#LHYD-11-2), [LHYD.9.3](#LHYD-9-3)

(RLOW-4-6-6-2)=
[RLOW.4.6.6.2] The synthetic name uses `#` as separator —
illegal in a DS `NAME` per LGRA.2.5.1 so it can't
collide with author-written rule names.

*see also:* [LGRA.2.5.1](#LGRA-2-5-1)

(RLOW-4-6-6-5)=
[RLOW.4.6.6.5] **Combination with rule-level rate_mod is
rejected.** Rule-level `cooldown` and
per-call `cooldown:` would create overlapping
trackers (rule-keyed + synthetic-keyed). The
validator emits
`DS_VALIDATE_CALL_COOL_INSIDE_RULE_COOL`. Authors
who want both can split into multiple rules.

*ratified*

(RLOW-4-6-6-6)=
[RLOW.4.6.6.6] **`nocooldown` opt-out.** A `call` carrying
the `nocooldown` flag emits no surrounding gate;
the call fires every time
its enclosing rule fires. Required by LCRS.8.1 to
make the rate-limit decision explicit at every
call site.

*checks:* `idris×3`  —  *see also:* [LCRS.8.1](#LCRS-8-1)

(RLOW-5)=
## Patterns [RLOW.5]

(RLOW-5-1)=
### Type + binding [RLOW.5.1]

A pattern `<Schema>` or `<Schema> <var>` with no `where` compiles to an
`AlphaNode` over `<Schema>` with no tests ([IRET.6]) and a
`BetaNode` with no join test. A read of its binding is a `VarExpr`
([RLOW.4.4.1]).

*checks:* `idris×5`  —  *see also:* [IRET.6](#IRET-6), [RLOW.4.4.1](#RLOW-4-4-1)

(RLOW-5-2)=
### Alpha tests [RLOW.5.2]

(RLOW-5-2-1)=
[RLOW.5.2.1] **A decided comparison with a literal is an alpha test.** The
compiler **must** compile each conjunct of a pattern's `where`, taken from its
top-level `and` chain, that compares a slot of the pattern's own fact with a
non-`null` literal by `==`, `!=`, `<`, `>`, `<=` or `>=`, in either operand
order, and is decided ([RLOW.5.5]), to an alpha test on that slot
(`proto/alfa.proto#AlphaTest`): `ALPHA_TEST_EQ` for `==`, `ALPHA_TEST_CMP` with
the operator otherwise. A regex conjunct is [RLOW.5.2.2]'s. Every other
conjunct stays in the join test.

*see also:* [RLOW.5.2.2](#RLOW-5-2-2), [RLOW.5.5](#RLOW-5-5)

(RLOW-5-2-2)=
[RLOW.5.2.2] The compiler **must** compile a `where` conjunct `s.x ~ /p/f`,
where `s.x` is a slot of the pattern's own schema and the conjunct is decided
([IRET.6.4]), to an alpha regex test on that slot
(`proto/alfa.proto#AlphaTest`, `ALPHA_TEST_REGEX`). Under `linear_scan: false` it
does so only when the slot also declares a `capacity` ([LSTR.11.1],
[IRET.6.2.1]); otherwise the conjunct stays in the join test.

The test's `pattern` is `p` when `f` is empty, and otherwise RE2's scoped flag
group `(?f:p)`, with `f`'s letters. An alpha test carries no flags field, and
the group means exactly what the flags mean: DS's three flags are RE2's inline
flags of the same letters ([LREG.5.2], [LREG.8.2]). The group does not capture,
and an alpha test reads no capture.

A valid `p` does not make `(?f:p)` valid: an unterminated `\Q` quotes to the
end of the pattern and takes the group's closing `)` with it. The compiler
**must** validate the wrapped pattern, and where RE2 refuses it the conjunct
stays in the join test, so the artefact never carries an alpha test its loader
would refuse ([IRET.6.3]).

A `!~` conjunct is not compiled to an alpha test: an alpha test has no
negation, so it stays in the join test.

*ratified*  —  *see also:* [IRET.6.2.1](#IRET-6-2-1), [IRET.6.3](#IRET-6-3), [IRET.6.4](#IRET-6-4), [LREG.5.2](#LREG-5-2), [LREG.8.2](#LREG-8-2), [LSTR.11.1](#LSTR-11-1)

(RLOW-5-2-3)=
[RLOW.5.2.3] The regex is compiled at load time
([IRET.6.3]), not a per-match call.

*see also:* [IRET.6.3](#IRET-6-3)

(RLOW-5-3)=
### `no` (negation) [RLOW.5.3]

A negated pattern compiles to a NOT (negative) beta node
over the pattern's alpha node, carrying the same alpha and
join tests as the positive form ([IRET.7.9]):
the token passes only when no matching fact exists.

*checks:* `k` `idris×9`  —  *see also:* [IRET.7.9](#IRET-7-9)

(RLOW-5-4)=
### `within <dur>` [RLOW.5.4]

A `within <dur>` constraint on a bare pattern compiles to
the pattern's alpha node plus a join test comparing the
fact's age — the monotonic clock's sample minus the fact's entry
stamp ([LTIM.2.8.6]) — against the duration converted to
nanoseconds, re-evaluated as the
`$ClockTick` fact advances ([IRET.7.5],
[IRET.19.5]).

*checks:* `k` `idris`  —  *see also:* [LTIM.2.8.6](#LTIM-2-8-6), [IRET.19.5](#IRET-19-5), [IRET.7.5](#IRET-7-5)

(RLOW-5-4-1)=
[RLOW.5.4.1] `now()` lowers to the `now` expression
(`proto/rete_expr.proto#Expr`), which reads the wall clock's held sample.
`monotonic_ns()` lowers to a call of `FN_MONOTONIC_NS`, which reads the
monotonic clock's ([IRET.14]). Every relative-time test the
compiler generates, the `within` window above included, reads `monotonic_ns()`
and never `now()` ([LTIM.2.8.3]).

*see also:* [IRET.14](#IRET-14), [LTIM.2.8.3](#LTIM-2-8-3)

(RLOW-5-5)=
### `where` clauses [RLOW.5.5]

A `where` clause compiles to a beta-node join test — a
boolean expression tree over the pattern's bound slots
([IRET.7.5]): the token passes only when
the expression evaluates true.

**A conjunct may leave the join test only when it is decided.** The compiler
may compile a conjunct as an alpha test ([IRET.6.4]), and the loader may probe
on one as the join's index key ([IRET.7.5.1]), only when every slot it reads is
non-nullable and it cannot raise by the declared types of its operands, read
without narrowing ([LNUL.5.2]). A decided conjunct answers only `true` or
`false`, and a `false` decides the `where` wherever it is evaluated
([LNUL.4.2]). Taking it first therefore changes neither the fault the `where`
yields nor, for a negated pattern, whether it blocks. A conjunct that could
answer `null` or raise could change both, and stays in the join test.

*checks:* `alloy×3` `k×2` `idris×15`  —  *see also:* [IRET.6.4](#IRET-6-4), [IRET.7.5](#IRET-7-5), [IRET.7.5.1](#IRET-7-5-1), [LNUL.4.2](#LNUL-4-2), [LNUL.5.2](#LNUL-5-2)

(RLOW-5-6)=
### Pattern sequencing — `then ... within` [RLOW.5.6]

A two-step `A then B within T` sequence compiles to `A`'s
alpha node joined to `B`'s alpha node through a beta node
whose join test enforces the strict-after and
within-duration constraints (`B.ts > A.ts and B.ts - A.ts <
T`).

(RLOW-5-6-1)=
[RLOW.5.6.1] The sequence compiles to a linear left-associative
chain of beta joins ([IRET.7.10]), with each
adjacent pair's join test enforcing the strict-after and
within-duration constraints. Each `then Y within T`
contributes two predicates: `Y.ts > X.ts` and
`Y.ts - X.ts < T`.

*checks:* `k`  —  *see also:* [IRET.7.10](#IRET-7-10)

(RLOW-5-6-2)=
[RLOW.5.6.2] **Chains** extend left-associatively: each further `then`
adds one beta join whose test pins the new step against the step immediately
before it ([LTHE.2.3.1]).

*see also:* [LTHE.2.3.1](#LTHE-2-3-1)

(RLOW-5-6-3)=
[RLOW.5.6.3] Each `within` binds to the immediately preceding
pair. No parenthesisation.

(RLOW-5-6-4)=
[RLOW.5.6.4] **A trailing `where` is the last step's.** A sequence has no
sequence-wide `where`; on a one-line sequence a trailing `where` belongs to the
last step ([LGRA.8.5.10]). The compiler lowers it as part of that step's join
test, after the step's ordering and window predicates, so it reads every binding
of the sequence and adds no node.

*checks:* `k`  —  *see also:* [LGRA.8.5.10](#LGRA-8-5-10)

(RLOW-5-6-5)=
#### The `latest` modifier [RLOW.5.6.5]

(RLOW-5-6-5-1)=
[RLOW.5.6.5.1] Without `latest`, a sequence lowers to every match, as any
pattern does: the rule fires once per valid anchor ([LTHE.2.5.1]). The `latest`
prefix pins the anchor to its most recent match ([LTHE.2.5.3]), and lowers as
[RLOW.5.6.5.2] states.

*see also:* [LTHE.2.5.1](#LTHE-2-5-1), [LTHE.2.5.3](#LTHE-2-5-3), [RLOW.5.6.5.2](#RLOW-5-6-5-2)

(RLOW-5-6-5-2)=
[RLOW.5.6.5.2] Lowering `latest` adds a NOT sub-node **after the
sequence's last join**, where the anchor `d`, the first follower `m` and every
later follower are on the token ([IRET.7.10.2]). Its join test asserts no
candidate anchor that excludes the anchor with these followers
([LTHE.2.5.3]): the NOT sub-node blocks the match whenever a fact `f` of the
anchor's schema has `d.ts < f.ts < m.ts`, `m.ts − f.ts < T`, and none of the
anchor's `where` and the followers' `where`s, with `f` in the anchor's place,
answers `false` ([LNUL.4.7]).
Only the first follower's window involves the anchor ([LTHE.2.3.1]). In a
two-pattern sequence the last join is the first follower's.

*checks:* `k` `idris×6`  —  *see also:* [IRET.7.10.2](#IRET-7-10-2), [LNUL.4.7](#LNUL-4-7), [LTHE.2.3.1](#LTHE-2-3-1), [LTHE.2.5.3](#LTHE-2-5-3)

(RLOW-5-6-5-3)=
[RLOW.5.6.5.3] That exclusion says: no other anchor that excludes it
with the same followers ([LTHE.2.5.3]) exists with a timestamp strictly between the anchor's and the
first follower's. It reduces the activation set to the single latest qualifying
anchor per follower match. The test compares timestamps, not arrival order, so an
anchor whose `ts` is not before the first follower's never excludes a match,
whenever it was asserted ([LTHE.2.5.3]).

*checks:* `idris×2`  —  *see also:* [LTHE.2.5.3](#LTHE-2-5-3)

(RLOW-5-6-5-4)=
[RLOW.5.6.5.4] `latest` modifies only the first pattern in the
sequence, and middle patterns fan out normally ([LTHE.2.5.4]). The compiler
refuses a `latest` on any other pattern ([LTHE.5.4]), so only the anchor ever
lowers with the exclusion of [RLOW.5.6.5.2].

*see also:* [LTHE.2.5.4](#LTHE-2-5-4), [LTHE.5.4](#LTHE-5-4), [RLOW.5.6.5.2](#RLOW-5-6-5-2)

(RLOW-5-6-5-5)=
[RLOW.5.6.5.5] **Bindings.** A sequence binds every named variable of
every pattern in it, and each is in scope as any pattern binding is
([LGRA.8.6]): in every later step's `where`, the trailing one included, and on
the RHS. Each step's ordering predicate is strict, `Y.ts > X.ts` and never
`>=` ([LTHE.5.2]).

---

*see also:* [LGRA.8.6](#LGRA-8-6), [LTHE.5.2](#LTHE-5-2)

(RLOW-6)=
## Expressions [RLOW.6]

(RLOW-6-1)=
### Operators [RLOW.6.1]

(RLOW-6-1-1)=
[RLOW.6.1.1] Every DS binary operator lowers to a `binop` expression node
(`proto/rete_expr.proto#BinopExpr`) carrying its `BinopOp`; every unary
operator but `+` to a `unop` node (`proto/rete_expr.proto#UnopExpr`) carrying
its `UnopOp`. Unary `+` lowers to its operand, with no node of its own.

| DS operator | Node and op |
|---|---|
| `+` `-` `*` `/` | `binop`: `BINOP_ADD`, `_SUB`, `_MUL`, `_DIV` |
| `//` `%` | `binop`: `BINOP_IDIV`, `_MOD` |
| `==` `!=` `<` `>` `<=` `>=` | `binop`: `BINOP_EQ`, `_NE`, `_LT`, `_GT`, `_LE`, `_GE` |
| `and` `or` | `binop`: `BINOP_AND`, `_OR` (absorbing, [LNUL.4.2]) |
| `&` `\|` `^` `<<` `>>` | `binop`: `BINOP_BAND`, `_BOR`, `_BXOR`, `_SHL`, `_SHR` |
| `~` | `unop`: `UNOP_BNOT` |
| `not` | `unop`: `UNOP_NOT` |
| unary `-` | `unop`: `UNOP_NEG` |

The operator set and each operator's operand types are [LEXP.1]'s.

*see also:* [LEXP.1](#LEXP-1), [LNUL.4.2](#LNUL-4-2)

(RLOW-6-1-2)=
[RLOW.6.1.2] Bitwise and shift operators lower to their own `BinopOp` and
`UnopOp` members. The expression tree carries bit and shift operations
natively; none is rewritten into arithmetic.

(RLOW-6-1-7)=
[RLOW.6.1.7] **An operation on an interval lowers inside `FN_INTERVAL_CHECK`.**
No runtime holds an interval value: the compiler lowers each operation on one to
comparisons of its endpoints ([LINT.3.1.1], [LINT.3.2.1], [LINT.4.1],
[LINT.5.1], [LINT.6.1]). It must lower each such operation to
`FN_INTERVAL_CHECK(s₁, e₁, v)`, or to `FN_INTERVAL_CHECK(s₁, e₁, s₂, e₂, v)` for
an operation with two interval operands (`proto/builtin_fn.proto#BuiltinFn`),
where each `sᵢ .. eᵢ` is an interval operand and `v` is the lowered endpoint
comparison or component read. The check encloses `v`. It is never an operand of
an `and` beside it, where a `false` operand would absorb its fault ([LNUL.4.2]).

The engine evaluates `FN_INTERVAL_CHECK` as [LINT.7.2] evaluates `..`:

1. If no argument faults and every pair is valid, it yields `v`, `null` included.
2. Otherwise it yields one fault, chosen as [LNUL.4.2] chooses among faults, from
   the faults of its arguments and, for each pair whose endpoints are two values,
   neither `null`, that are not `s <= e`, a `ComputationError` of kind `DOMAIN`
   ([LERR.1.4.10]) whose offending operand is that pair's `s`, as [LERR.9.12]
   chooses it for `..`.

A `null` `v` does not prevent the check, as a `null` operand prevents a builtin's
operation ([LNUL.4.10]): the check is the `..` operation, which [LINT.7.2]
performs whatever the enclosing operation's other operand is. A pair with a
`null` endpoint is valid.

Where both endpoints of a pair are compile-time constants ([LEXP.8.8]) and
`s <= e`, the compiler may leave the pair out of the check, and lowers the
operation to `v` alone when no pair is left. It must not fold an operation over
an invalid interval to a value; an invalid interval of literals is refused
instead ([LINT.7.4]).

*ratified*  —  *defined in:* [proto/builtin_fn.proto#BuiltinFn](#proto-builtin_fn-proto-BuiltinFn)  —  *see also:* [LERR.1.4.10](#LERR-1-4-10), [LERR.9.12](#LERR-9-12), [LEXP.8.8](#LEXP-8-8), [LINT.3.1.1](#LINT-3-1-1), [LINT.3.2.1](#LINT-3-2-1), [LINT.4.1](#LINT-4-1), [LINT.5.1](#LINT-5-1), [LINT.6.1](#LINT-6-1), [LINT.7.2](#LINT-7-2), [LINT.7.4](#LINT-7-4), [LNUL.4.10](#LNUL-4-10), [LNUL.4.2](#LNUL-4-2)

(RLOW-6-1-3)=
[RLOW.6.1.3] Precedence is applied at parse time; the lowered
expression tree directly encodes the parse tree, so precedence
never needs re-deriving downstream.

(RLOW-6-1-4)=
[RLOW.6.1.4] **The compiler must not simplify `and` / `or` by absorption or
distributivity.** Neither law holds once an operand can fault ([LNUL.4.2]):
`a and (a or b)` → `a` turns the fault `null and (null or t)` yields into
`null`, and distributing can do the same with an `EngineError`. The compiler may
reorder operands and apply commutativity, associativity, De Morgan and double
negation, which do hold. It may fold a literal `false` operand of an `and` (a
literal `true` of an `or`) to that literal, because the literal decides whatever
the other operands raise.

*checks:* `alloy` `k` `idris×3`  —  *see also:* [LNUL.4.2](#LNUL-4-2)

(RLOW-6-1-5)=
[RLOW.6.1.5] **`Date` arithmetic lowers to its own builtins.** A `Date` is held
as a day count and a `Duration` as nanoseconds ([RDSC.5.5.8]), so the integer
`+` and `-` would mix the two units. The compiler must lower `d + dur` to
`FN_DATE_ADD_DURATION(d, dur)`, `d - dur` to `FN_DATE_SUB_DURATION(d, dur)`, and
`a - b` with two `Date` operands to `FN_DATE_DIFF(a, b)`
(`proto/builtin_fn.proto#BuiltinFn`), and must not lower any of them to integer
arithmetic. The engine evaluates each as [LTIM.4.2.3] states.

*ratified*  —  *defined in:* [proto/builtin_fn.proto#BuiltinFn](#proto-builtin_fn-proto-BuiltinFn)  —  *see also:* [LTIM.4.2.3](#LTIM-4-2-3), [RDSC.5.5.8](#RDSC-5-5-8)

(RLOW-6-1-6)=
[RLOW.6.1.6] **A `Duration`-valued `-`, `*` or `/` lowers through `FN_MAX`.**
A `Duration` is held as nanoseconds and is never negative ([LTIM.1.3.1]), and
the integer operations do not stop at zero ([LEXP.7.13.1]). The compiler must
lower `a - b` with two `Time` or two `Duration` operands, `n * dur`, `dur * n`,
`dur / n` and `dur // n` to `FN_MAX(<the integer operation>, 0)`
(`proto/builtin_fn.proto#BuiltinFn`), and its constant folder must fold each to
the value that lowering computes ([LDET.3.3a.8]). `Date - Date` saturates
inside `FN_DATE_DIFF` ([RLOW.6.1.5], [LTIM.4.2.3]).

With a `Float` operand `f`, the compiler must lower `dur * f`, `f * dur`,
`dur / f` and `dur // f` to `FN_MAX(FN_ROUND(<the Float operation>), 0)`, folded
the same way. `FN_ROUND` raises what [LTIM.4.1.1] states. `a % b`, `a / b` and
`a // b` with two `Duration` operands lower as on two `Int`s. A `Duration?` read
of [LSER.17.2] lowers to `IF(r is null, r, FN_ROUND(r))`, with `r` the read.

The engine's subtraction of two `Time` operands must not wrap ([LTIM.4.1.1]). An
exact result below the integer range reaches `FN_MAX` as a negative value, and
one above it raises before `FN_MAX` runs.

**A `null` operand still yields `null`** ([LNUL.4.1]), and `FN_MAX` faults on
one. So where an operand's static type is nullable, the compiler must lower the
operation to `IF(x is null, x, FN_MAX(x, 0))`, with `x` the integer operation or
the `FN_ROUND`, and lowers it to the bare `FN_MAX(x, 0)` only where neither operand is nullable.

*ratified*  —  *defined in:* [proto/builtin_fn.proto#BuiltinFn](#proto-builtin_fn-proto-BuiltinFn)  —  *see also:* [LDET.3.3a.8](#LDET-3-3a-8), [LEXP.7.13.1](#LEXP-7-13-1), [LNUL.4.1](#LNUL-4-1), [LSER.17.2](#LSER-17-2), [LTIM.1.3.1](#LTIM-1-3-1), [LTIM.4.1.1](#LTIM-4-1-1), [LTIM.4.2.3](#LTIM-4-2-3), [RLOW.6.1.5](#RLOW-6-1-5)

(RLOW-6-2)=
### Comparison chains [RLOW.6.2]

A comparison chain (`a < b < c`) compiles to comparison `binop` nodes joined
by `BINOP_AND` — `and(lt(a, b), lt(b, c))`.

(RLOW-6-2-1)=
[RLOW.6.2.1] Each adjacent pair becomes one comparison; the
chain is ANDed. Same lowering for mixed
operators (`a < b <= c` → `and(lt(a, b), le(b, c))`).

(RLOW-6-3)=
### Path access [RLOW.6.3]

(RLOW-6-3-1)=
[RLOW.6.3.1] `b.s`, where `s` is a slot that the schema of pattern binding
`b` declares, lowers to one `var` node (`proto/rete_expr.proto#VarExpr`)
carrying `b`'s pattern index and `s`'s slot index.

(RLOW-6-3-2)=
[RLOW.6.3.2] The compiler resolves the slot against the binding's schema at
compile time. A path continues past the slot only when the slot is `Json`
([RLOW.6.3.3]).

*see also:* [RLOW.6.3.3](#RLOW-6-3-3)

(RLOW-6-3-3)=
[RLOW.6.3.3] A path that continues past a `Json` slot (no compile-time
schema) lowers to a `call` node (`proto/rete_expr.proto#CallExpr`) of
`FN_JSON_PATH` (`proto/builtin_fn.proto#BuiltinFn`). Its arguments are the
slot's `var` node and the rest of the path, as a string literal.

(RLOW-6-3-4)=
[RLOW.6.3.4] The Json-path traversal returns the leaf value, or `null` where
the path does not resolve ([LNUL.8.4]). A leaf whose type does not match the
operator raises a `TypeError` and abandons the activation ([LEXP.7.12.1]). No
marker is returned and nothing propagates as data.

*see also:* [LEXP.7.12.1](#LEXP-7-12-1), [LNUL.8.4](#LNUL-8-4)

(RLOW-6-4)=
### String interpolation [RLOW.6.4]

An interpolated string literal compiles, whole, to one `InterpExpr` node
(`proto/rete_expr.proto#InterpExpr`): a sequence of `FormatPrim`s in source
order. The runtime renders the node in one pass over that sequence, appending
each primitive's text. The compiler must resolve every format spec into the
primitives' fields. No format string reaches the runtime, and the runtime must
carry no code that interprets one.

*defined in:* [proto/rete_expr.proto#InterpExpr](#proto-rete_expr-proto-InterpExpr)

(RLOW-6-4-1)=
[RLOW.6.4.1] Each run of literal text between interpolation sites, with its
`{{` and `}}` already reduced to `{` and `}`, compiles to a `text` primitive.
Each site compiles to one primitive, by its spec:

(RLOW-6-4-2)=
- [RLOW.6.4.2] A site with a numeric or string spec, or with no spec, compiles
  to a `value` primitive (`proto/rete_expr.proto#ValuePrim`) whose fields hold
  the spec's parts ([LGRA.2.9.6.5]): alignment, `+`, `0`, width, precision and
  type. A site with no spec leaves every field at its zero value, which renders
  the value's canonical text ([LGRA.2.9.6.10]).

*defined in:* [proto/rete_expr.proto#ValuePrim](#proto-rete_expr-proto-ValuePrim)  —  *see also:* [LGRA.2.9.6.10](#LGRA-2-9-6-10), [LGRA.2.9.6.5](#LGRA-2-9-6-5)

(RLOW-6-4-3)=
- [RLOW.6.4.3] A site with a timestamp spec ([LGRA.2.9.6.4]) compiles to a
  `time` primitive (`proto/rete_expr.proto#TimePrim`): the value, evaluated
  once, and the pattern as a sequence of items — one field per directive, and
  text for the literal characters and each `%%` between them.

*defined in:* [proto/rete_expr.proto#TimePrim](#proto-rete_expr-proto-TimePrim)  —  *see also:* [LGRA.2.9.6.4](#LGRA-2-9-6-4)

(RLOW-6-4-4)=
- [RLOW.6.4.4] Literal `{` and `}` in an interpolated string are
  written as `{{` and `}}` in source and compile
  to literal `{` and `}` in the output.

(RLOW-6-4-5)=
[RLOW.6.4.5] Malformed interpolation (mismatched braces,
unknown binding, unparseable format spec) is a
compile error.

---

(RLOW-7)=
## Actions [RLOW.7]

(RLOW-7-1)=
### `emit X` / `emit X logical` [RLOW.7.1]

(RLOW-7-1-1)=
[RLOW.7.1.1] Intra-tier fact assertion (same tier's working
memory). An `emit` action compiles to a terminal action of
kind `ACTION_ASSERT` ([RDSC.4a.8]) carrying the target schema,
one slot assignment per slot kwarg the action gives, and one
`null` assignment per nullable slot it omits ([LGRA.13.11]). A
`retain:` is the fact's TTL override ([LDTL.4.1]), not a slot
assignment. The runtime asserts the fact via the dispatcher
([IDSP.3]).

*see also:* [IDSP.3](#IDSP-3), [LDTL.4.1](#LDTL-4-1), [LGRA.13.11](#LGRA-13-11), [RDSC.4a.8](#RDSC-4a-8)

(RLOW-7-1-2)=
[RLOW.7.1.2] Where the target schema declares `ts`, the `TerminalNode`'s emit action
carries no value for it, and the runtime fills it with the wall clock's current sample as a
UTC `Time` ([LTIM.2.8.3], [LFAC.9.2]), so an emitted fact's `ts` is source time
like an adapter's.

*ratified*  —  *checks:* `idris×2`  —  *see also:* [LFAC.9.2](#LFAC-9-2), [LTIM.2.8.3](#LTIM-2-8-3)

(RLOW-7-1-3)=
[RLOW.7.1.3] A `logical` emit compiles to the same action
with kind `ACTION_LOGICAL_ASSERT` ([RDSC.4a.8]).

*see also:* [RDSC.4a.8](#RDSC-4a-8)

(RLOW-7-1-4)=
[RLOW.7.1.4] A logical emit passes the firing activation
token as the TMS justifier ([IRET.12]): the engine retracts the emitted
fact when the justifying activation ends. The compiler refuses `emit X logical`
unless `X` is declared `logical` ([LFAC.4.5], [LLOG.11.1]).

*see also:* [IRET.12](#IRET-12), [LFAC.4.5](#LFAC-4-5), [LLOG.11.1](#LLOG-11-1)

(RLOW-7-2)=
### `publish X` [RLOW.7.2]

(RLOW-7-2-1)=
[RLOW.7.2.1] Cross-tier upward fact push. A `publish`
action compiles to a terminal action of kind `ACTION_CALL`
([RDSC.4a.8]) whose expression calls `FN_PUBLISH` over the
target schema's name and the slot values. The runtime routes it
through the dispatcher's publish path
([IDSP.4]).

*see also:* [IDSP.4](#IDSP-4), [RDSC.4a.8](#RDSC-4a-8)

(RLOW-7-3)=
### `call` [RLOW.7.3]

A `call` action compiles to a terminal action of kind
`ACTION_CALL` ([RDSC.4a.8]) whose expression calls `FN_CALL`
over the service, the correlation and the call's typed arguments
([RLOW.7.3.1]); a `cooldown` wraps that call in an `IF` over
`FN_RATE_ALLOW` inline ([IRET.25.5]), keyed by the call
site's tracker name and carrying its own window. See RLOW.4.6.6 for the
per-call gate's mechanics.

*see also:* [IRET.25.5](#IRET-25-5), [RDSC.4a.8](#RDSC-4a-8), [RLOW.4.6.6](#RLOW-4-6-6), [RLOW.7.3.1](#RLOW-7-3-1)

(RLOW-7-3-1)=
[RLOW.7.3.1] The call's `ACTION_CALL` enters the dispatcher ([IDSP.6]) as
`(service, correlation, args)`: the service from the dotted path; the
correlation from `as`, or, for a call with no `as`, the one the compiler
generates for each firing ([LCRS.3.1], [LCCO.3.4]); and one typed argument per kwarg the call supplies other than the call-site modifiers
`cooldown` and `as` ([LCRS.14.13]). The compiler emits each such
kwarg as its name and its value expression, sorted by name and laid out as
[RDSC.4a.9.4] states, so the artefact is the same whatever the source order. The runtime evaluates each value to its
kind ([RFCS.23.2]); a fact kwarg's binding becomes a `FactExternal`
([RFCS.23.1]). No JSON is built. A `Float` argument whose value is NaN refuses
the call ([LEXP.4.2]).

*ratified*  —  *see also:* [IDSP.6](#IDSP-6), [LCCO.3.4](#LCCO-3-4), [LCRS.14.13](#LCRS-14-13), [LCRS.3.1](#LCRS-3-1), [LEXP.4.2](#LEXP-4-2), [RDSC.4a.9.4](#RDSC-4a-9-4), [RFCS.23.1](#RFCS-23-1), [RFCS.23.2](#RFCS-23-2)

(RLOW-7-3-9)=
[RLOW.7.3.9] **A quantity argument on a unit-typed kwarg is lowered in the
kwarg's unit** ([LADP.8.5.3]). Unless the kwarg's unit is tier-2 ([LUNI.4.3]),
the compiler folds a value it knows at compile time into the kwarg's unit and
emits the result as a literal. For any other value, and for every value on a
tier-2 unit, it wraps the argument's expression in the unit's conversion from
the base unit:

- where the dimension's storage is `float64` ([LUNI.7.2]), the conversion built
  from the arithmetic expression nodes the engine already evaluates, with the
  constants the artefact carries ([LUNI.2.7.1]): a tier-1 unit's inverse affine
  map, and a tier-2 unit's `from_base` ([LUNI.4.3]);
- where the dimension's storage is `int64`, `FN_INT_DIV_EXACT`
  (`proto/builtin_fn.proto#BuiltinFn`) of the base-unit value and the unit's
  factor, which is a whole number of base units ([LUNI.2.7.1]).

`FN_INT_DIV_EXACT(n, d)` yields `n / d` when `d` divides `n` exactly. When `d`
is zero, the engine must raise a `ComputationError` of kind `DIVIDE_BY_ZERO`;
when `d` does not divide `n`, one of kind `NOT_REPRESENTABLE` ([LERR.1.4]). A
null operand yields null ([LNUL.4.1]).

*ratified*  —  *defined in:* [proto/builtin_fn.proto#BuiltinFn](#proto-builtin_fn-proto-BuiltinFn)  —  *see also:* [LADP.8.5.3](#LADP-8-5-3), [LERR.1.4](#LERR-1-4), [LNUL.4.1](#LNUL-4-1), [LUNI.2.7.1](#LUNI-2-7-1), [LUNI.4.3](#LUNI-4-3), [LUNI.7.2](#LUNI-7-2)

(RLOW-7-3-10)=
[RLOW.7.3.10] **Each value of the correlation counter is bound once per
firing.** The compiler must lower each value [LCCO.3.7] has a terminal take to
one `FN_GENSYM` (`proto/builtin_fn.proto#BuiltinFn`), bound before the
terminal's first action and in [LCCO.3.7]'s order. Every reader of that
correlation — the call, a continuation's capture fact ([LCCO.5.2]) and its
timeout timer — must read that one binding. A second `FN_GENSYM` for the same
correlation would give one activation two correlations, and its siblings would
join neither.

*defined in:* [proto/builtin_fn.proto#BuiltinFn](#proto-builtin_fn-proto-BuiltinFn)  —  *see also:* [LCCO.3.7](#LCCO-3-7), [LCCO.5.2](#LCCO-5-2)

(RLOW-7-3-8)=
[RLOW.7.3.8] A call compiles to no supersedure sweep of its own. The
retraction [LCRS.6.2.1] requires is the engine's: it performs it while
evaluating the call's `FN_CALL` ([RDSC.4a.9.4]), on that call's correlation and
before it issues the request, taking the response schema from the service's
`ServiceDef` (`proto/service_def.proto#ServiceDef`, [LCRS.4.3.2]).

So the call's `cooldown` gate ([RLOW.4.6.6.1]) covers the sweep as it covers
the call: a gate that denies the call leaves its `FN_CALL` unevaluated, and the
call retracts nothing. A correlation the compiler generates for a call with no
`as` ([LCRS.3.1]) is swept as an `as` value is; it is unique within the engine
([LCCO.3.4]), so no earlier call's answer carries it.

*ratified*  —  *checks:* `tla×2` `idris×23`  —  *see also:* [LCCO.3.4](#LCCO-3-4), [LCRS.3.1](#LCRS-3-1), [LCRS.4.3.2](#LCRS-4-3-2), [LCRS.6.2.1](#LCRS-6-2-1), [RDSC.4a.9.4](#RDSC-4a-9-4), [RLOW.4.6.6.1](#RLOW-4-6-6-1)

(RLOW-7-3-2)=
[RLOW.7.3.2] Compile-time checks:

(RLOW-7-3-3)=
- [RLOW.7.3.3] Service must resolve in the dispatch table. There is
  no deferred-validation path ([RLOW.13.12]).

*see also:* [RLOW.13.12](#RLOW-13-12)

(RLOW-7-3-4)=
- [RLOW.7.3.4] Arg types must match the service's request
  schema.

(RLOW-7-3-5)=
- [RLOW.7.3.5] Exactly one of `cooldown: <dur>` or
  `nocooldown` must be present ([LCRS.8.1]). `cooldown:` lowers
  per [RLOW.4.6.6]; `nocooldown` lowers to the bare
  `ACTION_CALL` with no surrounding gate.

*see also:* [LCRS.8.1](#LCRS-8-1), [RLOW.4.6.6](#RLOW-4-6-6)

(RLOW-7-3-7)=
[RLOW.7.3.7] A hydration call's kwargs are validated against its
service's declaration ([LHYD.9.2]). `retain:` is the call site's
([LCRS.14.13]); `filter:` is accepted only where the service declares
`filter_fields` ([LHYD.8.1]), which the storage library's services do not
([LSTO.13.1]).

*Deferred: not binding in this edition.*

*see also:* [LHYD.9.2](#LHYD-9-2), [LCRS.14.13](#LCRS-14-13), [LHYD.8.1](#LHYD-8-1), [LSTO.13.1](#LSTO-13-1)

(RLOW-7-4)=
### `cache` / `cache_delete` [RLOW.7.4]

A `cache` action compiles to a terminal action of kind
`ACTION_CALL` ([RDSC.4a.8]) whose expression calls
`FN_CACHE_SET` over the key, the value wrapped in an
`FN_JSON_VALUE` call, and the TTL (zero when the action gives
none). A `cache_delete` calls `FN_CACHE_DELETE` over the key.
The key is the action's string literal ([LGRA.10.3.7]), so it
lowers to a literal. The runtime routes each to its cache
dispatcher entry ([IDSP.7]).

The value goes through `FN_JSON_VALUE` so that any
expression ([LSTO.4.2.1]) — literal or runtime — reaches the
cache as a JSON value.

*see also:* [IDSP.7](#IDSP-7), [LGRA.10.3.7](#LGRA-10-3-7), [LSTO.4.2.1](#LSTO-4-2-1), [RDSC.4a.8](#RDSC-4a-8)

(RLOW-7-5)=
### The read expression `cached` [RLOW.7.5]

(RLOW-7-5-1)=
[RLOW.7.5.1] `cached(key)` is an expression, not an action, and appears
where [LGRA.9.8.2] admits it: as the whole right-hand side of a `let`, or in a
`where`. In a `let` it is sync-impure ([LGRA.9.8.4]) and lowers per
[RLOW.4.5.3.1], bound once per firing.

*see also:* [LGRA.9.8.2](#LGRA-9-8-2), [LGRA.9.8.4](#LGRA-9-8-4), [RLOW.4.5.3.1](#RLOW-4-5-3-1)

(RLOW-7-7)=
### `log` [RLOW.7.7]

A `log` action compiles to a log action carrying the
severity level, the category synthesised from the rule
name, and the message; see [IDSP.2].

*see also:* [IDSP.2](#IDSP-2)

(RLOW-7-7-1)=
[RLOW.7.7.1] **`log`** lowers to a terminal action of kind
`ACTION_CALL` ([RDSC.4a.8]) whose expression calls `FN_LOG`
over `(level, category, message)`. The compiler:

- Encodes the source-side level, in any case (so `info`,
  `INFO`, and `Info` are one level), as its
  `proto/log_level.proto#LogSeverity` number.
- Synthesises the `category` argument as the rule's
  name (e.g. `"trig_logger"` for a rule named
  `trig_logger`). The category has no source-level form;
  the author does not set it.

*defined in:* [proto/log_level.proto#LogSeverity](#proto-log_level-proto-LogSeverity)  —  *see also:* [LGRA.10.1](#LGRA-10-1), [RDSC.4a.8](#RDSC-4a-8)

(RLOW-7-8)=
### Comment passthrough [RLOW.7.8]

(RLOW-7-8-1)=
[RLOW.7.8.1] DS source comments per LGRA.2.4 (`#…` to end of
line) are non-semantic. They are **preserved** through
compilation by the `DsSource` atom ([RDSC.14]),
which carries the module's original DS source text verbatim
— comments and all. A tool that wants a node's source context reads it from
`DsSource[module_id]` via the node's `LineSpan` atom
([RDSC.14.3]).

---

*see also:* [LGRA.2.4](#LGRA-2-4), [RDSC.14](#RDSC-14), [RDSC.14.3](#RDSC-14-3)

(RLOW-8)=
## Series and companion slots [RLOW.8]

(RLOW-8-1)=
### Series declaration [RLOW.8.1]

(RLOW-8-1-1)=
[RLOW.8.1.1] Series declarations are not directly observable
runtime constructs. The compiler resolves the declaration's
observation lines (per [LSER.13])
into one or more **runtime backend atoms** per
[RDSC.5a], keyed as [RDSC.5a.8.1] states. Identity-slot resolution
follows [LSER.7.6.1] — the `by:` block-option, or the schema's
declared identity where it has one, with no by-name default;
schema-name resolution follows [LSER.7.7] (unique-required
scoping).

*see also:* [LSER.13](#LSER-13), [LSER.7.6.1](#LSER-7-6-1), [LSER.7.7](#LSER-7-7), [RDSC.5a](#RDSC-5a), [RDSC.5a.8.1](#RDSC-5a-8-1)

(RLOW-8-1-2)=
[RLOW.8.1.2] **Backend selection.** The compiler selects backings as
[RDSC.5a.2] states, from [LSER.18.1]'s matrix, and emits one backend atom per
(backing, window), each carrying the window of the observation line it serves
([RDSC.5a.8.1]). The author never names a backing. A line with no exact
backing is refused before lowering ([LSER.15.2]). A declaration no lowered rule
reads emits no backend atom.

*see also:* [LSER.15.2](#LSER-15-2), [LSER.18.1](#LSER-18-1), [RDSC.5a.2](#RDSC-5a-2), [RDSC.5a.8.1](#RDSC-5a-8-1)

(RLOW-8-1-3)=
[RLOW.8.1.3] **Sample absorption is an engine step, not a
compiled rule.** The compiler emits the backend atoms and
lowers each read to its aggregation call ([RLOW.8.1.4]); it synthesises **no**
rule for absorption. The engine absorbs once per pump, after
the agenda drains, per [LSER.8.6].

What the compiler does still owe the engine is the keying: the
per-(schema, slot, identity-value) backend state is keyed as the aggregation
reads it ([RLOW.8.2.1]).

*see also:* [LSER.8.6](#LSER-8-6), [RLOW.8.1.4](#RLOW-8-1-4), [RLOW.8.2.1](#RLOW-8-2-1)

(RLOW-8-1-4)=
[RLOW.8.1.4] **Aggregation calls.** The compiler lowers each series read
of an [LSER.16] aggregation to a call of that aggregation's `FN_SERIES_*`
builtin (`proto/builtin_fn.proto#BuiltinFn`) whose arguments are, in order: the
schema's name, the slot's name, the identity slot's name (empty when the series
has none), the identity value read from the bound fact, and the window of the
observation line the read resolves to ([LSER.6.11.1]), in nanoseconds and `0`
for `all_time` and `sample`. A percentile is `FN_SERIES_PERCENTILE` with its
rank as a sixth argument; `count_of` and `contains` take their value there. The
runtime answers from the backend state keyed on that row ([RDSC.5a.8.1]).

*see also:* [LSER.16](#LSER-16), [LSER.6.11.1](#LSER-6-11-1), [RDSC.5a.8.1](#RDSC-5a-8-1)

(RLOW-8-2)=
### Aggregation call [RLOW.8.2]

A series aggregation call in a `where` clause compiles to
the pattern's alpha node plus a beta-node join test whose
expression calls the aggregation's `FN_SERIES_*` builtin
([RDSC.4a.9]) over the pattern's schema, slot, and identity
binding.

*see also:* [RDSC.4a.9](#RDSC-4a-9)

(RLOW-8-2-1)=
[RLOW.8.2.1] The call reads the backend state for the fact's own identity
value ([RLOW.8.1.4]). Every aggregation read, in a `where` or on the RHS, sees
the **pre-sample snapshot** per [LSER.8.6.1]: the state as of the start of the
pump, before any sample absorbed in it, not merely before the current fact's
contribution.

*see also:* [LSER.8.6.1](#LSER-8-6-1), [RLOW.8.1.4](#RLOW-8-1-4)

(RLOW-8-3)=
### Companion slots [RLOW.8.3]

(RLOW-8-3-1)=
[RLOW.8.3.1] Companion slots (`s.mean`, `s.stdev`,
`s.median`, `s.p95`, `s.dwell`, `s.direction`, etc.)
desugar at compile time per [LSER.6.3]:

*see also:* [LSER.6.3](#LSER-6-3)

(RLOW-8-3-2)=
- [RLOW.8.3.2] `s.mean` → `mean(s)`, and so for every aggregation: the call
  [RLOW.8.1.4] lowers.

*see also:* [RLOW.8.1.4](#RLOW-8-1-4)

(RLOW-8-3-3)=
- [RLOW.8.3.3] `s.stdev` → `stdev(s)`. The read names no window
  ([LSER.6.11.3]); its call carries the window of the line it resolves to.

*see also:* [LSER.6.11.3](#LSER-6-11-3)

(RLOW-8-3-4)=
- [RLOW.8.3.4] `s.p95` → `p95(s)`, a call of `FN_SERIES_PERCENTILE` whose
  rank is `95`.

(RLOW-8-3-5)=
- [RLOW.8.3.5] `s.dwell` (only on `logical` bindings) →
  the monotonic clock's sample minus the fact's entry stamp
  ([LEXP.9.5.1]): a `CallExpr` of `FN_MONOTONIC_NS` minus an
  `entry_stamp` read (`proto/rete_expr.proto#Expr`) — a pure compile-time alias, not a
  series query (per [LSER.10.4]).

*see also:* [LEXP.9.5.1](#LEXP-9-5-1), [LSER.10.4](#LSER-10-4)

(RLOW-8-3-6)=
[RLOW.8.3.6] The window comes from the observation line the companion
resolves to ([LSER.6.11.1]): the slot, the aggregation, and the label where the
slot declares that aggregation on more than one line. A companion reference with
no declared observation covering it is a compile error per [LSER.6.7.1].

*see also:* [LSER.6.11.1](#LSER-6-11-1), [LSER.6.7.1](#LSER-6-7-1)

(RLOW-8-3-7)=
[RLOW.8.3.7] The desugaring runs **before** the lazy
backend-emission pass — companion-slot references count
as aggregation references, so they drive backend atom
emission per [RLOW.8.1.2].

*see also:* [RLOW.8.1.2](#RLOW-8-1-2)

(RLOW-8-4)=
### Hydrated facts skip series [RLOW.8.4]

*Deferred: not binding in this edition.*

(RLOW-8-4-1)=
[RLOW.8.4.1] Per [LHYD.10.4], on a hydration target ([LHYD.4.1.7]) the
series-update rule's alpha node pins `corr == ""`, restricting state
updates to live facts. Hydrated facts (with non-empty
`corr`) match author patterns normally but do
not contribute to series state.

*see also:* [LHYD.10.4](#LHYD-10-4), [LHYD.4.1.7](#LHYD-4-1-7)

(RLOW-8-4-2)=
[RLOW.8.4.2] Opt-in for hydrated-in-series inverts the
predicate. Syntax for the opt-in is deferred per
[LHYD.10.4.2] until a real use case appears.

---

*see also:* [LHYD.10.4.2](#LHYD-10-4-2)

(RLOW-9)=
## Synthetic identifiers [RLOW.9]

(RLOW-9-1)=
### Synthetic names [RLOW.9.1]

(RLOW-9-1-2)=
[RLOW.9.1.2] The compiler uses synthetic names ([LLEX.10.6.1]) for:

*see also:* [LLEX.10.6.1](#LLEX-10-6-1)

(RLOW-9-1-4)=
- [RLOW.9.1.4] **Compiler-generated bindings.** A pattern written without a
  binding gets a synthetic one, which reaches a display name ([RDSC.6.8.9]).

*see also:* [RDSC.6.8.9](#RDSC-6-8-9)

(RLOW-9-1-5)=
- [RLOW.9.1.5] **Compiler-generated auxiliary rules.** Every rule the
  compiler synthesises, a call continuation's sibling ([LCCO.3.1]) or an
  `after` follow-up among them, gets a synthetic name. Series absorption
  and the clock tick are engine steps, not rules ([RLOW.8.1.3]).

*see also:* [LCCO.3.1](#LCCO-3-1), [RLOW.8.1.3](#RLOW-8-1-3)

(RLOW-9-1-6)=
- [RLOW.9.1.6] **Engine-internal facts.** `$ClockTick` and
  other facts the runtime asserts directly.

(RLOW-9-2)=
### Mangling scheme [RLOW.9.2]

(RLOW-9-2-1)=
[RLOW.9.2.1] The compiler uses a deterministic mangling scheme,
source-derived and not random ([LDET.3.2.6]), to generate unique
synthetic names. The specific scheme is implementation, except where a
clause fixes a name's form; what the compiler commits to is determinism: the
same DS source compiles to the same synthetic-identifier set every time.

*see also:* [LDET.3.2.6](#LDET-3-2-6)

(RLOW-9-3)=
### IR- and protobuf-level treatment [RLOW.9.3]

(RLOW-9-3-1)=
[RLOW.9.3.1] A synthetic name is legal as a schema or rule name in the DSC
metadata atoms, which carry names as strings. The compiler emits synthetic
names unchanged into the schema-name, rule-name and fact-name positions of the
atom set. It writes no `.proto` text for a synthetic schema ([RDSC.5.8.3]): no
protobuf identifier can hold a `$`.

*see also:* [RDSC.5.8.3](#RDSC-5-8-3)

(RLOW-9-3-2)=
[RLOW.9.3.2] Slot reads in the lowered network are `VarExpr` positions
([RLOW.6.3.1]), not names, so a synthetic binding name appears in no
expression. Where the artefact carries a name, a synthetic one is written
unchanged, `$` and all ([RLOW.9.3.1]).

---

*see also:* [RLOW.6.3.1](#RLOW-6-3-1), [RLOW.9.3.1](#RLOW-9-3-1)

(RLOW-10)=
## Per-tier lowering differences [RLOW.10]

(RLOW-10-1)=
### Dialect gating [RLOW.10.1]

(RLOW-10-1-1)=
[RLOW.10.1.1] The compiler resolves the program's settings ([LDIA.1.3])
to determine which constructs are permitted. Rules using a construct its
settings forbid fail compilation with the error shape of [LDIA.5].

*see also:* [LDIA.1.3](#LDIA-1-3), [LDIA.5](#LDIA-5)

(RLOW-10-2)=
### Gated-form lowering impact [RLOW.10.2]

(RLOW-10-2-1)=
[RLOW.10.2.1] A capability set `false` refuses the forms it gates
([LFCA.2.1], [LFCA.5]); the compile fails, so no node is generated for the
rule. Nothing else in the artefact moves:

*see also:* [LFCA.2.1](#LFCA-2-1), [LFCA.5](#LFCA-5)

(RLOW-10-2-2)=
- [RLOW.10.2.2] **`series: false`.** A `series` declaration fails
  compilation, so no backend atom is emitted, and a series read has no
  declaration to resolve against ([LSER.6.7.1]).

*see also:* [LSER.6.7.1](#LSER-6-7-1)

(RLOW-10-2-3)=
- [RLOW.10.2.3] **`cache: false`, `cache_delete: false`.** Each refuses its
  own action verb. A `cached(key)` read is gated by neither and lowers
  unchanged ([LFCA.2.4]).

*see also:* [LFCA.2.4](#LFCA-2-4)

(RLOW-10-2-4)=
- [RLOW.10.2.4] **`call` disabled.** The `call` verb is unavailable and
  a rule using it fails compilation. Nothing else in the tier changes:
  the dispatch table is built from the imported service declarations
  ([RARC.9.3.4]) whether or not the tier may invoke a call, and the
  other action verbs dispatch normally.

*see also:* [RARC.9.3.4](#RARC-9-3-4)

(RLOW-10-2-5)=
- [RLOW.10.2.5] **`publish` disabled.** The `publish` verb is
  unavailable; rules using `publish` fail
  compilation.

(RLOW-10-2-6)=
- [RLOW.10.2.6] **`storage_hydrate_*` disabled.** Hydration
  calls do not compile.

*Deferred: not binding in this edition.*

(RLOW-10-2-7)=
- [RLOW.10.2.7] **`storage_hydrate_*` disabled.** Storage hydration
  is unavailable; rules using it fail.

*Deferred: not binding in this edition.*

(RLOW-10-3)=
### Tier identity is not spec-committed in lowering [RLOW.10.3]

(RLOW-10-3-1)=
[RLOW.10.3.1] The compiler's tier-awareness is only through the
dialect. A tier's name doesn't get special
lowering; the dialect associated with that tier
decides which forms are available. A deployment
that assigns the same dialect to two different
tiers gets the same lowering for rules in either.

(RLOW-11)=
## Compile-time validation [RLOW.11]

(RLOW-11-6)=
[RLOW.11.6] **The checks are the language's.** The compiler must refuse,
before it lowers anything, every program the language trees refuse, by
[LERR.1.1.11]'s criterion, and must report each refusal with the code its
clause names. This document adds one refusal of its own, a rule the compiler
cannot lower ([RLOW.1.6]). The rule-graph cycle check is [LCYC.3]'s, reported
as `DS_VALIDATE_RULE_CYCLE` ([LCYC.6.1.1]); a construct the program's settings
forbid is refused per [RLOW.10.1.1].

---

*see also:* [LCYC.3](#LCYC-3), [LCYC.6.1.1](#LCYC-6-1-1), [LERR.1.1.11](#LERR-1-1-11), [RLOW.1.6](#RLOW-1-6), [RLOW.10.1.1](#RLOW-10-1-1)

(RLOW-12)=
## Error reporting [RLOW.12]

(RLOW-12-1)=
### Source-location preservation [RLOW.12.1]

(RLOW-12-1-1)=
[RLOW.12.1.1] Every AST node and every intermediate representation carries
its DS source location, and every emitted beta and terminal node carries it into
the artefact as a `LineSpan` atom — module, line and column ([RDSC.14.3],
[RDSC.14.3.3]). Alpha nodes carry none ([RDSC.14.3.6]).

*see also:* [RDSC.14.3](#RDSC-14-3), [RDSC.14.3.3](#RDSC-14-3-3), [RDSC.14.3.6](#RDSC-14-3-6)

(RLOW-12-2)=
### Compile error shape [RLOW.12.2]

(RLOW-12-2-1)=
[RLOW.12.2.1] A compile diagnostic carries:

(RLOW-12-2-2)=
- [RLOW.12.2.2] Its source location ([RLOW.2.2]), and the end of the
  offending text where the site that raised it knows it.

*see also:* [RLOW.2.2](#RLOW-2-2)

(RLOW-12-2-3)=
- [RLOW.12.2.3] Its code: a `DS_VALIDATE_*` code from the registry
  ([RCLI.5.1]), or a code of the lexer or the parser.

*see also:* [RCLI.5.1](#RCLI-5-1)

(RLOW-12-2-4)=
- [RLOW.12.2.4] A message. The code is stable ([RCLI.5.2]); the wording
  is not, and a tool matches on the code.

*see also:* [RCLI.5.2](#RCLI-5-2)

(RLOW-12-2-5)=
- [RLOW.12.2.5] For a construct the settings forbid, the refactor hint
  [LDIA.5] requires. Any other diagnostic may carry a hint.

*see also:* [LDIA.5](#LDIA-5)

(RLOW-12-2-6)=
- [RLOW.12.2.6] Optionally, related source locations (the first
  declaration of a duplicate, for one) and a candidate resolution, such as
  the closest known name when a name does not resolve.

(RLOW-12-3)=
### Runtime-error mapping [RLOW.12.3]

(RLOW-12-3-1)=
[RLOW.12.3.1] A `ComputationError` on the stream ([RFCS.21.1]) names its
rule and the node that raised it (`proto/engine_error.proto#ComputationError`),
so a tool holding the artefact the stream is bound to resolves it to DS source
through that node's `LineSpan` atom ([RDSC.14.3.1]).

---

*see also:* [RDSC.14.3.1](#RDSC-14-3-1), [RFCS.21.1](#RFCS-21-1)

(RLOW-13)=
## Lowering commitments [RLOW.13]

(RLOW-13-1)=
- [RLOW.13.1] **The Rete network is the lowering target.**
  Schemas → schema groups ([RLOW.3.2.1]); rules → a chain of alpha and beta
  nodes ending in one terminal ([RLOW.4.2.1]); patterns and `where` → alpha
  tests and beta join tests ([RLOW.5]); expressions → `Expr` trees
  ([RDSC.4a.9]); actions → the terminal's actions ([RDSC.4a.8]); series →
  backend atoms ([RLOW.8.1.2]). One engine instance per tier instance
  ([RLOW.1.1]).

*see also:* [RDSC.4a.8](#RDSC-4a-8), [RDSC.4a.9](#RDSC-4a-9), [RLOW.1.1](#RLOW-1-1), [RLOW.3.2.1](#RLOW-3-2-1), [RLOW.4.2.1](#RLOW-4-2-1), [RLOW.5](#RLOW-5), [RLOW.8.1.2](#RLOW-8-1-2)

(RLOW-13-2)=
- [RLOW.13.2] **Terminal ordinal → source position.** The rule's
  index in the emitted terminal sequence is its ordering key;
  no numeric weight is emitted. Restates [RLOW.1.3].

*ratified*  —  *see also:* [RLOW.1.3](#RLOW-1-3)

(RLOW-13-3)=
- [RLOW.13.3] **Spec-committed firing order is enforced by the
  agenda's single deterministic total order**
  ([IRET.11]), not a selectable strategy.
  Lowering contributes only the terminal order ([RLOW.1.3], [LDET.5.4]);
  nothing on a terminal weights it.

*see also:* [IRET.11](#IRET-11), [LDET.5.4](#LDET-5-4), [RLOW.1.3](#RLOW-1-3)

(RLOW-13-4)=
- [RLOW.13.4] **Synthetic names are `$`-prefixed** ([LLEX.10.6.1]), so no
  author name can equal one. Compiler generates deterministic
  synthetic names ([LDET.3.2.6]).
  Restates [RLOW.1.2] and [RLOW.9].

*see also:* [LDET.3.2.6](#LDET-3-2-6), [LLEX.10.6.1](#LLEX-10-6-1), [RLOW.1.2](#RLOW-1-2), [RLOW.9](#RLOW-9)

(RLOW-13-5)=
- [RLOW.13.5] **Path access has two lowerings.** Known-schema
  paths resolve to a leaf-slot VAR node at compile time;
  `Json`-slot paths compile to `FN_JSON_PATH` calls
  ([RDSC.4a.9]); an unresolved path reads as `null` and a
  wrong-typed leaf raises a `TypeError` ([RLOW.6.3.4]). Restates [RLOW.6.3].

*see also:* [RDSC.4a.9](#RDSC-4a-9), [RLOW.6.3](#RLOW-6-3), [RLOW.6.3.4](#RLOW-6-3-4)

(RLOW-13-6)=
- [RLOW.13.6] **String interpolation lowers to one
  interpolation expression** ([RDSC.4a.9]): literal text and
  value sites in source order, each value site carrying its
  format spec. Restates [RLOW.6.4].

*see also:* [RDSC.4a.9](#RDSC-4a-9), [RLOW.6.4](#RLOW-6-4)

(RLOW-13-7)=
- [RLOW.13.7] **Logical emit registers a TMS justifier.**
  `emit X logical` produces a terminal action of kind
  `ACTION_LOGICAL_ASSERT` ([RDSC.4a.8]); the firing activation
  token is the justifier and the fact is retracted when the
  activation ends ([IRET.12]). Restates [RLOW.7.1.3] and
  [RLOW.7.1.4].

*see also:* [IRET.12](#IRET-12), [RDSC.4a.8](#RDSC-4a-8), [RLOW.7.1.3](#RLOW-7-1-3), [RLOW.7.1.4](#RLOW-7-1-4)

(RLOW-13-8)=
- [RLOW.13.8] **Action verbs lower to the terminal's actions**
  ([RDSC.4a.8]). `emit` → `ACTION_ASSERT`, or
  `ACTION_LOGICAL_ASSERT` for a logical emit; `withdraw` →
  `ACTION_WITHDRAW`, or `ACTION_WITHDRAW_BY_ID` when the binding
  is a continuation body's capture of an outer pattern. Every other verb is an `ACTION_CALL` whose
  expression calls the verb's builtin: `publish` → `FN_PUBLISH`,
  `call` → `FN_CALL`, `cache` / `cache_delete` →
  `FN_CACHE_SET` / `FN_CACHE_DELETE`, `log` → `FN_LOG` — routed to the
  dispatcher ([IDSP]) via the
  engine's adapter callbacks. Restates [RLOW.7].

*see also:* [RDSC.4a.8](#RDSC-4a-8), [RLOW.7](#RLOW-7)

(RLOW-13-9)=
- [RLOW.13.9] **Series compiles to backend atoms and aggregation calls.**
  The compiler emits backend atoms ([RLOW.8.1.2]) and lowers every series
  read, a companion slot included, to its aggregation call ([RLOW.8.1.4]). It
  emits **no** rule for sample absorption: that is an engine step running once
  per pump after the agenda drains ([LSER.8.6], [RLOW.8.1.3]).

*see also:* [LSER.8.6](#LSER-8-6), [RLOW.8.1.2](#RLOW-8-1-2), [RLOW.8.1.3](#RLOW-8-1-3), [RLOW.8.1.4](#RLOW-8-1-4)

(RLOW-13-10)=
- [RLOW.13.10] **Hydration skips series via a
  `corr == ""` test** on a hydration target's series-update rule's
  alpha node ([RLOW.8.4.1]).

*Deferred: not binding in this edition.*

*see also:* [RLOW.8.4.1](#RLOW-8-4-1)

(RLOW-13-11)=
- [RLOW.13.11] **Dialect gating drives lowering differences**,
  not tier identity. Lowering is per-dialect; a
  tier's lowering is whatever its dialect's
  form set enables. Restates [RLOW.10.3.1].

*see also:* [RLOW.10.3.1](#RLOW-10-3-1)

(RLOW-13-12)=
- [RLOW.13.12] **Compile-time validation is strict.** Every
  name must resolve, every type must match, every
  form must be gate-enabled, every structural
  invariant must hold. The service set is fixed at compile time and
  carried in the `.dsc`, so nothing arrives later to
  validate against ([RARC.9.4.2]). Restates [RLOW.1.4] and [RLOW.11.6].

*see also:* [RARC.9.4.2](#RARC-9-4-2), [RLOW.1.4](#RLOW-1-4), [RLOW.11.6](#RLOW-11-6)

(RLOW-13-13)=
- [RLOW.13.13] **Source locations survive lowering.** Every beta and
  terminal node the compiler attributes to a pattern or rule carries a
  `LineSpan` atom naming its DS source location, and a `ComputationError`
  names the node that raised it ([RLOW.12.3.1]). Restates [RLOW.1.5].

*see also:* [RLOW.1.5](#RLOW-1-5), [RLOW.12.3.1](#RLOW-12-3-1)

(RLOW-14)=
## Lowering refinements [RLOW.14]

(RLOW-14-3)=
3. [RLOW.14.3] **Compiler-emitted terminal ordering.** The
   firing-priority total order's FIRST key is the
   terminal ordinal ([LDET.5.4]). The compiler must emit
   terminals in a canonical source order the agenda can
   read back, and the encoding is not an implementation
   choice: earlier in its source file first, and across
   files **import-DAG topological, leaf libraries first**,
   so libraries come before the files that import them.

   The oracle rules the compiler synthesizes for tests ([RTIM.14.11]) are
   outside that source order. The compiler **must** emit every oracle rule's
   terminal after every other terminal in the artefact, whichever module's test
   it serves, in ascending `test_id` ([RLOW.17.1]) and then ascending assertion
   position, the `<t>` and `<i>` of its name ([RTIM.14.13]). Every other rule
   then has the same ordinal whether or not the artefact carries tests.

*ratified*  —  *see also:* [LDET.5.4](#LDET-5-4), [RLOW.17.1](#RLOW-17-1), [RTIM.14.11](#RTIM-14-11), [RTIM.14.13](#RTIM-14-13)

(RLOW-14-4)=
4. [RLOW.14.4] **Incremental compilation is an optimisation.** A compiler
   may recompile only the rules that changed, provided the artefact it writes
   is the one a full compile of the same source writes, so it carries the same
   `dsc_sha256` ([RDSC.3.6]). A runtime never compiles: it loads the artefact
   it is given.

*see also:* [RDSC.3.6](#RDSC-3-6)

(RLOW-14-5)=
5. [RLOW.14.5] **Protobuf descriptor stability across
   compiles.** A schema's protobuf field numbers
   must remain stable across compilations to
   preserve wire-encoding compatibility. The
   compiler numbers the fields of a schema's
   generated `.proto` ([RDSC.5.8.3]) from 1, in
   slot declaration order.

*see also:* [RDSC.5.8.3](#RDSC-5-8-3)

(RLOW-14-6)=
6. [RLOW.14.6] **Cross-file source order.** When rules live in
   multiple files, the inter-file ordering is part of the
   firing-priority total order, and the compiler's emission order
   must follow it. [LDET.5.4] fixes what that order is; this clause
   states only that the compiler is bound by it.

*see also:* [LDET.5.4](#LDET-5-4)

(RLOW-14-7)=
7. [RLOW.14.7] **A synthesized terminal takes the position of the
   rule that induced it.** Some terminals have no rule of their own in
   source: the follow-up rule an `after` clause lowers to ([LTAC.3.1]),
   the sibling rules a `call` continuation lowers to ([LCCO.10.1]), and the
   `_answered` and `_failed` rules each `timeout` clause lowers to
   ([LCCO.7.6]). The compiler must emit each such terminal **after the
   terminal of the rule that induced it, and before any later author
   rule's**, in this order:

   - the inducing rule's own terminal;
   - then the follow-up of each of its `after` clauses, in source order;
   - then the sibling of each of its `call` continuations, in source
     order. A `timeout` clause's sibling is followed, after every terminal
     it induces, by that clause's `_answered` rule and then its `_failed`
     rule, and only then by the next continuation's sibling.

   The order applies at every level. Each synthesized terminal is
   followed at once by the terminals it induces in turn, a nested
   `after`'s follow-up ([LTAC.6.2.2]) or the sibling of a continuation in
   its body, ordered the same way, and only then by the next terminal at
   its own level.

   So the emitted sequence is a depth-first walk: each author rule, then
   everything it induced, then the next author rule. A synthesized terminal's ordinal is therefore determined
   by the source that induced it, exactly as an author rule's is, and
   [RLOW.14.3]'s "canonical source order" is total over every terminal
   rather than only over the ones a reader can point at.

*ratified*  —  *see also:* [LCCO.10.1](#LCCO-10-1), [LCCO.7.6](#LCCO-7-6), [LTAC.3.1](#LTAC-3-1), [LTAC.6.2.2](#LTAC-6-2-2), [RLOW.14.3](#RLOW-14-3)

(RLOW-17)=
## `.dstest` lowering [RLOW.17]

(RLOW-17-1)=
[RLOW.17.1] **A `test` declaration lowers to the `Test*` and `Oracle*` atom
families** ([LGRA.21], [LTSL.1]): a `TestCase` per case, `TestFact` and
`TestCacheSeed` for `initial` and `inputs`, `TestResponse` and `TestReply` for
`responses`, and one `Oracle*` atom per `produces` assertion ([RDSC.7.1]).

**A test's number is its place in the artefact.** The compiler **must** number
the tests a compile carries ([LTSL.2.4]) from 1: by module, in ascending
`module_id`, which is import-DAG order, leaves first ([RDSC.4.7]); within a module,
the `.ds` file's tests in source order, then its companion's. A test's number is
its `TestCase.test_id`, so no two tests in one artefact share a `test_id`, and
every `Test*` and `Oracle*` atom of the test carries it.

*see also:* [LGRA.21](#LGRA-21), [LTSL.1](#LTSL-1), [LTSL.2.4](#LTSL-2-4), [RDSC.4.7](#RDSC-4-7), [RDSC.7.1](#RDSC-7-1)

(RLOW-17-2)=
[RLOW.17.2] **Construction and matching lower differently.** A constructed
fact — `TestFact`, `TestReply`, `TestCacheSeed` — carries **literal** slot
values, every author-supplied slot given. An `Oracle*` atom carries a **partial
match**: inline slot-equality constraints or, on `OracleEmit` and
`OraclePublish`, a `where` predicate in their place, flagged by `has_where` and
realized as a compiler-synthesised internal rule ([LTSL.8.4]).

The split mirrors the author surface ([LTSL.4]) rather than inventing one: what
an author builds is total, what an author asserts about is partial.

*see also:* [LTSL.4](#LTSL-4), [LTSL.8.4](#LTSL-8-4)

(RLOW-17-3)=
[RLOW.17.3] **Every test reference binds at compile time.** `OracleActivated`
rule names, every `Oracle*` schema name, and `TestFact` schema names **must**
resolve against the compiled program's rule and schema inventory.

A dangling reference is a compile error — an `activated` of an unknown rule, an
`emitted` of an unknown schema, a slot a schema lacks — reported as
`DS_VALIDATE_TEST_UNKNOWN_RULE`, `DS_VALIDATE_TEST_UNKNOWN_SCHEMA`,
`DS_VALIDATE_TEST_UNKNOWN_SLOT` or `DS_VALIDATE_TEST_NON_LITERAL`.

**This is the property that stops a `.dstest` drifting from the rules it
tests**: they compile together, so a rule renamed without its test is a build
failure rather than a test that silently stops asserting anything.

A `for call … as` correlation is a string literal ([LGRA.21.1]), so it too
is fixed at compile time.

*see also:* [LGRA.21.1](#LGRA-21-1)

