# Dialects and the `use` directive

Dialects, from the language side: what a
dialect is, what the `use` directive does, how it makes a
file a program rather than a library, how a program's
settings resolve at compile time, the built-in dialects,
and what the compile-error contract looks like when a
construct is not permitted. How integrators define their
own dialects lives at [NDLC].

Paired with:

- [LGRA] — `use` is a class-1
  hard keyword; grammar production lives there.
- [LCAP]
  — the capability registry every setting names.
- [LFCA] — the
  capabilities that gate surface constructs, with
  their refactor hints.
- [LLSU] —
  feature inventory, including which features are
  gateable and which are always-on.
- [NDLC]
  — the compiler-CLI integrator path: YAML dialect files
  loaded via `dsc compile -d`.

## What this is not

- Not a dialect-definition spec. How an integrator
  defines a dialect is [NDLC].
- Not the capability registry; see [LCAP].

---

(LDIA-1)=
## What a dialect is [LDIA.1]

(LDIA-1-1)=
[LDIA.1.1] A **dialect** is a name for a setting of every capability in
the registry of [LCAP.1] to `true` or `false`. A dialect is not itself a
capability. Dialect names and capability names are separate namespaces, so a
dialect may share a capability's name, as the built-in `realtime` dialect
does ([LDIA.9.3]).

*checks:* `alloy×2`  —  *see also:* [LCAP.1](#LCAP-1), [LDIA.9.3](#LDIA-9-3)

(LDIA-1-2)=
[LDIA.1.2] The key property: **the BNF does not change between
dialects.** Every DS file parses under the same
grammar.

*ratified*

(LDIA-1-3)=
[LDIA.1.3] A program's **resolved settings** are its dialect's settings
with its `use` block's overrides applied ([LDIA.3.3]). They decide which
constructs the program may use and how the engine carries them out. They
must not change what the program computes ([LDET.1.12]).

The compiler validates every construct in every module of the compilation
unit against the resolved settings. A construct whose capability is `false`
is a compile error at the construct's source location
(`DS_VALIDATE_CAPABILITY_DISABLED`, [LDIA.3.4.2]).

*checks:* `alloy×4`  —  *see also:* [LDET.1.12](#LDET-1-12), [LDIA.3.3](#LDIA-3-3), [LDIA.3.4.2](#LDIA-3-4-2)

(LDIA-1-4)=
[LDIA.1.4] A tier integrator cannot invent a different DS. The
dialect is a capability filter layered on top of a
single language.

---

(LDIA-2)=
## The `use` directive [LDIA.2]

(LDIA-2-1)=
[LDIA.2.1] The `use` directive's grammar is `use_decl` ([LGRA.3.1]). Its
two forms mean:

- **`use <dialect>`**: the dialect's settings, with nothing overridden;
- **`use <dialect>:`** followed by an indented block of
  `<capability>: true` or `<capability>: false` lines: the dialect's
  settings, each line overriding the one capability it names.

Only overrides need to be written. A line restating a setting the dialect
already has is permitted and changes nothing.

*ratified*  —  *see also:* [LGRA.3.1](#LGRA-3-1)

(LDIA-2-2)=
[LDIA.2.2] The first token after `use` is the **dialect name**. The
compiler resolves it against the dialects it knows at compile time: the
built-in dialects of [LDIA.9], and the dialect definitions the integrator
supplies ([NDLC.2.1]).

*ratified*  —  *see also:* [LDIA.9](#LDIA-9), [NDLC.2.1](#NDLC-2-1)

(LDIA-2-3)=
[LDIA.2.3] Rules:

(LDIA-2-6)=
- [LDIA.2.6] **At most one.** Multiple `use` directives in one
  file is a compile error.

*ratified*

(LDIA-2-7)=
- [LDIA.2.7] **First.** If present, `use` appears before any
  declaration. Any `const`, `schema`, `rule`, or
  other top-level form before `use` is a compile
  error.

(LDIA-2-9)=
- [LDIA.2.9] **Program-scoped.** A program's resolved settings govern its
  whole compilation unit: the artefact records them ([RDSC.16.2]), the
  engine runs them, and the compiler validates every module against them
  ([LDIA.2.10.9]).

*see also:* [LDIA.2.10.9](#LDIA-2-10-9), [RDSC.16.2](#RDSC-16-2)

(LDIA-2-10)=
### Programs, libraries and tests [LDIA.2.10]

(LDIA-2-10-1)=
[LDIA.2.10.1] A DS source file with a `use` directive is a **program**. A
DS source file without one is a **library** ([LIMP.1.3]). A program must
have a `use` directive and a library must not: the directive is what makes
a file one or the other.

*see also:* [LIMP.1.3](#LIMP-1-3)

(LDIA-2-10-6)=
[LDIA.2.10.6] A `.dstest` file must not have a `use` directive. The
compiler rejects one with `DS_VALIDATE_USE_IN_TEST`. A test has meaning only
in combination with the program it tests, and runs under that program's
resolved settings ([LTSL.2.2]).

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

(LDIA-2-10-7)=
[LDIA.2.10.7] A program imports libraries only. An `import` that resolves
to a file with a `use` directive is a compile error at the `import` line
(`DS_VALIDATE_IMPORT_OF_PROGRAM`), naming the imported file.

*ratified*

(LDIA-2-10-8)=
[LDIA.2.10.8] Given a library as the file to build, the compiler must
refuse with `DS_VALIDATE_NOT_A_PROGRAM` and produce no artefact: a library
has no settings to be built under. A library is compiled only as part of a
program that imports it.

*ratified*

(LDIA-2-10-9)=
[LDIA.2.10.9] **A library's requirements are computed from its content, not
declared.** The compiler validates each imported library's constructs
against the importing program's resolved settings, exactly as it validates
the program's own ([LDIA.3.4.1]). A `DS_VALIDATE_CAPABILITY_DISABLED` raised
inside a library names the library, the construct and its location, and
the setting that forbids it.

A library whose author means it to work under particular settings shows it
with a test program that uses those settings and passes its tests.

*ratified*  —  *see also:* [LDIA.3.4.1](#LDIA-3-4-1)

(LDIA-3)=
## Compile-time resolution [LDIA.3]

(LDIA-3-1)=
[LDIA.3.1] When the compiler parses a program's `use` directive, it
performs four checks, in order.

(LDIA-3-2)=
### 1. Dialect-name resolution [LDIA.3.2]

(LDIA-3-2-1)=
[LDIA.3.2.1] The compiler looks up `<name>` among the dialects it knows
([LDIA.2.2]).

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

(LDIA-3-2-2)=
- [LDIA.3.2.2] **Unknown dialect name** → compile error. The
  message names the dialect and lists the
  registered dialects (up to a reasonable limit;
  the runtime may truncate).

*ratified*

(LDIA-3-2-3)=
- [LDIA.3.2.3] **The `realtime` dialect** → compile error
  (`DS_VALIDATE_REALTIME_DEFERRED`), at the dialect name. The dialect is
  deferred with the capability it is named for ([LCAP.2.1.5]): the compiler
  knows the name and refuses it, whatever the `use` block overrides.

*ratified*  —  *see also:* [LCAP.2.1.5](#LCAP-2-1-5)

(LDIA-3-3)=
### 2. Override resolution [LDIA.3.3]

(LDIA-3-3-1)=
[LDIA.3.3.1] Each line of the `use` block replaces the dialect's setting
for the capability it names. The author's block always overrules the
dialect.

*checks:* `alloy×2`

(LDIA-3-3-2)=
- [LDIA.3.3.2] **Unknown capability** → compile error
  (`DS_VALIDATE_UNKNOWN_CAPABILITY`). The message names the capability and
  the registry that lacks it ([LCAP.1]).

*ratified*  —  *see also:* [LCAP.1](#LCAP-1)

(LDIA-3-3-6)=
- [LDIA.3.3.6] **A capability named twice** in one `use` block → compile
  error (`DS_VALIDATE_CAPABILITY_DUPLICATE`), naming the capability.

*ratified*

(LDIA-3-3-7)=
- [LDIA.3.3.7] **A deferred capability set `true`** → compile error, at the
  `use` block line: `DS_VALIDATE_REALTIME_DEFERRED` for `realtime` or
  `constspace` ([LCAP.2.1.5]), and `DS_VALIDATE_CAPABILITY_DEFERRED` for a
  storage or samples-plane capability ([LCAP.2.10]). A line setting any of
  them `false` is permitted. The compiler makes this check before the
  constraint check ([LDIA.3.5]), so a deferred setting is reported as
  deferred whatever else the block sets.

*ratified*  —  *see also:* [LCAP.2.1.5](#LCAP-2-1-5), [LCAP.2.10](#LCAP-2-10), [LDIA.3.5](#LDIA-3-5)

(LDIA-3-3-5)=
[LDIA.3.3.5] [LCAP.1] owns the capability registry. A dialect definition
and a `use` block name capabilities from it and from nowhere else.

*see also:* [LCAP.1](#LCAP-1)

(LDIA-3-5)=
### 3. Constraint check [LDIA.3.5]

(LDIA-3-5-1)=
[LDIA.3.5.1] The resolved settings must satisfy every constraint of
[LCAP.3.8]. A violation is a compile error
(`DS_VALIDATE_CAPABILITY_CONSTRAINT`) naming the constraint and the settings
involved. The compiler must not switch any setting on or off to satisfy a
constraint: stating a combination that holds is the author's
responsibility.

*ratified*  —  *checks:* `alloy×2`  —  *see also:* [LCAP.3.8](#LCAP-3-8)

(LDIA-3-4)=
### 4. Per-construct validation [LDIA.3.4]

(LDIA-3-4-1)=
[LDIA.3.4.1] Every construct in every module of the compilation unit, the
program's own and each imported library's, is validated against the
program's resolved settings. A construct its settings do not permit is a
compile error at the construct's source location.

*ratified*

(LDIA-3-4-2)=
- [LDIA.3.4.2] **Construct not permitted by the resolved settings** →
  compile error (`DS_VALIDATE_CAPABILITY_DISABLED`), carrying the message
  contents of [LDIA.5].

---

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

(LDIA-4)=
## Capabilities [LDIA.4]

(LDIA-4-1)=
[LDIA.4.1] Dialects and `use` blocks name capabilities by their stable
names from [LCAP.1], which owns the membership and the count. This clause
states neither, because a second copy is what drifts. A dialect definition
that names a capability outside the registry, or leaves one unset, is
rejected when the definition is loaded ([NDLC.2.2]).

*see also:* [LCAP.1](#LCAP-1), [NDLC.2.2](#NDLC-2-2)

(LDIA-4-2)=
[LDIA.4.2] Everything in the language that no capability governs is
available to every program, under every setting. This includes the full
expression grammar, pattern matching (including `no`, `within`,
`then`/`latest`), schema declarations (including `tolerance`), `let`
bindings, rule-header `cooldown`, intra-tier
`emit`, `log`, `cached(...)` reads, adapter entity matching and string
interpolation.

---

*ratified*

(LDIA-5)=
## Error-message contract [LDIA.5]

(LDIA-5-1)=
[LDIA.5.1] A compile error for a construct the resolved settings do not
permit must identify:

(LDIA-5-2)=
- [LDIA.5.2] The offending construct, and the capability that forbids it
  by its stable name ([LCAP.1]).

*see also:* [LCAP.1](#LCAP-1)

(LDIA-5-3)=
- [LDIA.5.3] Where the capability's setting came from: the program's
  dialect, or the `use` block line that overrode it.

(LDIA-5-4)=
- [LDIA.5.4] The capability's refactor hint, where it has one ([LFCA]):
  one sentence advising what the author can do instead.

(LDIA-5-5)=
- [LDIA.5.5] The source location of the offending construct.

(LDIA-5-8)=
- [LDIA.5.8] For a construct inside an imported library, the library
  ([LDIA.2.10.9]).

*see also:* [LDIA.2.10.9](#LDIA-2-10-9)

(LDIA-5-7)=
[LDIA.5.7] The reference compiler's exact layout is [NDLC.5.3].

---

*see also:* [NDLC.5.3](#NDLC-5-3)

(LDIA-6)=
## Language scope vs runtime scope [LDIA.6]

(LDIA-6-1)=
### The language commits to [LDIA.6.1]

(LDIA-6-1-1)=
- [LDIA.6.1.1] The `use` directive exists, has the grammar of
  [LGRA.3.1], and makes a file a program ([LDIA.2.10.1]).

*see also:* [LDIA.2.10.1](#LDIA-2-10-1), [LGRA.3.1](#LGRA-3-1)

(LDIA-6-1-2)=
- [LDIA.6.1.2] A capability registry exists at [LCAP.1], naming every
  capability by stable name.

*see also:* [LCAP.1](#LCAP-1)

(LDIA-6-1-3)=
- [LDIA.6.1.3] Compile-time validation of every construct against the
  program's resolved settings is required.

(LDIA-6-1-4)=
- [LDIA.6.1.4] Three dialect names are committed: `base`, `default` and
  `realtime`, with the settings of [LDIA.9]. Every other dialect is an
  integrator's.

*ratified*  —  *see also:* [LDIA.9](#LDIA-9)

(LDIA-6-1-5)=
- [LDIA.6.1.5] A `use` block overrides the dialect's settings one
  capability at a time ([LDIA.3.3.1]), and the result must satisfy the
  constraints of [LCAP.3.8] ([LDIA.3.5.1]).

*see also:* [LCAP.3.8](#LCAP-3-8), [LDIA.3.3.1](#LDIA-3-3-1), [LDIA.3.5.1](#LDIA-3-5-1)

(LDIA-6-1-6)=
- [LDIA.6.1.6] Error messages follow the contract in
  [LDIA.5] above.

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

(LDIA-6-2)=
### The runtime owns [LDIA.6.2]

(LDIA-6-2-1)=
- [LDIA.6.2.1] **Dialect registration.** Where dialects are
  defined, how they're loaded at runtime startup,
  what format their declarations take.

(LDIA-6-2-3)=
- [LDIA.6.2.3] **Integrator-registered dialects.** How
  integrators declare additional dialects for their
  platform.

(LDIA-6-2-6)=
- [LDIA.6.2.6] **Runtime-side error-string format.** The
  language commits to the error contract; the
  runtime picks the exact strings.

(LDIA-6-2-7)=
- [LDIA.6.2.7] **Settings introspection.** How debuggers and test
  harnesses query a loaded artefact's resolved settings.

(LDIA-6-2-8)=
- [LDIA.6.2.8] **Which capabilities a build has, and which settings an
  integration admits.** A runtime may be compiled without the code for a
  capability, and an integration may admit only some settings. Both are
  checked when an artefact is loaded, not when it is compiled
  ([NLFC.10]).

---

*see also:* [NLFC.10](#NLFC-10)

(LDIA-9)=
## Built-in dialects [LDIA.9]

Every conforming compiler knows these three dialects. The capabilities are
those of [LCAP.1]. `base` and `default` have exactly the settings below;
`realtime` is deferred with its capability, and the compiler refuses it by
name ([LDIA.3.2.3]).

*see also:* [LCAP.1](#LCAP-1), [LDIA.3.2.3](#LDIA-3-2-3)

(LDIA-9-1)=
[LDIA.9.1] **`base`** sets every capability `false`. A program starting
from `base` turns on, in its `use` block, exactly what it uses.

*checks:* `alloy`

(LDIA-9-2)=
[LDIA.9.2] **`default`** sets each capability as the table states:

| Setting | Capabilities |
|---|---|
| `true` | `call`, `publish`, `cache`, `cache_delete`, `nocooldown`, `regex`, `series`, `logical`, `linear_scan`, `float` |
| `false` | `realtime`, `constspace`, `dynamic_ttl`, `sampled_slots`, `persist_on_series`, `storage_hydrate_facts`, `storage_hydrate_samples` |

The deferred capabilities are `false` here as everywhere in this edition
([LCAP.2.1.5], [LCAP.2.10]).

*ratified*  —  *checks:* `alloy`  —  *see also:* [LCAP.2.1.5](#LCAP-2-1-5), [LCAP.2.10](#LCAP-2-10)

(LDIA-9-3)=
[LDIA.9.3] **`realtime`** has `default`'s settings with `realtime: true`
and `constspace: true`. It leaves `linear_scan: true`: a hard real-time
program may join by scan, because under `constspace` every scan is bounded
by a stated capacity ([LCAP.2.1]). A program that also wants each join
constant in the number of facts writes `linear_scan: false` in its `use`
block.

*Deferred: not binding in this edition.*

*checks:* `alloy×2`  —  *see also:* [LCAP.2.1](#LCAP-2-1)

