Reflex (Haskell)
A fully deterministic, higher-order push-pull FRP library: Event, Behavior and Dynamic are abstract types indexed by a timeline t, all reachable only through typeclasses (Reflex, MonadSample, MonadHold, PerformEvent, TriggerEvent, …), run by the Spider engine's height-ordered frames, with reflex-dom building retained-mode DOM UIs on top.
| Field | Value |
|---|---|
| Language | Haskell (GHC 8.4 – 9.14 and GHCJS, per tested-with in reflex.cabal) |
| License | BSD3 (reflex.cabal) |
| Repository | reflex-frp/reflex (README.md); reflex-frp/reflex-dom for the DOM layer |
| Documentation | Quickref.md, reflex-frp.org, Hackage, Ryan Trinkle's NY Haskell talk (slides, video) |
| Category | Discrete-time, higher-order push-pull FRP; one engine (Spider), plus a denotational reference implementation (Reflex.Pure) |
| Versions surveyed | reflex master at f443526bd2a9 (2026-11-22; cabal version 0.10.0.0 plus an Unreleased ChangeLog section); reflex-dom master at de31ceff3d99 (2026-11-23) |
| Core abstraction | Event t a (discrete occurrences), Behavior t a (a value at every instant, sampled, never observed for change), Dynamic t a (a Behavior and an Event that move together); Incremental t p (a Dynamic updated by patches) |
NOTE
The subject is the reflex library and its Spider engine. reflex-dom is cited only for how a UI obtains Dynamics and Events from widgets (inputElement) and for the MonadWidget constraint bundle. Obelisk, reflex-vty and reflex-native were not examined. Talks are cited from their published slides, not from the video.
Overview
What it solves
Reflex lets a program describe an interactive system as a pure network of time-indexed values, built once in a monad and then driven by external triggers. Its distinguishing claims among FRP libraries are that the network is higher-order (events of events, behaviours of events, networks rebuilt by events) without the space-time leaks of classic FRP, and that propagation cost is independent of network size. Trinkle's slides put the latter bluntly: "In Reflex, overall application size has no impact on event propagation time", and "Practical FRP should support dynamic data flow … In Reflex.Dom, widgets can be created, laid out, or destroyed within the FRP semantics" (NY Haskell slides).
Design philosophy
From the package description (README.md, identical in reflex.cabal):
"Interactive programs without callbacks or side-effects. Functional Reactive Programming (FRP) uses composable events and time-varying values to describe interactive systems as pure functions. … Reflex is a fully-deterministic, higher-order Functional Reactive Programming interface and an engine that efficiently implements that interface."
The phrase "an interface and an engine" is literal. The Reflex class in Reflex/Class.hs declares the types and ~20 primitives; Spider (Reflex/Spider/Internal.hs) is the production instance; Reflex/Pure.hs is the specification: "a pure implementation of Reflex, which is intended to serve as a reference for the semantics of the Reflex class. All implementations of Reflex should produce the same results as this implementation". The current unreleased ChangeLog states the direction of travel: "The goal of this release is to bring hosts closer to Reflex's denotational model of time as implemented by Reflex.Pure" (ChangeLog.md).
Compared with its siblings, Reflex is Sodium's closest relative (both discrete, transactional, glitch-free, with a value/stream split), but where Sodium exposes concrete classes, Reflex abstracts the engine behind a type parameter and every effect behind a capability class.
How it works
Three types and a timeline
The Reflex class (Reflex/Class.hs L246–L387) owns the data families:
class ( MonadHold t (PushM t), MonadSample t (PullM t), MonadFix (PushM t)
, Functor (Dynamic t), Applicative (Dynamic t), Monad (Dynamic t) ) => Reflex t where
data Behavior t :: Type -> Type
data Event t :: Type -> Type
data Dynamic t :: Type -> Type
data Incremental t :: Type -> Type
never :: Event t a
constant :: a -> Behavior t a
push :: (a -> PushM t (Maybe b)) -> Event t a -> Event t b
pull :: PullM t a -> Behavior t a
switch :: Behavior t (Event t a) -> Event t a
coincidence :: Event t (Event t a) -> Event t a
current :: Dynamic t a -> Behavior t a
updated :: Dynamic t a -> Event t a
-- … mergeG, fanG, unsafeBuildDynamic, unsafeBuildIncremental, coercionsThe doc comments define the split precisely: a Behavior "can be sampled at will, but it is not possible to be notified when they change"; an Event "during any given frame … is either occurring or not occurring"; a Dynamic is "Basically a combination of a 'Behavior' and an 'Event', with a rule that the 'Behavior' will change if and only if the 'Event' fires" (Reflex/Class.hs).
The t parameter is the timeline: "Timelines are fully-independent FRP contexts, and the type of the timeline determines the FRP engine to be used" (Reflex/Class.hs). type Spider = SpiderTimeline Global is the statically allocated default; withSpiderTimeline reifies a fresh one with a rank-2 continuation, so values from two timelines cannot be mixed — the ST-monad trick (Spider/Internal.hs L2621, L2841).
A frame, phase by phase
A frame is Reflex's transaction: one batch of trigger firings, one propagation, one set of state updates. runFrame and propagateAndRead in Spider/Internal.hs do, in order:
- Take the timeline lock (
_spiderTimeline_lock :: MVar ()); frames on a timeline are serialised. - Write each root trigger's occurrence and
propagateit depth-first to its subscribers. Pure push nodes (fmap,push,gate) run immediately. - Nodes that join several parents —
merge,coincidence, a reconnectedswitch— do not run when the first parent fires. TheyscheduleMergethemselves into aHeightQueuekeyed by their height; the loop then drains the queue lowest-height-first (heightQueueNext, "Running height …") (Spider/Internal.hsL1649–L1675,Spider/Height.hs). - Clear occurrences; apply every deferred
holdassignment (writeIORef vRef v) and invalidate the pulledBehaviors that read them. - Re-subscribe
switchnodes whose behaviour changed, apply merge patches, unsubscribe the old parents, recalculate heights.
A node's height is one more than its highest parent (heightBagSuccHeight). A merge whose height is not above the current height is a causality loop: checkCycle throws EventLoopException ("causality loop detected") (Spider/Internal.hs L1809, L2081). Reflex does not reject cycles statically; it detects them while propagating.
Getting values in and out
External input enters through a trigger. TriggerEvent offers newTriggerEvent :: m (Event t a, a -> IO ()); the standard TriggerEventT writes each invocation to a Chan, and the host's loop (runHeadlessApp, reflex-dom's main loop) reads that Chan and fires one frame per batch (TriggerEvent/Class.hs, TriggerEvent/Base.hs, Host/Headless.hs). Output leaves through PerformEvent: performEvent :: Event t (Performable m a) -> m (Event t a), whose result "will generally occur later than the input 'Event', since most 'Performable' actions cannot be performed during 'Event' propagation" (PerformEvent/Class.hs). Effects therefore run between frames, and their results start new frames.
Evaluation model
- Push-pull.
Events are pushed from triggers;Behaviors are pulled and cached, recomputed only after an invalidation (pull"will be recomputed whenever any of the read 'Behavior's changes").Dynamiccarries both halves, so a consumer chooses notification (updated) or sampling (current). - Lazy subscription, hot streams. An
Eventnode exists only while something subscribes; nodes are held by weak references and since 0.10 "garbage collected together with the last subscriber" (ChangeLog.md).Quickref.mdnotes thattraceEvent"Only prints if Event is ultimately used." There is no cold/replay notion: an occurrence missed is gone; aBehavioralways has a value. - Glitch-free. Height ordering means a merge sees all simultaneous parents before it runs;
holdupdates apply at the end of the frame, so everysamplein a frame sees the same, old value.hold's doc: "if the occurrence that sets the 'Behavior' (or one that is simultaneous with it) is used to sample the 'Behavior', it will see the old value" (Haddock emphasis rendered as bold) (Reflex/Class.hsL452–L459). - Simultaneity is first-class. Two events firing in one frame coincide; combinators must say what happens then (
leftmostis left-biased,alignyieldsThese a b,mergeWithcombines). Much of the API's variant naming (Promptly, below) exists to answer the coincidence question. - Synchronous within a frame, batched across. One frame is atomic and single-threaded;
performEventresults start a later frame.
Composition
The operator surface is large: a single-line signature grep finds 75 top-level functions in Reflex/Class.hs and 38 in Reflex/Dynamic.hs (an undercount: multi-line signatures are missed), on top of ~20 class methods and everything granted by instances (Functor, Applicative, Monad on Behavior/Dynamic; Semigroup and Filterable on Event; Align from semialign). Families, by what they produce:
| Purpose | Combinators |
|---|---|
| Event → Event | fmap, fmapMaybe/mapMaybe, ffilter, push, pushAlways, difference, headE/tailE/headTailE |
| Sample a value at an event | tag, attach, attachWith, attachWithMaybe, gate, <@>, <@; tagPromptlyDyn, attachPromptlyDyn… |
| Combine events | <>, leftmost, mergeWith, mergeList, merge (over a DMap), mergeMap, align/alignWith |
| Fan out | fan/fanMap → EventSelector, select; fanEither, fanThese; demux/demuxed for Dynamic |
| State | hold, holdDyn, foldDyn, foldDynMaybe, foldDynM, count, toggle, accum*, mapAccum*, holdUniqDyn |
| Higher-order (flatten) | switch, switchHold, switchDyn, switchHoldPromptly, switchPromptlyDyn, coincidence, join, switcher |
| Dynamic networks | runWithReplace (Adjustable), networkHold, networkView, listWithKey, listHoldWithKey, listWithKeyShallowDiff |
| Effects and time | performEvent, performEvent_, performEventAsync, delay, debounce, throttle, batchOccurrences, tickLossy |
gate :: Behavior t Bool -> Event t a -> Event t a is literally our toggle: "only occurs if the supplied 'Event' occurs and the 'Behavior' is true at the time of occurrence", defined as one attachWithMaybe (Reflex/Class.hs L1200–L1203).
fan is the efficient one-to-many primitive: "Using 'EventSelector's and the 'fan' primitive is far more efficient than (but equivalent to) using 'mapMaybe' to select only the relevant occurrences" (Reflex/Class.hs L595–L607). A fanMap over Map k a delivers each key only to that key's subscribers: O(log n) per occurrence rather than O(n) filters.
Collections are where Incremental matters. Incremental t p is "a more general form of a 'Dynamic'. Instead of always fully replacing the value, only parts of it can be patched" (PatchMap, PatchIntMap, PatchMapWithMove). listWithKey turns a Dynamic (Map k v) into one child network per key, built and torn down by diff (Reflex/Collection.hs L82); since 0.9 it filters unchanged children, which "imposes an Eq constraint on the child values" (ChangeLog.md). Compare the incremental-computation background in ../parsing/theory/incremental.md and jane-street-incremental.md.
Writer-like channels upward: EventWriter t w m (tellEvent :: Event t w -> m ()) lets a child emit into a monoid collected by its parent; Requester t m emits requests and receives responses, "Semantically, the response event occurs at a later time than the request event" (EventWriter/Class.hs, Requester/Class.hs). PerformEventT is itself implemented as a RequesterT (PerformEvent/Base.hs L77).
API structure and naming
Source / transform / sink. Reflex separates them by capability, not by type:
- Sources are created only in a monad holding a source capability:
TriggerEvent(newTriggerEvent),PostBuild/now(the build instant),MonadHold(holdcreates state). Widgets in reflex-dom are sources:inputElementreturns_inputElement_value :: Dynamic t Textand_inputElement_input :: Event t Text(Builder/Class.hsL113, L332–L335). - Transforms are pure functions when they need no "now" (
fmap,gate,switch,leftmost) and monadic when they create state (holdDyn,foldDyn,switchHold,debounce). The monad is the marker of statefulness. - Sinks are
performEvent_, a host's read phase, or (in reflex-dom) a widget that consumes aDynamic(dynText,elDynAttr). There is nosubscribe/listenin user code.
The Quickref is organised by return type and capability, not by operator name: sections "Functions producing Event", "Functions producing Behavior", "Functions producing Dynamic", "Flattening functions", then capability sections, with every signature simplified (t and constraints erased) and tagged (Quickref.md):
[ ] -- Pure function
[S] -- Function runs in any monad supporting MonadSample
[H] -- Function runs in any monad supporting MonadHold
-- later: [B] PostBuild, [A] Adjustable, [T] TriggerEvent, [P] PerformEvent
[P,T] delay :: NominalDiffTime -> Event t a -> m (Event t a)That annotation scheme is the most transferable idea in the document: the reader learns, per function, what context it needs, which is exactly the question a D user asks of an operator (does it need a Scope, a fiber, nothing?).
Modules pair a class with its standard implementation: Reflex.X.Class declares the capability, Reflex.X.Base provides the XT transformer (PerformEvent, TriggerEvent, PostBuild, EventWriter, DynamicWriter, BehaviorWriter, Requester, Query). Primitives live in the Reflex class; derived operators are free functions in Reflex.Class, Reflex.Dynamic, Reflex.Time, Reflex.Network, Reflex.Collection; Reflex re-exports everything.
Name families (from Reflex/Class.hs, Reflex/Dynamic.hs):
| Pattern | Meaning | Examples |
|---|---|---|
| suffix = result type | which of the three types comes back | holdDyn, foldDyn, zipDyn, switchDyn; accumB, mapAccumB; headE, tailE |
Hold infix | creates state, needs MonadHold | switchHold, holdUniqDyn, listHoldWithKey, networkHold |
Promptly | at a coincidence, use the new value/event | switchHoldPromptly, switchPromptlyDyn, tagPromptlyDyn, attachPromptlyDyn |
With | takes a combining function | attachWith, zipDynWith, alignWith, mergeWith |
Maybe | the function may drop (return Nothing) | fmapMaybe, attachWithMaybe, foldDynMaybe, mapAccumMaybeDyn |
M | the function runs in PushM (may sample/hold) | foldDynM, accumM, mapAccumMaybeM |
_ | discard the result | performEvent_, selectViewListWithKey_ |
' | a generalisation, or "also return the element" | tickLossyFrom', reflex-dom el', elAttr' |
Cheap | uncached; "may be run multiple times without any caching" | pushCheap, fmapCheap, tagCheap, mergeWithCheap |
ffor | flipped fmap (lambda last) | ffor, fforMaybe, ffor2, ffor3 |
The families compose mechanically, which is both the system's strength and its cost: attachPromptlyDynWithMaybe, mapAccumMaybeMB, switchHoldPromptOnly. Every name is predictable; many are long.
Promptly is marked as the dangerous direction everywhere it appears. switchHold's doc: "Use this rather than 'switchHoldPromptly' and 'switchHoldPromptOnly' unless you are absolutely sure you need to act on the new event in the coincidental case"; switchPromptlyDyn's: "The timing requirements that switching before imposes are likely to bring down your app unless you are very careful" (Reflex/Class.hs L1075–L1112, Reflex/Dynamic.hs L178–L197). The safe default got the short name.
A user-defined operator is an ordinary polymorphic function whose constraints list the capabilities it uses. debounce itself (Reflex/Time.hs L224–L229):
debounce :: (MonadFix m, MonadHold t m, PerformEvent t m, TriggerEvent t m, MonadIO (Performable m))
=> NominalDiffTime -> Event t a -> m (Event t a)
debounce dt e = do
n :: Dynamic t Integer <- count e
let tagged = attachPromptlyDynWith (,) n e
delayed <- delay dt tagged
return $ attachWithMaybe (\n' (t, v) -> if n' == t then Just v else Nothing) (current n) delayedRegrets and renames (ChangeLog.md, DEPRECATED pragmas in source):
switchPromptly→switchHoldPromptly,switchPromptOnly→switchHoldPromptOnly: "The 'switchHold*' naming convention was chosen because those functions are more closely related to each other than they are to 'switch'." (Reflex/Class.hsL1789–L1798). Naming by what it builds from (a hold) won over naming by what it resembles.MonadDynamicWriter→DynamicWriter(0.6.0.0) andMonadBehaviorWriter→BehaviorWriter(0.7.0.0, "for consistency withEventWriter/DynamicWriter"): theMonadprefix was dropped from capability names, butMonadHold/MonadSamplekept it.FunctorMaybedeprecated for Witherable'sFilterable(0.6.0.0) (FunctorMaybe.hs): a home-grown class replaced by the ecosystem one.mergeIntMap→mergeInt(0.10.0.0);distributeListOverDynPure→distributeListOverDyn.- Unreleased:
PostBuilddeprecated — "UsenowofMonadHoldinstead" — andgetPostBuildis nownow;NotReady"is an artifact from early reflex development and no longer has a purpose";SpiderHostlost itsMonadSample/MonadHoldinstances because "Eachsample,holdorsubscribeEventinSpiderHostused to run at its own instant, so you could miss an occurrence ofnow." A capability class was retired once a primitive (now, added toMonadHoldin 0.7.2/0.8.0) subsumed it. - reflex-dom:
TextInput/textInputdeprecated — "Use 'inputElement' directly" (Widget/Input.hsL55);MonadWidgetsurvives inReflex.Dom.Oldas an 18-constraint synonym class (Old.hsL105–L129).
The ergonomic cost of the capability style is visible in those constraint lists: debounce needs five; MonadHeadlessApp is a 23-constraint tuple (Host/Headless.hs L25); MonadWidget bundles 18 constraints plus CPP. Errors from a missing MonadFix or a mismatched Performable m are notoriously opaque, which is why the Quickref erases constraints entirely. Compare the mtl-style discussion in ../algebraic-effects/haskell-mtl.md.
Time and scheduling
There is no clock in the core: time is the sequence of frames. Wall-clock time lives in Reflex/Time.hs and is built entirely from performEventAsync plus forkIO:
delay :: (PerformEvent t m, TriggerEvent t m, MonadIO (Performable m)) => NominalDiffTime -> Event t a -> m (Event t a)
delay dt e = performEventAsync $ ffor e $ \a cb -> liftIO $ void $ forkIO $ do
Concurrent.delay $ ceiling $ dt * 1000000
cb aEvery occurrence forks a GHC thread that sleeps and then triggers a new frame (Reflex/Time.hs L120–L123). Durations are NominalDiffTime (seconds, picosecond resolution); timers are lossy TickInfo streams (_tickInfo_lastUTC, _tickInfo_n, _tickInfo_alreadyElapsed) from tickLossy/clockLossy, which drop ticks "If the system starts running behind" (Reflex/Time.hs L45–L95).
Debounce (quoted above) is generation counting, not timer cancellation: count e numbers every occurrence; each is delayed tagged with its number; when it returns, it passes only if current n still equals its tag. Older timers still sleep and still fire a frame, which attachWithMaybe discards. throttle and batchOccurrences are state machines over align e delayed, tied in a loop with rec (RecursiveDo) (Reflex/Time.hs L224–L280).
Can the duration change at runtime? Not in the library: debounce, throttle, delay take a plain NominalDiffTime. Two workarounds exist. Rebuild the debouncer with networkHold on each change, which discards its pending occurrence along with the old subnetwork. Or write a five-line debounceB that samples a Behavior t NominalDiffTime in the performEventAsync (shown in the worked pipeline). One function takes the lag as a parameter: throttleBatchWithLag :: (Event t () -> m (Event t ())) -> Event t a -> m (Event t a) — "For example, using delay 20 would give a simple time-based throttle." That is the only hook for injecting a different clock.
There are no schedulers or dispatchers. Within a frame, order is height order; across frames, order is the trigger Chan's FIFO.
Concurrency and cancellation
- One frame at a time per timeline, under
_spiderTimeline_lock. Triggers may be called from any thread; they enqueue on theChanand the host thread fires them. - Async work is
performEventAsync: "NOTE: Despite the name, 'performEventAsync' does not run its action in a separate thread - although the action is free to invoke forkIO and then call the callback whenever it is ready" (PerformEvent/Class.hs). CPU-bound work runs in a forked thread; the result re-enters as a trigger. - Latest-wins is
switch, and it is about events, not work.switchHold never eOfEorswitchDynlistens to the newest inner event; the old one is unsubscribed at the end of the switching frame. Nothing reaches the thread that would have fed it. - No cancellation primitive exists.
Adjustablesays so outright: "obviously, IO (and some other side-effects) cannot be undone, so it is up to the instance implementer to determine what the best meaning for this class is in such cases" (Adjustable/Class.hsL43–L48). WhenrunWithReplace/networkHoldreplaces a subnetwork, its forked threads keep running; their trigger callbacks fire into an event nobody subscribes to, and the occurrence is dropped. Stale results are discarded, never prevented. - Real cancellation is manual: keep a
ThreadIdin anIORefandkillThreadit from aperformEvent_. GHC'sthrowTo"does not return until the exception has been raised in the target thread" (baseControl.Exception) — it waits for delivery, not for the thread's cleanup to finish. That is cancel without join.
Errors and completion
Neither applies in the stream sense. An Event has no error channel and no completion: it is a set of occurrences over all time (never is the empty one). Failure is a value (Event t (Either e a), split by fanEither) or a Haskell exception: an exception thrown inside performEvent propagates to the host and ends the program unless caught there; EventLoopException reports a causality cycle at run time. "Done" is modelled as an event too: runHeadlessApp takes a network returning Event t a and "The FRP network is closed at the first occurrence of the resulting 'Event'" (Host/Headless.hs). One-shot behaviour comes from headE.
State and sharing
- Cells:
Behavior(no notification) andDynamic(notification).Dynamicexists because neither half suffices for a UI: a widget needs the current value at build time and an event to patch the DOM. Purefmapover aDynamickeeps both in sync without ahold, which aBehavior+Eventpair could not. - Conflation:
Dynamic'supdatedfires on every assignment, even to an equal value.holdUniqDyn :: Eq a => Dynamic t a -> m (Dynamic t a)filters ("only signals changes if the values actually change"), andUniqDynamicmakes this the type's invariant (Reflex/Dynamic.hsL114–L123,Dynamic/Uniq.hs). Within one frame, a trigger may fire only once ("one trigger fired twice in one frame" is a debug assertion), so multiple writes to a trigger between frames are batched by the host, not conflated by the library. - Multicast is the default: every
Eventnode is shared by all subscribers (cacheEvent), andfanmakes keyed multicast cheap. There is noshare()operator because there is nothing unshared. - Sampling freshness:
tag (current d) esees the old value at a coincidence;tagPromptlyDyn d ethe new one — and "the output 'Event' may not be used to directly change the input 'Dynamic', because that would mean its value depends on itself. When creating cyclic data flows, generallytag (current d) eis preferred." (Haddock markup normalised) (Reflex/Dynamic.hsL259–L270). - Cycles are declared with
mdo/rec(MonadFix m): afoldDyncan read an event defined below it. Delaying throughcurrent/holdbreaks the instant; reading throughupdated/Promptlycreates a same-frame dependency, which is whereEventLoopExceptions come from.
Lifetime and ownership
Ownership is the network position, and teardown is garbage collection. Nodes subscribe through weak bags (Data.WeakBag); an unreferenced subtree is collected with its last subscriber. Structural lifetime comes from Adjustable: a subnetwork built by runWithReplace, networkHold, dyn/widgetHold (reflex-dom) or a listWithKey child lives until the next replacement, at which point its subscriptions are dropped (switchSubscriptionsToKill, mergeSubscriptionsToKill in runFrame). The lifetime is lexical in the build monad — a child network created inside a parent's runWithReplace slot dies with that slot — which is close to structured scopes, except that effects started by the child (forked threads) are not owned. newEventWithLazyTriggerWithOnComplete adds setup/teardown for external listeners, with the caveat that "there is no guarantee that the teardown will be executed promptly, or even at all" (TriggerEvent/Class.hs). The 0.10 ChangeLog lists half a dozen fixed leaks and finalizer races (fanInt retention, cacheEvent thunks, switch races): GC-driven teardown is hard to make correct.
Testing
- Semantics are tested against the reference implementation: test networks are written against a
TestPlanclass (plan :: [(Word, a)] -> m (Event t a)schedules occurrences at frame numbers) and run onPure Int(time =Int) and on Spider, with outputs compared (test/Reflex/TestPlan.hs,test/Reflex/Test/CrossImpl.hs). This is the strongest part: an executable denotation. - Applications use
runHeadlessApp(test/Headless.hs) or the third-partyreflex-test-host(0.1.2.3, 2023-11-25), whoserunAppSimple :: (Event t eIn -> TestGuestT t m (Event t eOut)) -> [eIn] -> IO [[Maybe eOut]]feeds one input per frame and returns per-frame outputs. - No virtual time. Both units are frames.
Reflex.TimecallsforkIOandConcurrent.delaydirectly, so a test ofdebouncesleeps for real and is timing-sensitive. A deterministic debounce test means writing the operator over an injected lag (thethrottleBatchWithLagshape) and passing a lag that is a test-controlled trigger. Reflex has nothing likeTestClock.
The worked pipeline
reflex-dom, with two small user-defined operators: a debounce whose duration is a Behavior and which can be reset, and a latest-wins async runner with manual thread killing.
{-# LANGUAGE OverloadedStrings, ScopedTypeVariables #-}
import Control.Concurrent (forkIO, killThread, threadDelay)
import Control.Exception (evaluate)
import Control.Monad (void)
import Control.Monad.IO.Class (liftIO)
import Data.Foldable (traverse_)
import Data.IORef
import Data.Time.Clock (NominalDiffTime)
import Reflex.Dom
-- Debounce whose duration is sampled per occurrence; `reset` invalidates a pending one.
debounceB :: (MonadHold t m, MonadFix m, PerformEvent t m, TriggerEvent t m, MonadIO (Performable m))
=> Behavior t NominalDiffTime -> Event t () -> Event t a -> m (Event t a)
debounceB dur reset e = do
gen <- count (leftmost [() <$ e, reset]) :: m (Dynamic t Int)
fired <- performEventAsync $ ffor (attach dur (attachPromptlyDyn gen e)) $ \(d, gx) cb ->
liftIO . void . forkIO $ threadDelay (ceiling (d * 1e6)) >> cb gx
pure $ attachWithMaybe (\g (g', x) -> if g == g' then Just x else Nothing) (current gen) fired
-- Latest-wins worker: a new request (or `reset`) kills the running thread; stale results are dropped.
latestAsync :: (MonadIO m, MonadHold t m, MonadFix m, PerformEvent t m, TriggerEvent t m, MonadIO (Performable m))
=> Event t () -> (a -> IO b) -> Event t a -> m (Event t b)
latestAsync reset work e = do
gen <- count (leftmost [() <$ e, reset]) :: m (Dynamic t Int)
running <- liftIO (newIORef Nothing)
let stop = readIORef running >>= traverse_ killThread
performEvent_ $ liftIO stop <$ reset
done <- performEventAsync $ ffor (attachPromptlyDyn gen e) $ \(g, x) cb -> liftIO $ do
stop
tid <- forkIO $ work x >>= evaluate >>= \r -> cb (g, r)
writeIORef running (Just tid)
pure $ attachWithMaybe (\g (g', r) -> if g == g' then Just r else Nothing) (current gen) done
app :: MonadWidget t m => m ()
app = do
q <- inputElement def
ivl <- inputElement $ def & inputElementConfig_initialValue .~ "300"
btn <- button "on/off"
enabled <- toggle True btn -- Dynamic t Bool
let durB = current (parseMs <$> _inputElement_value ivl) -- Behavior t NominalDiffTime
off = () <$ ffilter not (updated enabled)
queries = gate (current enabled) (_inputElement_input q) -- the toggle is `gate`
debounced <- debounceB durB off queries
digests <- latestAsync off hashFileSet debounced
status <- holdDyn "idle" $ leftmost [showDigest <$> digests, "hashing…" <$ debounced, "off" <$ off]
el "div" $ dynText statusTrace (frames F1, F2, …; each user action or thread callback is one frame):
- Keystroke during debounce. F1:
_inputElement_inputfires;gatesamplesenabled(True);countmakesgen= n+1 andattachPromptlyDyntags the occurrence n+1 in the same frame;durBis sampled (300 ms); after the frame,performEventAsyncforks a sleeper. The previous sleeper (tag n) is still asleep. When it wakes it fires frame F2, whereattachWithMaybeseescurrent gen= n+1 ≠ n and drops it. One sleeping thread and one wasted frame per keystroke; nothing is cancelled. - Keystroke during hash. The new query's debounce completes in some frame Fk;
latestAsync'sgenincrements; after the frame the action runsstop(killThreadon the hashing thread, blocking until the exception is delivered, not until the thread unwinds) and forks a new hash. Had we used the library idiom (performEventAsyncwithout theIORef), the old hash would run to completion and its result would be dropped by the generation filter. - Interval change. Editing
ivlupdates itsDynamic;durBchanges at the end of that frame. A pending sleeper keeps the duration it was forked with; the next keystroke uses the new one. With librarydebounceplusnetworkHoldinstead, the pending query would be lost. - Toggle off.
btnfires;enabledbecomes False at frame end;offfires in the same frame, incrementing both generations, so the pending sleeper's and any in-flight hash's results will be dropped;performEvent_kills the hash thread after the frame. The debounce sleeper is not killed — it wakes, starts a frame, and is filtered.gatesees False from the next frame on (in the toggle frame itself it still samples True, which is harmless here because no keystroke coincides).
Fit with a per-frame UI. Reflex is the opposite of rebuild-every-frame: the network and the DOM are built once and patched by events, and a Reflex "frame" is a transaction, not a render tick. What transfers is the shape of state: our frame-polled Cell!T is a Behavior (sample/current), and a Cell plus its version counter is a Dynamic (the version bump is updated). What does not transfer is cancellation: on a structured-concurrency loop, the generation-counter pattern that Reflex needs for both debounce and latest-wins collapses into cancelling the child scope.
Strengths
- The most complete higher-order FRP vocabulary in production use:
switch/switchHold/switchDyn,runWithReplace,listWithKey,Incrementalpatches, keyedfan. - Glitch-freedom and simultaneity are specified (a reference implementation) and enforced (height-ordered merges, end-of-frame
hold). - Cost scales with what changes, not with the network: lazy subscription,
fan/select,demux,Incrementalmerges. - A clean source/transform/sink split by capability: pure operators are visibly pure; anything stateful is monadic; effects are named by class.
- Quickref's organisation by result type with capability tags is an excellent reference format.
- Naming is systematic and safe-by-default (
switchHoldshort,switchHoldPromptlylong and warned against).
Weaknesses
- No cancellation: async work is fire-and-forget, latest-wins is discard-on-arrival, and
Adjustableadmits IO cannot be undone. - No virtual time:
Reflex.Timehard-codesforkIO+ wall-clock delay; time-based operators cannot be tested deterministically without rewriting them. - Time operators take fixed
NominalDiffTime; a runtime-adjustable debounce is user code. - Large constraint sets (
MonadWidget,MonadHeadlessApp) and opaque type errors; thetparameter appears in every signature. - GC-driven teardown has produced a long tail of leaks and finalizer races (0.10 ChangeLog).
- Causality loops are detected at run time, not prevented; the
Promptlyvariants are documented as likely to "bring down your app". - No error or completion channel; "done" and "failed" must be modelled as ordinary events.
Key design decisions and trade-offs
| Decision | Rationale | Trade-off |
|---|---|---|
Event/Behavior/Dynamic as three types | Separate notification from sampling; Dynamic gives a UI both at once | Three variants of every combinator (tag/tagPromptlyDyn, hold/holdDyn) |
Engine behind a type-indexed class (Reflex t) | Swappable engines, a pure reference, timeline isolation | t everywhere; dictionary passing unless specialised (SPECIALIZE, -fexpose-overloaded-unfoldings) |
Capability classes (MonadHold, PerformEvent, …) | Constraints document and restrict what a function may do | Constraint explosion; opaque errors; bundles like MonadWidget erase the precision again |
Frames with height-ordered merges, end-of-frame hold | Glitch-free, deterministic simultaneity | Run-time causality-loop detection; Promptly coincidence semantics are subtle |
Effects only via performEvent, results in a later frame | Keeps propagation pure | Every async step is an extra frame; no ownership of forked work |
Latest-wins = switch (unsubscribe), not cancel | Purely semantic; needs no runtime support | Stale work runs to completion; generation counters everywhere |
Time from forkIO + wall clock | Simple; works on GHC and GHCJS | No virtual time; non-deterministic tests |
| GC + weak references for teardown | No explicit dispose in user code | Leaks and finalizer races; teardown timing unspecified |
Retire classes once a primitive subsumes them (PostBuild → now) | Fewer capabilities, closer to the denotation | Breaking churn across releases |
Sources
- reflex at
f443526bd2a9d4ba43f65f3a755d21e61082f4e0:README.md,Quickref.md,ChangeLog.md,reflex.cabal;Reflex/Class.hs,Reflex/Dynamic.hs,Reflex/Dynamic/Uniq.hs,Reflex/Time.hs,Reflex/PerformEvent/Class.hs,Reflex/PerformEvent/Base.hs,Reflex/TriggerEvent/Class.hs,Reflex/TriggerEvent/Base.hs,Reflex/PostBuild/Class.hs,Reflex/Adjustable/Class.hs,Reflex/Network.hs,Reflex/Collection.hs,Reflex/EventWriter/Class.hs,Reflex/Requester/Class.hs,Reflex/FunctorMaybe.hs,Reflex/Spider/Internal.hs,Reflex/Spider/Height.hs,Reflex/Host/Class.hs,Reflex/Host/Headless.hs,Reflex/Pure.hs; testsTestPlan.hs,CrossImpl.hs,Headless.hs. - reflex-dom at
de31ceff3d995381da661dc25b50e5b99fdd6316:Quickref.md,Reflex/Dom/Builder/Class.hs,Reflex/Dom/Widget/Input.hs,Reflex/Dom/Old.hs. - reflex-frp.org; Hackage: reflex; Hackage: reflex-test-host; Ryan Trinkle, Reflex: Practical Functional Reactive Programming, NY Haskell User's Group (slides, video); base
Control.Exception.throwTo. - Related: index, concepts, comparison, Sodium, RxJS, Elm, Yampa, synchronous languages, Jane Street Incremental, Effect-TS, mtl.