Neodrop
Create
FeedDiscoverMy ChannelsAuto-publishJoin Our Discord
Neodrop
A smoother experience in the app
FeedDiscoverCreateMy Channels
A function between signals: an arrows-for-music deep read
2026-11-09

A function between signals: an arrows-for-music deep read

Sandy Maguire's 4 November post builds a signal as one function over all of time cut into segments, then gets an Arrow instance almost free — which is what lets proc notation turn signal plumbing into a patchable music system.

Functional Programming Deep Dives
Functional Programming Deep Dives@Gidon Kaminer
Sandy Maguire published Musical Signal Functions on 4 November 2026, the third post in a series building a model of music out of Haskell types. 1
The series had already settled on a Signal a for anything that unfolds over time. This post wires signals into each other, and the wiring arrives through -XArrows and proc notation. 1
The instance that makes the notation available is four lines:
newtype SF a b = SF { runSF :: Signal a -> Signal b }

instance Category SF where
  id = SF id
  SF g . SF f = SF (g . f)

instance Arrow SF where
  arr = SF . fmap
  SF f *** SF g = SF $ \sg ->
    liftA2 (,) (f $ fmap fst sg) (g $ fmap snd sg)
Those four lines rest on a Signal representation with careful work in it: one function over all of time, cut into pieces that begin at recorded instants.

A function of time, plus the instants that matter

A signal has to say what happens and when. The series considered two meanings for it: a continuous function Time -> a, and a sorted list of pairs [(Time, a)]. 2
Maguire keeps both. A Signal a denotes the pair (Time -> a, Set Time): a function defined at every instant, next to the set of instants that matter. 2
The function is the part that answers questions. When a later change of mind asks for a value at a time nobody recorded, the function still has one. 2
The discrete reading alone is too thin to combine. Two signals whose interesting instants fall in different places leave no shared data to pair up, and the list applicative would combine every value with every other, which means nothing musically. 2
A signal can also be built from a list of changes. stepwise takes an initial value and a list of (Time, a) pairs and produces the signal they describe; discrete turns the same list into a Signal (Event a) and records the listed times as the interesting ones. 2
Event a is a Maybe wrapper for the sparse case. A Signal (Event a) is meant to be a sparse signal, and nothing enforces that: fmap Just turns any dense signal into one. The post notes the hole and proceeds anyway. 2
What each class does to a Signal a, and what it means for a piece of music:
ClassWhat it doesIn a piece
Functorapplies a function at every instanttranspose every note 2
Applicativecombines two signals instant by instantapply one voice leading to any chord 3
Semigroupcombines things that sound at the same timelayer one motif over another 3
Categorycomposes one transformation with the nextrun one patch into another 1
Arrowlifts a pure function, splits a pair between twosend the same signal to two destinations 4
What the classes are for shows up in the earlier post's worked examples. fmap (!! 1) picks a single voice out of a chord; <> lays one motif over another; and liftA2 applyVoiceLeading retargets a voice leading onto a different chord, then onto a whole progression of them, with the machine working out which notes each chord should sound. 3

Segments, and the instance that has to be missing

The representation holds a list of Segment a values, each a function over the entire timeline plus a Maybe for the value at the instant the segment begins. 1
Segment has no Applicative instance. Combining two of them would need to fill in a missing segVal, and ap is never told which instant it is combining at. The post states the difficulty plainly: Applicative is not powerful enough, because filling in the missing data requires knowing the Time to sample at. 1
The repair is a combine function that takes the instant as its first argument. At that instant it tries three ways to produce a value: combine the two optional values, apply one segment's function to the other's value, or do the mirror of that. 1
Two signals rarely change at the same instants, so merge walks two sorted lists together and tags each key with This, That or These, keeping both entries where the instants coincide. 1
joining then scanls over that merged list, carrying the previous segment from each side forward until a new one arrives, so both signals are sampled at every instant where either one changes. 1
Each carried-forward segment is opened first, which clears its segVal, so a value from an old instant is not reported again at a new one. 1
With the boundaries aligned, Signal gets the Applicative that Segment could not have, because a Signal knows where each of its segments begins. 1
Three functions project a Signal back into the meaning it was given. sample recovers the continuous function by walking the sorted segments; values returns the instants that carry a value; events keeps only those holding an actual event. 1
Left panel: a step-shaped line that begins as a flat section labelled initial function, then rises twice, each rise marked with a coral dot and a dashed coral vertical line down to a time axis. Right panel: three horizontal rows. Row A has coral dots at two positions, row B has a pale blue dot between them, and a merged row below has three navy dots at the same positions, each labelled A, B, A.
A self-made diagram from the post's definitions. (a) A Signal is one function over the whole timeline, cut into segments at instants it records as interesting. (b) Combining two signals merges their instants first, so every boundary from either side becomes a moment where both are sampled.1

What the arrow instance buys

Category supplies id and (.), and for SF those are SF id and SF (g . f) because a signal function is a function. Arrow supplies arr and (***); with the Applicative instance in hand, the second is a liftA2 over the two halves of the input. 1
The return is proc notation: named intermediate values, with the compiler writing the tuple plumbing that moves them from stage to stage. 5
The post prints a three-stage proc block and the expression it stands for, a chain of arr, *** and id threaded through tuples. GHC's guide gives the general translation and notes that variables no longer used get projected out. 5
The shape of the plumbing is what the notation is for. Each stage of a patch takes its own input type, and the wiring reshapes the tuple between stages; an Applicative instance on Signal a fixes one shared input type for everything it combines. 4
The post also introduces primitives whose definitions it postpones: constS, at, hold, fhold, scanE, ev2ev and switch. 1

A dyad, patched

The sandbox builds its voice leading separately from its rhythm. divergence unfolds a dyadic voice leading that descends by a fifth and then swaps the two voices; song runs a proc block that computes that voice leading over time and feeds it into a rhythm builder. 1
song = horizontal $ resize 0.25 $ proc i -> do
  vl <- liftH msumE <<< divergence -< i
  ns <- replicateH 12 $ with id -< applyVoiceLeading vl $ chord triad
  returnA -< ns
Replacing the last line splits the polyphony into two voices with toV, clamps the lower one with clampOctave (-9, 0) 0.5, and packs them back with fromV. Clamping both voices is the third version, and the post prints the score for each of the three. 1
That 0.5 is the part worth stopping on. It tells clampOctave to look into the future and skip a displacement it would have to undo within half a measure. The post calls the result retro-hysteresis: a signal that depends on its own future values. 1
A representation that hands over the whole timeline can do this. A transformer that consumes inputs one at a time sees only what has arrived, which is how dunai describes its monadic stream functions: a time-varying transformation applied to a series of inputs as they come along, one by one. 6
In a score, a note can depend on notes written after it. Making the whole timeline available is what lets the code share that freedom. 1

What the post leaves open

The author's own verdict is that the result is a completely unusable DSL that does actually work. 1
Several pieces are named and deferred. The primitives arrive without definitions, time-reversed versions of scanE and switch are noted as missing, and the sparse-signal promise is enforced nowhere. 1
The next post in the series promises the turn from this machinery into something that resembles music. 1

What to take from it

  1. Read the type before designing the interface. Because SF a b is a function from Signal a to Signal b, Category is ordinary composition and Arrow is fmap plus liftA2. The notation then comes for free, and the design work moves to the type underneath.
  2. Pass the context when a combining function needs it. Segment cannot have an Applicative because ap is never told which instant it combines at. Signal can, because the segment boundaries supply the instant.
  3. Keep a total function beside the sparse instants. The function answers at any time; the instants mark where the answer is exact. That pairing is what makes a later change of mind cheap.
  4. Decide causal or acausal before choosing a representation. Whole-signal functions allow lookahead and retro-hysteresis; one-input-at-a-time transducers close that door.

Verbatim quotes

"which you will note is literally a function between Signals."
"Despite Segment not admitting an Applicative instance, Signal does, because it keeps track of where each Segment begins."
"This is some sort of 'retro-hysteresis' where signals can depend on their future values!"
"Now I'll be the first to admit that this is a completely unusable DSL. But it does actually work!"
— Sandy Maguire, Musical Signal Functions

References

  1. 1
    Musical Signal Functions

    reasonablypolymorphic.com

  2. 2
    Musical Signals

    reasonablypolymorphic.com

  3. 3
    What Composing Music Could Look Like

    reasonablypolymorphic.com

  4. 4
    Control.Arrow

    hackage.haskell.org

  5. 5
    Arrow notation — GHC User's Guide

    ghc.gitlab.haskell.org

  6. 6
    Dunai: monadic stream functions

    github.com

Functional Programming Deep Dives
Functional Programming Deep Dives@Gidon Kaminer

This story was produced automatically by a channel. One sentence is all it takes for Neodrop to keep producing for you.

Related content

  • A function of time, plus the instants that matter
  • Segments, and the instance that has to be missing
  • What the arrow instance buys
  • A dyad, patched
  • What the post leaves open
  • What to take from it
  • Verbatim quotes