waxwing

ADR 013: Structural records with row-typed tails

Implemented on branch records 2026-10-14 (plan Tasks 1-11) and 2026-10-15 (Task 12, R012); not merged. Binding spec: docs/plans/2026-10-14-records-design.md (approved 2026-10-14); plan docs/plans/2026-10-14-records-plan.md; evidence docs/progress.md (“R001”). Builds on ADR 002 (planned specialization), ADR 005 (structural order), ADR 007 (specialization), ADR 010 (effects and Fail keys), ADR 011 (Text) and ADR 012 (qualified operations, which kept . free on lowercase receivers). Records of operations as services, extension and merge, and C001 instances are not part of this decision. Record patterns (R004, branch record-patterns) are the section “Record patterns (R004)” below, and extension, removal and written lacks (R005 Tranche A, branch record-extension) the section after it.

Context

Waxwing computed only with Int, Bool, Text, Unit, user ADTs and functions. Records are the data form the services scenario, records of operations and C001 instances will build on. Haskell’s records are the model to avoid: generated global selectors that are partial and clash across types, field names that collide between declarations, and awkward update syntax. The user chose PureScript-style records: anonymous, structural, row-typed, with type-safe access and update and functions polymorphic over the rest of a record.

Decision

Surface

Representation

One new type form, TRecord over Domain.Type.Record’s RecordRow a v = { fields, tail }: fields kept sorted by label (recordRow sorts on construction), each with a field type, and an optional tail variable whose image is a record type. The field type is a parameter of the row, which avoids a module cycle with Domain.Type.Core. Record rows and effect rows are separate types and never unify. Diagnostics print a record with sorted labels and its tail written ...r; an unsolved meta tail prints ..._.

Row sorts

A row variable is record sort or effect sort, never both; reusing the ...r spelling does not make them interchangeable. A record tail is a record use, a with row an effect use, and an argument to a declared type’s row parameter takes that parameter’s sort and gives it its own. Features.Resolve.Sorts and RowSettle solve the declarations’ row parameters (controller rulings, Task 3, after four review rounds):

The texts are the approved r is a record row, not an effect row and its mirror. A type written in a body (a lambda’s annotation, a handler’s label arguments, a handle clause’s payload) uses its signature’s row variables with the sorts the signature settled (BACKLOG R016, 2026-10-15, Features.Resolve.BodySorts): reading one row sort as the other is that text at the body’s use, noted first written here at the signature’s first use, the later use being the error as above. Before, an effect row used as a lambda’s record tail read as a is not a field of { a: Int, ...r }, a record row as a lambda’s effect row was accepted, and an effect row passed to a record parameter in a body reached E_INTERNAL (rows r016-effect-as-record, r016-record-as-effect). A row variable read as a type there, or a type variable as a row, is the signature’s own text too, through SortConflict conflict, with no note: Expected a type, found an effect row at the r, Expected an effect row, found a type at the ...a or with ...a (BACKLOG R017; before, accepted, E_INTERNAL or b is not a field of { b: Int, ...a }; rows r017-row-as-type, r017-type-as-row). A record tail naming a type parameter, and ...r naming an effect’s parameter (Features.Resolve.EffectSorts), are the existing E_TYPE Expected an effect row, found a type: an effect’s parameters are types, so effects cannot take record-row parameters in this tranche (Task 4 ruling; BACKLOG R009).

Record-row arguments (R012)

The final review (I2) found that a declared type’s record-row parameter took only a written ...r, although diagnostics print Holder({ y: Bool }) where a solved row fills it. Plan Task 12 added the form; the user approved the surface and both diagnostics on 2026-10-15.

Unification and lacks sets

Features.Check.UnifyRecord follows each side through its tail’s bindings (Features.Check.FlatRecord, a loop) to its fields and a tail that is closed, rigid or an unbound meta, unifies the labels both hold field by field in label order, then decides the tails (Features.Check.RecordTail): the same labels and tails (rule 1); labels on one side extend the other’s meta tail (rule 2); labels on both sides give two distinct meta tails one fresh remainder, which lacks both sides’ labels (rule 3, r001-shared-remainder); anything else (a closed or rigid tail asked to grow, one tail on both sides, an occurs-check cycle) is the records’ mismatch. If unifying the shared fields bound a row’s own tail, as in { a: { ...r }, ...r }, the rows are flattened again and unified anew rather than the binding overwritten (Task 4 review; r001-field-bound-tail).

Every record meta has a lacks set in the substitution (Features.Check.Lacks): binding a tail checks the new fields against it and carries it into the rest (rule 4, r001-lacks). Signatures, constructors and effect operations seed the implied lacks sets at each instantiation, and a function’s own signature seeds them for its body (Features.Check.Seed, Implied), also through declared types’ record parameters. A lacks violation reports the shape mismatch (Expected { ..._ }, found { a: Int }), never a duplicate location invented from an inferred row.

Seeding scope. Those seeds cover signatures, constructors and effect operations, not types written in a body. The final review (C1) found that this hole was not latent: a lambda annotation { a: Int, ...r }, a with handler State({ n: Int, ...r }) label or a Holder(...r) annotation under fn g(p: { ...r }) passed the checker, a caller filled r with a, and lowering declared the field twice (go build failed with f_a redeclared; probes l2, m1, m4, o2), while a field access through such an annotation read as the self-contradictory a is not a field of { a: Int, ...r } (l1). Resolve now holds every type written in a body (a lambda’s parameter annotations, a handler’s label arguments, a handle clause’s payload) to its signature’s lacks sets (Features.Resolve.Lacking, with the same Implied facts the body is seeded with): each label the type puts beside one of the signature’s record row variables, directly or through a declared type’s record parameter, must be one the signature says the variable lacks. Otherwise it is E_TYPE r may already have field a (text approved 2026-10-15) at the written label, or at the ...r passed to the declared type when the label is that type’s, with the note r is introduced here at the signature’s first ...r; the placement at the passed ...r, with its note, was implemented by the fix wave and approved by the user on 2026-10-15 afterwards. As defence in depth, Specialize’s recordType reports a label both written and in the tail’s image as E_INTERNAL Duplicate record label rather than lowering it (regression rows r001-body-lacks, r001-record-guard).

Inference and diagnostics

A literal infers the closed type of its fields; its checked node keeps the written order, its type the sorted one. Access, and each update label, unify the receiver with { l: α, ...ρ }, ρ fresh and lacking l (Features.Check.Record). Since α and ρ are fresh, a failure has two causes only: a record whose tail cannot hold l is age is not a field of { name: Text } at the label, and anything else is Expected a record, found Int at the receiver. An update requires each value to have its field’s type and has the base’s type, so an update never changes a type. A repeated label in a type, literal or update is the existing template Duplicate field <label> at the second label, noted first written here (ruling, Task 3: the spec’s Duplicate field name is that template at the label name). No other new text was added in the tasks; the final review added r may already have field a with its note (text approved 2026-10-15; placement approved afterwards, “Unification and lacks sets”).

Comparison and printability

Records join ADR 005’s order: fields compare in sorted label order, the first difference deciding. A record tail is a variable for comparison groundness (Features.Check.Comparable), so comparing { name: Text, ...r } in generic code is Type { name: Text, ...r } is not comparable. A record is printable when every field is: { age: 36, name: "Ada" }, each field in nested form.

Printability is now also checked at the ground type (BACKLOG E017, Task 2): after specialization, Features.Specialize.Printable checkSites judges each show, Console.print and crash site with groundPrintable ∷ Functional → Ty Void → Boolean (it takes the set of declared types that store a function, so Holder(Int) with type Holder(a) = Holder(a -> a) is refused), giving the existing Expected a printable value, found <t> at the site instead of E_TOOL from go build. Generic printers of open records are accepted and judged at each instantiation.

Records as Fail payloads

A closed record’s head is HeadRecord labels, its sorted label set (Domain.Row), so the labels play the part of a declared type’s name and the field types its arguments: reordered shapes share a family, the same labels with other field types unify field-wise, and different label sets are different families. A payload whose labels are not yet known has a deferred key (Features.Check.FlatRecord failHead); never settled, it is Fail needs a concrete error family. A payload with a row tail written in a signature or a handle clause is refused where written, as Fail(a) is.

Runtime keys (Format.Go.Families): the existing keys are unchanged (Int -1, Bool -2, Unit -3, a declared type -4 minus its id, Text MinInt32), and label sets take MinInt32 + 1 upwards, numbered in the sorted order of the program’s label lists, so the numbering is deterministic and lies between Text’s key and the declared types’. ADR 010’s key list gains this range. A program whose two ranges would meet needs over 2^31 - 4 label sets and declared types, so the refusal is an invariant check, not a user limit: keyed, called by Program.Compile after specialization and before emit (which stays total), reports the existing internal text Fail without a family (E_INTERNAL). Only its boundary, familiesFit, is tested; no program reaches the refusal.

Specialization: record rows enter keys, effect rows stay erased

A record row variable is a specialization parameter, like a type variable. Rows are canonicalised before they enter a key: fields sorted, field types numbered, and each closed shape hash-consed into one layout (Features.Specialize.Layouts, Record), so reordered rows share one function copy. An unsolved record-sorted argument or tail is the empty rest, never the Int representative, so every empty rest reaches one key (Task 8 carry-over fix).

Record rows enter keys while effect rows stay erased (ADR 010) because only a record row decides representation. A function reading p.name from { name: Text, ...r } selects a field of a specific Go struct, whose field set the row fixes; one copy per concrete row makes every access a static Go field selection, with no evidence, maps or boxing. An effect row changes no representation: operations are found at run time by key in the context, so a function’s code is the same for every row, and erasing rows keeps row-growing recursion from creating instances.

Layouts are not specialization keys: they create no work and are not counted against the 10,000 limit, as interned arrows are not; the function copies that reach a new row are counted (test/r001-go-keys.test.mjs pins both). Growth was measured: 200 shapes through 20 generic links give exactly 4,001 keys and 19,617 Go lines, linear in both (scale test), and a services proxy is recorded in docs/progress.md as a proxy, not a validation.

This confirms ADR 002’s records clause as written: unique labels with lacks constraints, normalized label order, and open-row functions specialized to concrete layouts. No accessor evidence was needed, because every function is specialized, and the boxed or dictionary fallback was not chosen.

Go lowering

Record patterns (R004)

Spec: docs/plans/2026-10-16-record-patterns-design.md; plan docs/plans/2026-10-16-record-patterns-plan.md; evidence docs/progress.md (“R004 Task 1” to “Task 4”). Decided by the user on 2026-10-16.

Extension, written lacks and removal (R005 Tranche A)

Spec: docs/plans/2026-10-16-record-extension-design.md §1-§5 (Tranche A approved by the user, 2026-10-16); plan docs/plans/2026-10-16-record-extension-plan.md; evidence docs/progress.md (“R005 Task 1” to “R005 Task 3”). Branch record-extension; not merged.

Consequences and limits

Alternatives considered

Evidence and gates are in docs/progress.md (“R001”). Isolated-copy regression rows (scripts/regression-r001.mjs, probes in test/regression-r001.mjs): r001-print-ground, r001-record-order, r001-row-sort-propagation, r001-duplicate-field, r001-callee-to-caller, r001-settled-sort, r001-caller-conflict, r001-sort-feedback, r001-field-bound-tail, r001-sorted-layout, r001-lacks, r001-shared-remainder, r001-field-order, r001-shape-types, and from the final review r001-body-lacks and r001-record-guard (probes in test/regression-r001-body.mjs), and for R012 r012-record-argument, r012-argument-clash, r012-argument-lacks, r012-slot-position, r012-spread-argument, r012-nested-row and r012-nested-walk (scripts/regression-r012.mjs, probes in test/regression-r012.mjs), and for the follow-ups r015-effect-row, r015-record-row, r016-effect-as-record, r016-record-as-effect, r017-row-as-type, r017-type-as-row, r010-nested-row and r010-mutual-row (scripts/regression-ie.mjs, probes in test/regression-ie.mjs). Tests: test/r001-.test.mjs (test/r001-body-types.test.mjs for C1), the scale test test/r001-scale.serial.test.mjs, test/r012-.test.mjs, and the reference interpreter and generator (test/fx-oracle-records.mjs, fx-gen-records.mjs, fx-gen-record-fns.mjs).