A Grammar of Bach
A Lambek type system induced from four hundred and thirteen chorale harmonizations by constraint satisfaction.
I.Premise
A Lambek grammar is a system for assigning types to words in such a way that the types of a sentence's words, concatenated and reduced according to a small calculus, yield a single distinguished type — by convention, the type of a complete sentence. The grammar is what licenses sentences as sentences. An inducer's job is to discover, given a corpus of acceptable sentences and a fixed inventory of primitive types, type assignments that make the corpus parse.
Bach's chorales provide an unusually clean corpus for an inducer that does not natively know about music. The four-voice texture is settled, the cadential vocabulary is constrained, and the corpus is bounded: four hundred and thirteen harmonizations are available in the music21 library, partitioned into 2,514 melodic phrases by rests of one beat or longer and by fermatas. Treating the soprano's scale-degree sequence as the surface yield, and a phrase as a sentence, makes Bach a corpus and Lambek a tool.
The premise is then literal: induce a type system over scale degrees that licenses every observed phrase as parse-complete, and read the resulting type assignments to see what they say.
II.Method
The inducer wraps Google's CP-SAT constraint solver. Four primitive types are supplied by hand: M, the type of a complete melodic phrase, and three arrival points — At1, At3, At5 — encoding stability at tonic, mediant, and dominant respectively. The maximum derived-type depth is two; the solver runs with a one-hundred-and-twenty-second timeout and a memory cap of one and a half gigabytes. Phrase length is filtered to the range of three to ten notes, leaving 2,196 phrases over which the solver must succeed.
Each scale degree may be assigned a set of types of depth at most two. A phrase parses if some choice of types for its scale-degree tokens reduces, by application of the Lambek calculus, to M. The inducer then searches for an assignment under which every phrase in the filtered corpus parses. The constraint problem at full corpus scale does not fit in memory, so the solver attacks it one phrase at a time, batched, with types accumulating across batches rather than being jointly optimized.
The filtered corpus is diatonic: scale degrees one through seven, with no chromatic alterations surviving in the phrases the solver sees. The lexicon contains seven words and seven only.
III.The Lexicon
The corpus's first arithmetic facts are themselves illuminating, and bear stating before any type is induced. Bach's soprano lines are not uniformly distributed across the seven scale degrees, and the shape of that distribution is part of the structure the inducer must accommodate.
Mi is the most common surface token; fa, despite its weight in cadential theory, is comparatively scarce; la and ti are the rare two. The inducer's constraint problem is, in part, to find a small set of types per scale degree that copes with this skew without any one degree carrying the entire grammar.
Most categorial inducers work with several thousand types over several thousand lexical items. Here the inducer must squeeze every observed phrase through a vocabulary of seven, and the structure has to come not from dictionary growth but from polysemy: a single scale degree, at different points in different phrases, takes different types from a small finite set. That is the project's actual question. How polysemic is each scale degree, and how is its polysemy organized?
IV.The Induced Grammar
The solver returns fifty-three type assignments across the seven scale degrees, drawing on twenty-nine distinct types. The per-degree distribution is reproduced in full below.
| Degree | Induced types | N |
|---|---|---|
| 1 (do) | At1/At1, At1\M, At3\M, At5/At5, M/At3, M\At1, At3, At5, M | 9 |
| 2 (re) | At1/At5, At1/M, At1\At3, At3\At5, At5\At1, At5\M, M\At1, At5, M | 9 |
| 3 (mi) | At1\M, At5\At3, M/At1, M/At3, M/At5, M\At1, At1 | 7 |
| 4 (fa) | At1\At5, At3/At1, At3\At3, At5/At3, At5\M, At5 | 6 |
| 5 (sol) | At1\M, At3\M, At5/At1, At5\At1, M/At5, M\At5, M\M, At1, M | 9 |
| 6 (la) | At1\At1, At3\At1, At5\At3, M/At5, At1 | 5 |
| 7 (ti) | At1/At3, At1\At1, At3\M, At5\At1, M/At3, M/At5, At1, At3 | 8 |
Each degree carries between five and nine type assignments — a tight band, given that the problem permitted depth-two types and any subset thereof. The solver did not require depth-two constructions of the form (At5\At3)/At3 that hand-crafted music-theoretic grammars sometimes invoke for passing-tone behavior; depth-one assignments sufficed for the filtered corpus.
V.Functional Clusters
The first and most musically resonant pattern in the induced grammar is that the seven scale degrees partition into functional clusters according to which primitive types they can produce. The clusters do not coincide with the surface identity of the degrees; they cut across the scale.
Three of the four clusters cohere with elementary tonal theory once one reads past the surface. Tonic-arrival degrees — those carrying At1 — are 3, 5, 6, and 7: the degrees from which a melodic line typically resolves to the tonic. Dominant-arrival degrees are 1, 2, and 4: the degrees that approach or stabilize at the dominant. Mediant-arrival is held by 1 and 7 alone. And the phrase-ending cluster — the degrees that can carry M outright, completing a phrase without further support — is just 1, 2, and 5.
Two findings of this section deserve to be lifted out of the table.
Fa, alone among the seven degrees, cannot produce M. There is no induced type assignment in any returned solution that licenses a phrase whose terminal lexical item is the subdominant in isolation. Theorists describe fa as inherently unstable, tendentious in its motion toward 3 or 5. The inducer, given no music theory beyond the four primitive labels and the locations of phrase boundaries, takes this tendency as a hard constraint and finds none. The corpus enforces it.
Do — the tonic — does not carry At1. The tonic pitch, in the induced grammar, is not a destination for tonic stability; it is a distributor, producing At3, At5, and M, but never At1. Tonic stability arrives at non-tonic pitches. This is initially counterintuitive and turns out, on reflection, to express something long observed about Bach's chorale soprano lines: do appears more often as a passing or neighbor tone than as a point of arrival, and the work of cadential resolution is done not by the tonic landing on itself but by the leading-tone or the mediant resolving to it. The grammar locates tonic-arrival in the approach, not in the destination.
VI.Type Connectivity and Asymmetry
Beyond the partition into functional clusters, two finer structural observations emerge from reading the type assignments as a directed system.
Connectivity gaps
Each scale degree, through its types, can reach some subset of the four primitives. The connectivity matrix shows only two gaps in an otherwise saturated table. Degree 3, the mediant pitch, does not produce At3; it reaches At1 and M instead, mirroring at a higher granularity the same inversion observed for do at the tonic. Degree 4, the subdominant, cannot produce M, as already noted. Every other (degree, primitive) pair is realized.
Directional bias
Lambek types divide into right-seeking forms (X/Y, which seek an argument to their right) and left-seeking forms (X\Y). The ratio of right-seekers to left-seekers within each degree's type set is itself informative.
| Degree | Right-seeking | Left-seeking | Ratio |
|---|---|---|---|
| 1 | 3 | 3 | 1.00 |
| 2 | 2 | 5 | 0.40 |
| 3 | 3 | 3 | 1.00 |
| 4 | 2 | 3 | 0.67 |
| 5 | 2 | 5 | 0.40 |
| 6 | 1 | 3 | 0.33 |
| 7 | 3 | 3 | 1.00 |
Degrees 2, 5, and 6 show a marked left-seeking bias. These are precisely the degrees whose canonical melodic roles involve resolving from material that preceded them — 2 → 1, 5 → 1, 6 → 5 — and the induced grammar registers this asymmetry directly in the shapes of the types it assigns. The supertonic, the dominant, and the submediant want something behind them.
VII.Voice-Leading as Shared Types
The most striking single finding of the experiment comes from asking which types appear on more than one degree. Thirteen of the twenty-nine unique types are shared, and the patterns of sharing recover, without any music-theoretic input, several of the classical voice-leading motifs of the chorale literature.
| Type | Degrees | Pattern |
|---|---|---|
| At5\At1 | 2, 5, 7 | dominant-to-tonic resolution |
| At1 | 3, 5, 6, 7 | tonic arrival from non-tonic |
| M/At5 | 3, 5, 6, 7 | phrase seeking dominant completion |
| At1\M | 1, 3, 5 | post-tonic phrase continuation |
| M\At1 | 1, 2, 3 | phrase completion from tonic |
| M/At3 | 1, 3, 7 | phrase seeking mediant |
| At3\M | 1, 5, 7 | post-mediant continuation |
| M | 1, 2, 5 | phrase completion |
| At5 | 1, 2, 4 | dominant stability |
| At5\At3 | 3, 6 | dominant-to-mediant descent |
| At5\M | 2, 4 | post-dominant continuation |
| At1\At1 | 6, 7 | neighbor motion around tonic |
| At3 | 1, 7 | mediant stability |
The first row is the most consequential. The type At5\At1 — a function that takes a dominant-stable argument to its left and returns tonic-stability — appears on degrees 2, 5, and 7. These are the canonical resolving tones of the chorale tradition: the supertonic falling to the tonic, the dominant falling to the tonic, and the leading-tone rising to the tonic. The induced grammar has rediscovered the V–I cadential family as a single type, distributed across three scale degrees, purely from distributional evidence.
Similar observations follow for the other shared types. At5\At3 on degrees 3 and 6 captures the 5–4–3 melodic descent line. At1\At1 on 6 and 7 captures neighbor figures around the tonic — both the upper neighbor from ti and the lower neighbor from la. M/At5 shared across four degrees describes the broader phenomenon of phrases that orient themselves toward dominant arrival before completion. None of this was given to the solver. It was forced out by the corpus.
VIII.Degree Portraits
A brief reading of each degree, in the language of the induced grammar:
Do (1) — the hub. Reaches all four primitives. Balanced direction. Carries At3, At5, M, but not At1: a distributor, not a destination.
Re (2) — pre-dominant. Heavy left-seeking bias. Dominant-oriented through At5, At5\At1, At5\M. Can also end phrases. Bridges the I–V axis via At1/At5.
Mi (3) — the M-richest degree. Four M-producing types, more than any other. A tonic arrival point through At1. Cannot produce At3 despite being the mediant pitch.
Fa (4) — the dependent. The only degree without any M-producing type. Strongly At5-oriented. Confirms the music-theoretic description of the subdominant as inherently unstable, requiring resolution.
Sol (5) — the most versatile degree. Nine types, reaching all four primitives. The only degree carrying M\M — phrase continuation in the strong sense. Bridges I–V bidirectionally. Can end phrases. If do is the hub, sol is the connector.
La (6) — the most constrained degree. Five types only. Tonic-oriented through At1 and At1\At1. M/At5 seeks dominant completion. Limited connectivity suggests a dependent function.
Ti (7) — the resolver. Tonic resolution through At1 and At5\At1. Also mediant-oriented through At3 and M/At3. At1\At1 encodes its role as upper neighbor to do.
IX.Caveats
The experiment as run carries four limitations that bear on how its results should be received.
Batched processing. The corpus did not fit in memory under joint optimization. Phrases were therefore processed singly, and types accumulated across batches rather than being co-optimized against the corpus as a whole. A joint solution, were it computationally feasible, could differ — possibly by tightening the induced grammar, possibly by changing its shape in ways that the batched approach cannot anticipate.
No negative examples. The solver was given a corpus of well-formed phrases and asked to license all of them. It was not given non-phrases to reject. Without negative pressure the grammar may overgenerate, admitting sequences Bach would not have written. The recovery of voice-leading patterns from positive evidence alone is suggestive but not conclusive evidence that the grammar models Bach's constraints rather than Bach's habits.
Diatonic only. The filtered corpus contains no chromatic alterations — they are simply absent from the phrases the solver sees. The genuinely chromatic structure of Bach's harmonic language is therefore not in the surface lexicon. Whether re-running on an explicitly chromatic alphabet (raised fa, lowered ti, applied dominants) would yield a recognizably enriched grammar, or would simply scale up the type inventory without structural change, is an open question.
Depth-one solutions only. The solver was permitted derived types up to depth two, but every returned solution was depth one. This may reflect either the genuine character of the soprano line — that depth-one types suffice — or an artifact of the batched search settling on the simplest sufficient grammar at every step. Hand-crafted music-theoretic accounts often invoke depth-two types for passing-tone behavior; the question of whether negative examples would force the solver to those depths remains untested.
X.Directions
Five extensions present themselves naturally.
Other voices. The same procedure applied to the bass, alto, and tenor lines of the chorales would produce four grammars, one per voice. The prediction is that the bass would emerge richer in structural At1/At5 types — given its role as the harmonic foundation — while the inner voices would show different polysemy patterns from the soprano.
Other composers. Comparable corpora exist for Palestrina (sacred polyphony with different cadential conventions), Mozart (a later harmonic language), and the chant repertoires (modal rather than tonal). Differences in induced grammars across these corpora would constitute style-distinguishing evidence in the type system itself.
Held-out validation. Whether the induced grammar can parse phrases withheld from training while rejecting scrambled or randomized sequences is the cleanest test of overgeneration. This experiment has not yet been run.
Interval representation. Replacing absolute scale degrees with melodic intervals (step, skip, leap) would shift the grammar from a positional account to a motional one. Whether intervals admit a cleaner type structure than scale degrees is an empirical question with implications for what kind of object the soprano line really is.
Negative examples. The most consequential single addition. A handful of carefully constructed non-phrases — melodically implausible sequences that respect surface statistics but violate cadential logic — could force the solver into tighter and more diagnostic type assignments. This is the planned next experiment.
XI.What This Might Mean
Two angles.
Music-theoretically, the Riemannian apparatus of tonic, subdominant, and dominant function has a long history of phenomenological description. A type-theoretic restatement — fa is the scale degree with no terminal license; do is a distributor rather than a destination; the V–I cadence is a single type shared across three resolving degrees — is unusually crisp. It also opens a path: induce type systems on different repertoires and read off where the lexicon's connector-and-arrival structure differs. Style as a configuration of polysemy.
Linguistically, Lambek grammars were originally developed for natural language, with semantic content as the implicit motivation. Their applicability to a domain with no obvious semantic content — only structural well-formedness — is a quiet vindication of the formalism's claim to be a theory of combinatorial possibility rather than of meaning. Bach's chorale soprano lines parse for the same reason English sentences parse: the categories have to fit. The grammar does not care what the categories are; it cares only that they compose.
The grammar does not explain why Bach wrote what he wrote. It describes the shape of what he wrote, in a vocabulary of seven words and twenty-nine types, and reports that the shape is mostly the shape that music theory has been describing in different vocabularies for three centuries. Reading the two descriptions side by side is the present pleasure of the work.
Projectlambek-type-induction
Corpus413 BWV chorale harmonizations · music21 library
Phrases2,514 total · 2,196 after 3–10-note filter
Notes22,017 across the filtered corpus
SolverGoogle CP-SAT · batched one phrase at a time
CalculusLambek · primitives M / At1 / At3 / At5
Type depthup to 2 permitted · only depth-1 returned
Original conceptDaniel Wymark with Colin Bloomfield, ca. 2025
Current inducerDaniel Wymark with Claude Code
FindingsDaniel Wymark
Analysis passClaude Code, under Daniel Wymark's instruction
Prose draftClaude (Anthropic), from the analysis
Set inIowan Old Style, with Iosevka for the colophon
StatusComplete
DraftedGenerated 5/1/2026, fact-checked and published 5/16/2026