alpha-2027 · draft-alpha-2027 · tests/language/draft/thenView source

Feature contract: then#

1. Lexical and grammar rules, including precedence and termination#

(then L1 L2 ... Ln Z), n >= 1. Each link is one identifier (a function, a qualified name, or a unary form head such as succ) or a parenthesised prefix (P1 ... Pk) whose tokens begin that form, such as (f a) or (constructor F C x). Z is one term. The form means (L1 (L2 ... (Ln Z))), with a parenthesised link spliced without its parentheses, so each link's last argument is the rest of the chain. Fewer than two operands, an empty link, or a link that is neither a name nor a prefix is ALPHA-PARSE-THEN. then is a reserved head in every edition, as nest is: a function or binder of that name is applied as (app then x), because (then ...) always parses as this form (under alpha-2026 it is then refused by the edition gate). No workspace definition applies the name.

(nest H x y z) is the chain (then (H x) (H y) z): nest is the shorter spelling when every link has one head.

2. Scope and binding rules, including generated names#

None of its own: each link is the term written out, so names resolve in each link as they would in the chain. No names are generated.

3. Type and quantity requirements#

The chain's: every link is checked as the explicit application or form it stands for, so Z must have the type the innermost link's last argument needs (ALPHA-TYPE-MISMATCH otherwise), and quantities are counted over the written-out chain.

4. Effects, totality, and evaluation-order rules#

Those of the chain it abbreviates.

5. Elaboration into existing core or a specifically versioned new core extension#

The parser rebuilds the chain's tokens and parses them (Alpha/Surface/Parser.hs, parseThen), so elaboration sees the explicit chain and the core is byte-identical to it.

6. Representation and target support, where relevant#

Unchanged.

7. Error codes and primary/secondary origins#

ALPHA-PARSE-THEN for a malformed chain. Each rebuilt link spans the whole then form, so a diagnostic inside the chain points at the form and an operand keeps its own span. Under an edition other than alpha-2027 the spelling is ALPHA-EDITION-UNSUPPORTED-FORM, found on the lexemes (Module.draftSyntaxSpans).

8. Formatting and migration rules#

The formatter keeps then and its first link on the first line, and otherwise lays a then out as any form. The registered migration then-right-chains rewrites a maximal right-nested chain of four or more links, of any heads, into one then: depth falls from the chain's length to two, for one more pair of parentheses, which shorter chains do not repay. A form with one identifier before the rest becomes a bare link. Heads with binders or branches (lambda, let, match, eliminate, ...) never link, so such a form is the chain's last term; so is a nest, which nest-right-chains makes first. No comment is removed, because the rewrite only moves parentheses. alpha migrate proves the rewritten module has the same core. bare-nullary-name leaves a link spelled out, because a link is a prefix and not a value.

A link may itself be a nest without its last operand: (nest H x y) is the prefix that rebuilds (nest H x y REST), so a run of one head sits inside a then as one link. The registered migration flatten-chain-tails uses this. After the two chain rewrites, a chain that changes form at its tail is still one level deeper per change: (then a (nest f x y (then b z))), or a nest whose rest is an application whose own rest is a nest, as instruction lists interleave runs of one emitter with labels and branches. The rewrite takes a maximal right chain of units, each a then, a nest with an operand before its rest, or an application (a name head that is no binder, branch or other special form), that holds a then or a nest, and writes it as one then whose links are the units' prefixes: (then a (nest f x y) b z). The last unit stays the chain's last term whole unless it is a then: splitting a final (g u v) into a link (g u) and a last term v would only move a parenthesis into the middle of an application. A chain begins at a form or at an open body whose head is a name. It is rewritten when it has three units, or two whose flat spelling adds no parentheses: depth falls by one per unit after the first. An inner unit's (then and a one-name application's parentheses go; any other moves its ) to the end of its prefix. A comment between an inner unit's ( and its head ends the chain before that unit. alpha migrate proves the rewritten module has the same core; alpha fmt then lays the flat chain out one link per line.

9. Positive, negative, execution, and edge-case tests#

tests/language/draft/then/: name and prefix links, a constructor chain with differing constructors, a nest as the last term, a then inside a link, refl against each written-out chain, a native run (exit 42), and the operand-count, empty-link, literal-link and last-term-type refusals. debug/lang-contract-check.py checks the edition gate both ways with tests/language/ThenUnderAlpha2026.alpha.

10. Compatibility with the old edition, serialized forms, and cache keys#

alpha-2026 refuses the spelling; written-out chains stay valid in both editions. No core, interface or artifact schema changes.

Known discrepancies and limits#

  • A chain nests only through the last argument. A chain through the first argument, (f (f (f base a) b) c), is written with pipe (pipe): (pipe base (f a) (f b) (f c)).
  • The self-hosted front end (packages/compiler) expands then under alpha-2027 (scripts/proofs/alpha-dependent-telescope-parser.sh), with the limits of its nest: a literal link is not refused there, and (then () 1) fails as an empty form. Its own closure is written with then. It expands a form's children before the form, so it first gives each nest, then or pipe link the rest of its chain (Parser.syntaxSpliceThenLinks): without that, (then (nest f x y) z) read as (f x y z) there. The splice runs from the outside in, so a then link that itself holds a nest or then link passes the rest on to it: (then x (then (nest f a b) g) c) is (x (f a (f b (g c)))) in both front ends. The proof script checks a nest link, a then link, a then link holding each, and an open-body then against their written-out chains.