Skip to content

Changing the QVR language

A change to QVR is never only a grammar edit. A change to what .qvr source can say, or to what it means, also changes the grammar's derived assets, every .qvr file in the repository and every fenced qvr block in the documentation, the reference machine's ground truth, every transpile target, and the documentation prose. This page orders that work so that a change lands complete. The grammar asset procedure describes the derived assets and the release hop framework in detail, and the migration guide describes qvr migrate as users run it.

1. Is it a language change?

A change is a language change when it changes any of:

  1. grammars/qvr/grammar.js;
  2. the shape of an AST node in quivers.dsl.ast_nodes;
  3. the meaning of source that parses today, including elaboration, defaults, and name resolution;
  4. the spelling of a generated name that source can mention (G.Item, Σ.Term.normalize);
  5. the set of reserved words.

Every language change ships a contribution to the open release migration hop (§5), pure extensions included. An extension contributes identity probes, released forms that must keep their bytes and their parse tree. The migrator's own gates check parsing only, so a change that silently alters the meaning of released source passes every parse gate; the probes and the hash gate (§6) are what catch it.

A change that touches only the Python API, or adds a standard library module written in existing syntax, is not a language change, and the pull request says that the surface is unchanged.

2. Design commitments

Five commitments constrain every language change. They are what keeps one core and one meaning under many features.

  1. One core. A new feature elaborates into the Quivers Indexed Effect Core (QIEC). Modules, functors, holes, higher-order functions, rows, notations, and generated operations are all interpreted away before code generation (the module calculus), so the core, the reference machine, and every transpile target stay untouched. A change that needs a new core node states why elaboration cannot express it.
  2. Composition by colimit, identity by origin. Anything that combines theories, deductions, lexicons, signatures, or modules combines parts by the declarations that introduce them. Two things never merge because their names coincide; a clash is refused with both sources named.
  3. Checks that reject. A new check is validated by tests that show what it rejects. A check that cannot be decided in general (confluence, termination, a law of an infinite model) is sound and incomplete, and records what it could not prove as assumed, never as passed.
  4. Clean breaks. A replaced surface is removed, and a rewriting migration feature carries released source to the new one. The language keeps one spelling for each construct.
  5. Identity by content. A syntactic rewrite preserves the content hash of every declaration it rewrites. A rewrite that changes meaning says so, with its reason.

3. Design before code

The design is written down, in the pull request or as a draft of the semantics page, before grammar.js is edited. It names:

  • The surface. Every new form, with an example, and every released form whose meaning the change affects.
  • The semantics. The typing and elaboration rules, stated as the pages under Semantics state them. A feature whose rules cannot be stated is not designed yet.
  • The axes of variation. A branch on a particular family, constructor, atom, or term name signals a missed abstraction: the design finds the declared property every member shares and branches on that. A refusal follows from a declared property, and its message names the property.
  • No fallbacks. Where a fallback is tempting, the code raises instead, and the design is revisited. A fallback usually hides a missing case of the grammar or of the abstraction.
  • Reserved words. For each new keyword, the corpus and the fenced doc blocks are parsed with the current grammar to find identifiers spelled like it. Each hit forces a choice, recorded in the migration feature: a contextual keyword, which still lexes as an identifier wherever its construct cannot start (proved by an identity probe that uses the word as a name), or a reserved keyword, with a rewrite that renames released identifiers consistently.
  • The migration. Whether released source keeps its meaning (identity probes only) or must be rewritten, and the exact rewrite. Rewrites that are exact on the syntax and need no type information are preferred: construct c(x) as T becomes (c(x) : T), which keeps the annotation, rather than a bare c(x), which is correct only when inference succeeds.

4. Implementation layers

The work proceeds from the surface inward, with each layer's tests green before the next:

Layer Where
grammar grammars/qvr/grammar.js, with a tree-sitter corpus case in grammars/qvr/test/corpus for every new form
parse and AST src/quivers/dsl/parser/, src/quivers/dsl/ast_nodes/; name positions in quivers.dsl.parser.scopes
module layer src/quivers/dsl/modules/, when the change affects names, imports, or module expressions
elaboration src/quivers/dsl/compiler/, the *_elaboration.py modules, src/quivers/dsl/qiec_lowering.py
checked core, only when unavoidable src/quivers/qiec/ (checker, evaluator, serialization, content hashing)
transpile src/quivers/transpile/
tooling the language server (src/quivers/lsp/), the REPL, the Pygments lexer, the editor grammars
printing quivers.dsl.emit, so that LSP formatting writes every new form back as a fixed point

Two rules hold across the layers.

  • The reference machine defines meaning. A feature works when the reference machine, the compiled PyTorch runtime, and each transpile target agree on it.
  • Targets render or refuse. Each transpile target either renders the feature or refuses it with a diagnostic derived from a declared capability, tested per target. A target that drops or approximates a feature silently is a defect.

A change to the identifier positions of the grammar updates the position tables of quivers.dsl.parser.scopes, which tests/dsl/test_name_positions.py checks against the grammar, so that name resolution, renaming in the language server, and content hashing all see the new binders and references.

5. Grammar assets and the migration feature

After the last edit to grammar.js:

python tools/grammar_assets.py regenerate   # while developing
python tools/grammar_assets.py check        # verify without writing
python tools/grammar_assets.py ritual       # the full sequence, once the grammar is final

regenerate runs tree-sitter generation and the corpus tests, copies the packaged grammar data, regenerates the highlight queries and TextMate vocabularies, refreshes the HEAD migration snapshot and its hashes, and reports the parse-action table against its budget. ritual also rebuilds the HEAD parser library, appends the working grammar to the schema history with grammars/qvr/vcs/build_schemas.py --revision HEAD and pins the new commit in SCHEMA_COMMITS, and repackages the VS Code extension. Use the explicit --revision HEAD form so development updates only the working revision. --reset re-identifies released revisions and breaks their pins. The grammar asset procedure lists each asset, its readers, and the tests that pin it, and explains how to keep the parse table small.

Write the migration as a feature module in the open release hop package of quivers.cli.migrations. During development this package migrates the newest released grammar to HEAD. Release preparation freezes its target at the new grammar revision and connects that revision to HEAD with an identity hop. The module defines one FEATURE:

Field States
vertex_collectors for each released grammar rule the feature rewrites, a function from a vertex of that rule to byte span edits
file_collectors functions that need the whole file
source_rule_coverage the released rules the collectors translate
probes released forms that keep their bytes and their parse tree, or a stated argument when the tree is reshaped
rewrites released forms and the exact bytes they migrate to
cuts released constructs the feature refuses, with the line, the column, and the replacement the refusal names
keywords the words the feature makes keywords, each contextual or reserved with its rename
semantic empty when every rewrite keeps the checked meaning, otherwise the reason it does not

The hop loads every feature module in name order and runs all collectors over one parse of each released file. Edits are span edits, never a re-emission of the file, so comments and layout outside the rewritten tokens survive byte for byte. Two edits from any features that touch the same bytes or insertion point are refused rather than ordered, so the result does not depend on the order of the features; a feature keeps its edits token-local so another feature can edit inside the same construct. A construct with no mechanical rewrite is refused with its location and what to write instead, and is never approximated or dropped. The output must parse at the hop's TARGET_REVISION and must not end a line in whitespace the source did not.

With the feature written, the repository is migrated in place:

qvr migrate --check                                   # every removed rule is covered
qvr migrate --from v<released> --dry-run docs/ tests/ regression.qvr README.md
qvr migrate --from v<released> docs/ tests/ regression.qvr README.md

qvr migrate rewrites .qvr files and the fenced qvr blocks of Markdown files, literate modules included, and lists every compile: false fragment as review PATH:LINE so that each one is read by a person.

6. Tests

The tests of a language change prove what it accepts, what it rejects, and that nothing released changed meaning.

  1. Rejections first. Every new check (coverage, typing, modes, confluence, ambiguity, matching against a signature) has a test that it fails on bad input, asserting that the diagnostic names the culprit.
  2. Differential. A feature that can also be written by hand in existing syntax is compared with that encoding, on the reference machine and on every target that supports both. A generated operation is compared with its host counterpart.
  3. Golden outputs. A restructuring (a module split, a rewriting migration) pins results before and after.
  4. Pinned strength. Tolerances, point counts, and generator sizes are pinned in the test, so that weakening the validation breaks a test.
  5. Migration cases. The feature's own declarations are its migration tests: tests/cli/test_migrate_release_hop.py checks that every probe keeps its bytes and tree, every rewrite migrates exactly, every cut is refused at its location with its replacement, and every keyword is justified. Longer cases go in tests/cli/migration_cases/<release>/<feature>/ as .before and .after pairs.
  6. The hash gate. qvr hash --check-migrations applies every hop, and every feature of the release hop on its own, to the corpus and requires every checked declaration's core hash to be unchanged, unless the feature states a semantic reason.
  7. The corpus gates. tests/cli/test_grammar_gates.py parses every .qvr file with the current grammar and refuses trailing whitespace; tests/test_emit_roundtrip.py requires the printer to be a fixed point; tests/test_doc_blocks.py compiles every fenced qvr block whose marker asks for it; tests/test_gallery_sweep.py (marked slow) compiles and fits the gallery.

No test is skipped because a tool or a container is missing: the tool is installed and the test runs.

7. Documentation

The documentation describes the system as it is, for a reader who wants to understand it. A language change thus rewrites every page that its change makes stale, as a statement about the current system: there are no upgrade notes, no account of a retired surface, and no version annotations. A search of docs/ for every name, behavior, refusal, and cost the change touches finds the pages. The change updates:

  • the language reference, with an example that compiles for each new form;
  • the semantics, with the typing and elaboration rules;
  • the guides and tutorials whose examples or prose the change affects;
  • the QIEC note, when the core changes;
  • CHANGELOG.md, describing the shipped surface only.

Every fenced qvr block compiles under its marker, and a compile: false block states in its marker why it cannot, in terms a reviewer would accept. Every class, method, or function named in inline code links to its API page, and every named mathematical concept links to a definition on its first mention in a page.

8. Done

A language change is complete when:

  1. the design, the semantics, the migration kind, and the keyword decisions are recorded;
  2. every layer is implemented, with the reference machine as ground truth and every transpile target rendering the feature or refusing it with a declared reason;
  3. the grammar assets are regenerated in one pass and their tests are green;
  4. the migration feature is written, the repository and its documentation are migrated, the hash gate holds, and no file gained trailing whitespace;
  5. rejection, differential, and golden tests are in place, with pinned strength;
  6. the reference, the semantics, the guides, and the changelog describe the change;
  7. ruff check, ruff format --check, and pyright src/quivers are clean, and the test suite, including the slow tests, is green.