Module

Rebus.Canonical

Package
purescript-rebus
Repository
afcondon/purescript-rebus

Rebus.Canonical — the text a rebus is actually made from.

Rebus hashes a string, and that is a deliberate constraint rather than a leftover. The point of a rebus is that two independent programs, with no shared code path, agree on the identity of the same content: the sequencer that captured a chord progression and the sampler that later recalls it must draw the same pair of icons, or the identity is worthless. That agreement is only checkable if the thing being hashed is a readable string someone can print from either side and compare by eye.

A class Hashable a where hash :: a -> Int would hide the disagreement: two implementations would produce different numbers and there would be nothing to look at. So the class produces text, and the text is the interop contract — a wire format, versioned like one.

#Canonical Source

class Canonical a  where

Types that can say what they are, canonically, as one line of text.

An instance is a PROMISE about a serialisation, not a convenience: change the string an instance produces and every identity previously derived from it changes with it. Write instances the way you would write a file format.

There is deliberately no instance for String. Claiming that any string is already canonical is exactly the mistake this class exists to prevent — a caller who genuinely holds canonical text should say so by reaching for rebusOfText instead.

Members

Instances

#Chords Source

newtype Chords

A chord progression as MIDI note numbers — the case two independent programs were always going to need to agree on, and therefore the case that most needed pinning down.

Its canonical form is chords separated by spaces, notes within a chord by commas, each note rendered as a decimal integer:

Chords [[42,61,66],[46,58,65]]  ⇒  "42,61,66 46,58,65"

THIS STRING IS THE WIRE FORMAT. It is not a pretty-printer and it is not open to improvement: sorting the notes, padding them, or switching the separators would re-identify every rebus ever minted from a progression. Note in particular that the order of the notes is preserved rather than normalised, so a voicing is part of the identity, and duplicates are kept, so a strum reads as its own thing.

Constructors

Instances

#chordsOf Source

chordsOf :: Array (Array Int) -> Chords

The normal form for chords that SOUND TOGETHER, and the constructor two programs should both reach for when they want to agree.

Chords preserves the order it is given, which is right for the general case and wrong for the common one. A simultaneity has no order: the notes of a chord arrive in whatever sequence the fingers landed, or bass-first from a voicing engine, or ascending from a printer — and those are three orderings of ONE chord. Left alone, two honest programs hash the same chord to different pictures and neither can see why.

So the shared path sorts each chord ascending, and the progression's own order is left alone because a progression played backwards IS a different progression. Duplicates survive the sort, so a doubled octave still reads as its own thing.

Use the bare Chords constructor only when the order WITHIN a chord is meant — an arpeggio, a strum, a roll — and say so where you do, because nothing downstream can tell the two intentions apart afterwards.