---
title: 'Prismriver in Lean 4: Formal Music Theory & Composition'
url: https://www.emergentmind.com/topics/prismriver
type: topic
---

# Prismriver in Lean 4: Formal Music Theory & Composition

Searching arXiv for the specified paper to ground the article in the source metadata.
Prismriver is a domain-specific library and embedded language in Lean 4 for music theory, analysis, and algorithmic composition. It formalizes pitches, intervals, scales, chords, transposition, counterpoint, part assignments, and durations in a dependently typed setting, with the stated goals of capturing the mathematical essence of music theory and leveraging that formalization for verifiable, extensible algorithmic composition and accompaniment generation. The system is presented as a framework in which classical theorems of music theory can be verified and composition algorithms can be made correct-by-construction, while interoperating with MIDI, MusicXML, and Alda for playback and export [2606.19936].

## 1. Definition, scope, and stated objectives

Prismriver’s primary goal is explicitly two-fold. First, it formalizes notes, accidentals, intervals, scales, chords, part assignments, and durations in Lean 4. Second, it uses that formalization as a basis for verifiable and extensible algorithmic composition, including accompaniment, counterpoint, and monadic score generators, with interoperability for MIDI, MusicXML, and Alda [2606.19936].

The project is motivated by the claim that nearly every core notion in music carries algebraic structure, specifically including torsors, abelian groups, and dihedral-group actions. In Prismriver, these structures are not treated as informal analogies but as objects of formal proof. This design links music-theoretic reasoning to interactive theorem proving: one can represent a score, express compositional rules as propositions, and discharge proof obligations within Lean.

The abstract also states that Prismriver opens the door to verifiable algorithmic composition and accompaniment generation and enables the analysis of monadic analysis of structures in music [2606.19936]. A plausible implication is that the project is intended not merely as a notation library, but as a formal substrate for both compositional construction and proof-oriented analytical work.

## 2. Algebraic representation of pitch, interval, and scale

The foundational abstraction is the Lean type class `class PseudoScale (P : Type)`, where the elements of `P` represent pitches in an arbitrary tuning system [2606.19936]. A true `Scale` extends this idea by equipping a `PseudoScale P` with an index type `I` of intervals satisfying two requirements: `I` is an additive abelian group, and there is a group action `· : I → P → P` making `P` into an `I`-torsor. In the paper’s presentation, this supports the notation \(p + i\) for transposition of a pitch \(p\) by an interval \(i\), while pitch subtraction yields intervals.

The familiar 12-tone equal-temperament system is recovered concretely by taking
\[
P_{\mathrm{eq}} = \mathbb{Z}
\]
for MIDI-style pitch numbers,
\[
I_{\mathrm{eq}} = \mathbb{Z}/12\mathbb{Z}
\]
for semitone intervals modulo 12, and fundamental interval \(1_{\mathrm{eq}} = 12\) [2606.19936]. Enharmonic equivalence, such as \( \mathrm{C}\sharp \) versus \( \mathrm{D}\flat \), is described as emerging as the orbit under \(I_{\mathrm{eq}}\).

For Western classical music with explicit note names and accidentals, Prismriver instantiates the following structures:
```lean
structure Pitch := (name : ℤ) (acc : Accidental)
structure Interval := (name : ℤ) (semitones : ℤ)
```
In this representation, an interval stores both its diatonic name-distance \(n\) and its semitone-count \(a\) [2606.19936]. The stated significance of this separation is that it permits modeling non-equal-temperaments and xenharmonic systems, including Bohlen–Pierce and quarter-tones, by choosing a different group \(I\) or base \(P\). This directly counters a common misconception that a formal music-theory library in Lean would necessarily be confined to 12-tone equal temperament.

## 3. Symmetry, transposition, and generalized dihedral actions

Prismriver’s formal machinery is organized around two algebraic pillars. The first is the `Scale` class, which axiomatizes that \(I\) is an `AddCommGroup` with zero \(0_I\), together with scalar multiplication by integers, negation, subtraction, and a list of natural pitches in one fundamental interval, such as those for C major [2606.19936]. The second is a generalized treatment of transposition and inversion through dihedral-type actions.

Any transposition or inversion of pitches is encoded by the inductive type
```lean
inductive TransposeAction
| r  (i : I)        -- rotation (translation) by i
| sr (a : P) (i : I) -- reflection about a followed by translation by i
```
Prismriver then proves in Lean that the four possible compositions satisfy the following relations:
\[
r_i \circ r_j = r_{i+j}
\]
\[
r_i \circ sr_{a,j} = sr_{a,\,j-i}
\]
\[
sr_{a,i} \circ r_j = sr_{a,\,i+j}
\]
\[
sr_{a,i} \circ sr_{b,j} = r_{2\cdot(a/b)+j-i}
\]
where \(a/b\) denotes pitch subtraction yielding an interval, and \(2 \cdot (a/b)\) uses the group structure on \(I\) [2606.19936].

A centerpiece theorem relates this abstract `TransposeAction` on `Pitch` and `Interval` to the classical action of the dihedral group \(D_{12}\) on 12-tone pitch classes \((\mathbb{Z}/12\mathbb{Z})\). Lean code defines a lift
```lean
def TransposeAction.toDihedral : TransposeAction → DihedralGroup 12
```
and proves that for every \(t : \texttt{TransposeAction}\) and every pitch-class triad \((p,q) \in \mathbb{Z}/12\mathbb{Z} \times \texttt{Parity}\) with major/minor parity,
\[
\Phi(t) \,\bullet\, (p,q) = (\, t \bullet p,\ \tau_t(q) \,)
\]
where \(\tau_t\) flips parity precisely for inversions [2606.19936]. The paper describes this theorem as machine-checked and presents it as establishing the duality between abstract scale theory and the classical neo-Riemannian/\(D_{12}\) viewpoint associated with Crans–Fiore–Satyendra.

## 4. Verifiable algorithmic composition and counterpoint

On top of the representation library, Prismriver provides a `CompositionT` monad, described as a free monad over time-cursor movements, note addition, and event addition [2606.19936]. This permits composition routines to be written in Lean `do`-notation while simultaneously supporting proofs of invariants about the resulting musical objects. The paper gives the following example:
```lean
def compositionM : CompositionT Id Unit := do
   addPart 0 { instrument? := .some Instrument.violin }
   let t14 : MeasuredTime := mkRat 1 4
   addNote (.new .e 4) t14 (partId? := some 0)
   addNote (.new .c 5) t14 (partId? := some 0)
   moveBar
   addNote (.new .e 4) (mkRat 1 1) (partId? := some 0)
```
This code can be executed via Lean’s `#play` directive, targeting Alda for real-time playback, or compiled to MusicXML or MIDI for use by external tools [2606.19936].

Prismriver also includes a counterpoint-composition framework. First-species rules are encoded as `Prop`-valued predicates on pairs of voice lists. The rules listed in the paper are: start and end on octave or unison, only consonant vertical intervals, allowed leaps, contrary motion into the finale, no repeated notes, and no hidden fifths or tritones [2606.19936]. A proof that generated counterpoint satisfies `isFirstSpecies` is described as a routine Lean proof obtained by unfolding definitions.

This arrangement places compositional correctness inside the proof assistant rather than outside it. In practical terms, a score generator is not merely a producer of candidate outputs; it can be coupled to machine-checked guarantees about contrapuntal admissibility or intervallic properties. This suggests a synthesis of algorithmic composition and formal verification in which rule systems are executable and provable in the same environment.

## 5. Library organization and workflow

Prismriver is organized into representation, theory, composition, and examples components [2606.19936].

| Module path | Contents |
|---|---|
| `Prismriver.Repr` | `Scale.lean`, `Classical.lean`, `Dihedral.lean` |
| `Prismriver.Theory` | `Dihedral.lean`, transposition theorems |
| `Prismriver.Composition` | `CompositionT`, score analysis, `Counterpoint.lean` |
| `examples/` | `Necrofantasia.lean`, `CounterpointExample.lean` |

The paper also gives a typical workflow. One imports Prismriver, opens `Prismriver.Repr.Classical`, defines pitches or scores either as Lean expressions or with a Lilypad-style syntax, proves theorems about the composition, and exports to MusicXML. The specific sequence presented is:

1. `import Prismriver`
2. `open Prismriver.Repr.Classical`
3. define pitches or score via Lean expressions or the Lilypad-style syntax:
   ```lean
   #play [e4 c'4 b4 d4 e2]
   ```
4. prove theorems about the composition:
   ```lean
   example : isFirstSpecies myCantus myCounter := by …
   ```
5. export to MusicXML via `runCmd (exportMusicXML “my_score.xml”)` [2606.19936]

The coexistence of an internal proof workflow and external output targets is central to the library’s architecture. Prismriver is not presented solely as a formal semantics for music-theoretic objects, nor solely as a score-generation tool; rather, it combines theorem-proving infrastructure with practical interchange formats.

## 6. Significance, extensibility, and proposed directions

The paper identifies four principal benefits of embedding music theory within a proof assistant. The first is formal guarantees: a proposition such as “every interval in this chord progression is consonant” can be proved in Lean. The second is extensibility: quarter-tone accidentals, Pythagorean tuning, and entirely new scales such as Bohlen–Pierce can be introduced by supplying new instances of the `Scale` and `Interval` classes. The third is a reusable library in a Mathlib-style repository, allowing proof patterns about dihedral compositions, counterpoint rules, and part-writing invariants to accumulate for future music-theoretic research. The fourth is pedagogical tooling, because students can hear generated output through `#play` while inspecting proof obligations corresponding to classical compositional rules [2606.19936].

Potential extensions listed in the paper include integrating stochastic processes through Markov-chain-based composition, richer species-counterpoint monads, automated search using SMT or tactics for chord progressions satisfying specified criteria, and formalization of twentieth-century techniques such as set theory and serialism [2606.19936]. These proposals are future directions rather than implemented features.

Prismriver therefore occupies a technically specific position at the intersection of formal methods, computational music theory, and algorithmic composition. Its contribution is not only to encode musical objects, but to express their transformations, constraints, and compositional procedures in a framework where proofs and generated musical artifacts coexist. The paper’s concluding claim is that classical music theory and modern formal methods can illuminate one another, yielding verifiably correct musical artifacts and a platform for future work in computer-aided composition [2606.19936].

Source: https://www.emergentmind.com/topics/prismriver