# Rule-local `let` bindings

A narrow rule-scoped `let`
construct for naming intermediate values within a
rule, sited to fill the within-rule
expression-duplication gap that neither
a service call nor `emit X logical` addresses.
The author-facing grammar is
[LGRA]; this doc
specifies the semantics.

Paired with:

- [LGRA] — the
  author-facing grammar.
- [LSCO] — the stance `let` respects by not
  opening the user-defined-function door.
- [LCRS] — the RHS-only
  service-call mechanism `let` complements.
- [LLOG] — the
  cross-rule sharing mechanism `let` does not
  compete with.

(LLET-3)=
## `let` bindings [LLET.3]

(LLET-3-1)=
[LLET.3.1] A rule-local `let` binding is written after `->`, before the
actions. Properties of the construct:

*ratified*

(LLET-3-2)=
- [LLET.3.2] **Rule-scoped.** `let` bindings live
  in the rule block only. No file-scope `let`.
  File-scope would be a user-defined function with a softer name;
  see [LLET.6].

*see also:* [LLET.6](#LLET-6)

(LLET-3-3)=
- [LLET.3.3] **Expression body.** The right-hand
  side is an expression in the same grammar
  `where` clauses already use. No patterns, no
  effects, no calls, no emits.

(LLET-3-4)=
- [LLET.3.4] **Non-parameterised.** A `let`
  binding takes no arguments. This is the
  structural property that prevents it from
  growing into a user-defined function.

(LLET-3-5)=
- [LLET.3.5] **Compile-time inlining.** The Rete
  network sees a pure `let`'s expanded expression at
  every use site. A sync-impure `let` is bound once per
  activation ([LGRA.9.8.4]).

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

(LLET-3-6)=
- [LLET.3.6] **Typed.** Type inferred from the
  right-hand expression, using the existing
  expression type rules. Same machinery that
  checks `where`.

*ratified*

(LLET-3-7)=
- [LLET.3.7] **Visible in all expression positions
  after the `let`.** Later `let`s in the same rule, action-arg
  values, emit/publish slot values, log string
  interpolation.

*ratified*

(LLET-6)=
## Grammatical limits of `let` [LLET.6]

(LLET-6-2)=
- [LLET.6.2] **A name and an expression.** The production
  is `'let' NAME '=' expression`, and a parameter
  list after the name is a parse error.

*ratified*

(LLET-6-3)=
- [LLET.6.3] **Rule-scoped only.** The production
  appears in the rule-block grammar, not at file
  scope. A file-scope `let` would be a user-defined-function shape
  (callable from multiple rules, callable with
  any in-scope binding); refusing it
  grammatically prevents that shape.

*ratified*

(LLET-6-4)=
- [LLET.6.4] **Expression body only.** The
  right-hand side is the existing expression
  grammar. No statements, no blocks, no `call`,
  no `emit`. Cannot express control flow,
  effects, or iteration.

(LLET-7)=
## Grammar [LLET.7]

(LLET-7-1)=
[LLET.7.1] The `let` productions — `let_decl` and `let_binding`, in the RHS
block after `->` — are [LGRA.6.1]'s; this document adds none of its own.

*see also:* [LGRA.6.1](#LGRA-6-1)

(LLET-7-2)=
[LLET.7.2] Type checking reuses the expression
type checker verbatim. The compiler inlines `let`
bindings at every use site during lowering; the
Rete network never sees the binding as a distinct
entity. See [LGRA]
for the author-facing grammar.

(LLET-8)=
## `where` is pattern-attached [LLET.8]

(LLET-8-1)=
[LLET.8.1] **`where` is pattern-attached.** A `where` clause
binds to the pattern it follows and reads that
pattern's bindings and an earlier pattern's ([LGRA.8.6]). A standalone rule-level `where` — one
outside any pattern, able to read `let` bindings — is a
**compile error** (`DS_PARSE_GENERIC`); `let` is not visible to any `where`
([LLET.10.4]).

*ratified*  —  *see also:* [LGRA.8.6](#LGRA-8-6), [LLET.10.4](#LLET-10-4)

(LLET-9)=
## Evaluation order [LLET.9]

(LLET-9-1)=
[LLET.9.1] A rule's body evaluates in this order:

(LLET-9-2)=
- [LLET.9.2] Patterns match (with their inline
  `where` clauses), produce an activation.

(LLET-9-3)=
- [LLET.9.3] The RHS block begins. A sync-impure `let`
  binds first, in source order; a pure `let` is inlined
  at its uses ([LLET.3.5]).

*see also:* [LLET.3.5](#LLET-3-5)

(LLET-9-4)=
- [LLET.9.4] Actions fire in source order, with
  full access to pattern bindings and `let`
  bindings.

(LLET-9-5)=
[LLET.9.5] The `let`-computation step is pure
computation over bindings the pattern-match step
produced — no side effects, no new facts, no
re-matching.

(LLET-9-6)=
[LLET.9.6] A `let`'s expression takes the same disposition it would in any
other position ([LERR.1]) — binding it changes nothing:

- **Absent** (an unresolved `Json` path, a failed fallible conversion): the
  binding is `null` and propagates under Kleene to its use sites
  ([LNUL.4.1], [LNUL.8.4]). The rule cycle continues.
- **A fault** (a wrong-typed leaf, a computation with no answer): the
  activation is abandoned ([LERR.1.4]) and the binding has no use sites,
  because nothing downstream of it runs.

No sentinel is produced in either case.

*see also:* [LERR.1](#LERR-1), [LERR.1.4](#LERR-1-4), [LNUL.4.1](#LNUL-4-1), [LNUL.8.4](#LNUL-8-4)

(LLET-10)=
## Rules around `let` [LLET.10]

(LLET-10-1)=
- [LLET.10.1] **Ordered, no forward references.**
  A `let` can reference earlier `let`s in the
  same rule, pattern bindings from any preceding
  pattern, `const` declarations, and companion
  slots. Cannot reference: later `let`s, later
  patterns, anything in the RHS. Such a reference is an unresolved name
  ([LGRA.22.7]), `DS_VALIDATE_UNKNOWN_BINDING`.

*see also:* [LGRA.22.7](#LGRA-22-7)

(LLET-10-2)=
- [LLET.10.2] **No shadowing.** A `let` name that
  collides with a pattern binding, a `const`, or
  another `let` in the same rule is a compile
  error.

*ratified*

(LLET-10-3)=
- [LLET.10.3] **Inlined, not memoised.** A pure `let`
  binding that references a series aggregation
  (`let z = (t.state - t.mean) / t.stdev` where
  `t.mean` / `t.stdev` are companion slots)
  re-evaluates every time the engine
  re-evaluates any use site. Same cost as
  writing the expression inline. Authors who
  want memoisation promote to `emit` facts or
  use the cache plane.

*ratified*

(LLET-10-4)=
- [LLET.10.4] **RHS-only visibility.** `let` is
  syntactically in the RHS block and semantically
  visible only there: later `let`s' right-hand
  sides, action arguments, emit / publish slot
  values, string interpolation. Not visible in
  pattern `where` clauses (those are in the LHS
  block and match only on pattern bindings) and
  not visible across rules. Syntactic and semantic
  position now match.

(LLET-10-5)=
- [LLET.10.5] **A `let` name is not a fact binding.** `let` binds an
  expression, not a fact, so it has no slots: a slot access on a
  `let` name is a compile error. Bind the fact in a pattern if its
  slots are wanted.

*ratified*

(LLET-10-6)=
- [LLET.10.6] **A hanging-indent group is layout; the ordering rule above is
  unchanged.** Writing `let` once with its remaining bindings indented under it
  ([LGRA.6.7.4]) groups them visually and does nothing else. Within a group the
  bindings are still **ordered with no forward references** ([LLET.10.1]): a
  binding may reference earlier ones in its group and in any earlier group, and
  may not reference later ones.

  **Stated because the form invites the opposite reading.** Haskell's `let`
  group is *mutually recursive* and order-independent, so an author arriving
  from it will expect a group to be a simultaneous binding set. DS's is not, and
  the visual grouping does not make it one. **SML is the closer relative** —
  `let val a = … val b = … in … end` is a sequential block in which each binding
  sees the previous, which is what DS already had and what this spells more
  compactly. OCaml's multi-binding `let` is likewise sequential by default, with
  `and` as the marked simultaneous form. Sequential is the norm; Haskell is the
  outlier.

*ratified*  —  *see also:* [LGRA.6.7.4](#LGRA-6-7-4), [LLET.10.1](#LLET-10-1)

(LLET-12)=
## `let` commitments [LLET.12]

(LLET-12-1)=
- [LLET.12.1] **Placement: after `->`, before
  actions.** The grammar admits no other
  placement — not before `->`, not interleaved
  with patterns, and not in a preamble block.
  One placement keeps a rule readable.

*ratified*

(LLET-12-2)=
- [LLET.12.2] **`where` stays pattern-attached.**
  Attaching the
  test to its pattern is what lets the Rete
  discriminate early; see [LLET.8].

*ratified*  —  *see also:* [LLET.8](#LLET-8)

(LLET-12-3)=
- [LLET.12.3] **Ordered, no forward references.**
  A `let` references only earlier `let`s,
  pattern bindings, `const`s, and companion
  slots.
  Restates [LLET.10.1].

*ratified*  —  *see also:* [LLET.10.1](#LLET-10-1)

(LLET-12-4)=
- [LLET.12.4] **No shadowing.** Collision with
  any in-scope name is a compile error.

*ratified*

(LLET-12-5)=
- [LLET.12.5] **A pure `let` is inlined at compile time.**
  Not memoised ([LLET.3.5]).

*ratified*  —  *see also:* [LLET.3.5](#LLET-3-5)

(LLET-12-6)=
- [LLET.12.6] **Rule-scoped only.** No file-scope
  `let` (which would be a user-defined-function shape).
  Restates [LLET.3.2] and [LLET.6.3].

*ratified*  —  *see also:* [LLET.3.2](#LLET-3-2), [LLET.6.3](#LLET-6-3)

