
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.
Signal a for anything that unfolds over time. This post wires signals into each other, and the wiring arrives through -XArrows and proc notation. 1newtype 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)Signal representation with careful work in it: one function over all of time, cut into pieces that begin at recorded instants.Time -> a, and a sorted list of pairs [(Time, a)]. 2Signal a denotes the pair (Time -> a, Set Time): a function defined at every instant, next to the set of instants that matter. 2stepwise 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. 2Event 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. 2Signal a, and what it means for a piece of music:| Class | What it does | In a piece |
|---|---|---|
Functor | applies a function at every instant | transpose every note 2 |
Applicative | combines two signals instant by instant | apply one voice leading to any chord 3 |
Semigroup | combines things that sound at the same time | layer one motif over another 3 |
Category | composes one transformation with the next | run one patch into another 1 |
Arrow | lifts a pure function, splits a pair between two | send the same signal to two destinations 4 |
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. 3Segment a values, each a function over the entire timeline plus a Maybe for the value at the instant the segment begins. 1Segment 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. 1combine 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. 1merge walks two sorted lists together and tags each key with This, That or These, keeping both entries where the instants coincide. 1joining 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. 1opened first, which clears its segVal, so a value from an old instant is not reported again at a new one. 1Signal gets the Applicative that Segment could not have, because a Signal knows where each of its segments begins. 1Signal 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
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.1Category 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. 1proc notation: named intermediate values, with the compiler writing the tuple plumbing that moves them from stage to stage. 5proc 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. 5Applicative instance on Signal a fixes one shared input type for everything it combines. 4constS, at, hold, fhold, scanE, ev2ev and switch. 1divergence 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. 1song = horizontal $ resize 0.25 $ proc i -> do
vl <- liftH msumE <<< divergence -< i
ns <- replicateH 12 $ with id -< applyVoiceLeading vl $ chord triad
returnA -< nstoV, 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. 10.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. 1scanE and switch are noted as missing, and the sparse-signal promise is enforced nowhere. 1SF 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.Segment cannot have an Applicative because ap is never told which instant it combines at. Signal can, because the segment boundaries supply the instant."which you will note is literally a function betweenSignals."
"DespiteSegmentnot admitting anApplicativeinstance,Signaldoes, because it keeps track of where eachSegmentbegins."
"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!"
reasonablypolymorphic.com
reasonablypolymorphic.com
reasonablypolymorphic.com
hackage.haskell.org
ghc.gitlab.haskell.org
github.com
This story was produced automatically by a channel. One sentence is all it takes for Neodrop to keep producing for you.