Skip to content

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.

FieldValue
LanguageHaskell (GHC 8.4 – 9.14 and GHCJS, per tested-with in reflex.cabal)
LicenseBSD3 (reflex.cabal)
Repositoryreflex-frp/reflex (README.md); reflex-frp/reflex-dom for the DOM layer
DocumentationQuickref.md, reflex-frp.org, Hackage, Ryan Trinkle's NY Haskell talk (slides, video)
CategoryDiscrete-time, higher-order push-pull FRP; one engine (Spider), plus a denotational reference implementation (Reflex.Pure)
Versions surveyedreflex 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 abstractionEvent 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:

haskell
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, coercions

The 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:

  1. Take the timeline lock (_spiderTimeline_lock :: MVar ()); frames on a timeline are serialised.
  2. Write each root trigger's occurrence and propagate it depth-first to its subscribers. Pure push nodes (fmap, push, gate) run immediately.
  3. Nodes that join several parents — merge, coincidence, a reconnected switch — do not run when the first parent fires. They scheduleMerge themselves into a HeightQueue keyed by their height; the loop then drains the queue lowest-height-first (heightQueueNext, "Running height …") (Spider/Internal.hs L1649–L1675, Spider/Height.hs).
  4. Clear occurrences; apply every deferred hold assignment (writeIORef vRef v) and invalidate the pulled Behaviors that read them.
  5. Re-subscribe switch nodes 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"). Dynamic carries both halves, so a consumer chooses notification (updated) or sampling (current).
  • Lazy subscription, hot streams. An Event node 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.md notes that traceEvent "Only prints if Event is ultimately used." There is no cold/replay notion: an occurrence missed is gone; a Behavior always has a value.
  • Glitch-free. Height ordering means a merge sees all simultaneous parents before it runs; hold updates apply at the end of the frame, so every sample in 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.hs L452–L459).
  • Simultaneity is first-class. Two events firing in one frame coincide; combinators must say what happens then (leftmost is left-biased, align yields These a b, mergeWith combines). 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; performEvent results 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:

PurposeCombinators
Event → Eventfmap, fmapMaybe/mapMaybe, ffilter, push, pushAlways, difference, headE/tailE/headTailE
Sample a value at an eventtag, attach, attachWith, attachWithMaybe, gate, <@>, <@; tagPromptlyDyn, attachPromptlyDyn…
Combine events<>, leftmost, mergeWith, mergeList, merge (over a DMap), mergeMap, align/alignWith
Fan outfan/fanMap → EventSelector, select; fanEither, fanThese; demux/demuxed for Dynamic
Statehold, holdDyn, foldDyn, foldDynMaybe, foldDynM, count, toggle, accum*, mapAccum*, holdUniqDyn
Higher-order (flatten)switch, switchHold, switchDyn, switchHoldPromptly, switchPromptlyDyn, coincidence, join, switcher
Dynamic networksrunWithReplace (Adjustable), networkHold, networkView, listWithKey, listHoldWithKey, listWithKeyShallowDiff
Effects and timeperformEvent, 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 (hold creates state). Widgets in reflex-dom are sources: inputElement returns _inputElement_value :: Dynamic t Text and _inputElement_input :: Event t Text (Builder/Class.hs L113, 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 a Dynamic (dynText, elDynAttr). There is no subscribe/listen in 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):

haskell
[ ]   -- 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):

PatternMeaningExamples
suffix = result typewhich of the three types comes backholdDyn, foldDyn, zipDyn, switchDyn; accumB, mapAccumB; headE, tailE
Hold infixcreates state, needs MonadHoldswitchHold, holdUniqDyn, listHoldWithKey, networkHold
Promptlyat a coincidence, use the new value/eventswitchHoldPromptly, switchPromptlyDyn, tagPromptlyDyn, attachPromptlyDyn
Withtakes a combining functionattachWith, zipDynWith, alignWith, mergeWith
Maybethe function may drop (return Nothing)fmapMaybe, attachWithMaybe, foldDynMaybe, mapAccumMaybeDyn
Mthe function runs in PushM (may sample/hold)foldDynM, accumM, mapAccumMaybeM
_discard the resultperformEvent_, selectViewListWithKey_
'a generalisation, or "also return the element"tickLossyFrom', reflex-dom el', elAttr'
Cheapuncached; "may be run multiple times without any caching"pushCheap, fmapCheap, tagCheap, mergeWithCheap
fforflipped 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):

haskell
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) delayed

Regrets 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.hs L1789–L1798). Naming by what it builds from (a hold) won over naming by what it resembles.
  • MonadDynamicWriter → DynamicWriter (0.6.0.0) and MonadBehaviorWriter → BehaviorWriter (0.7.0.0, "for consistency with EventWriter/DynamicWriter"): the Monad prefix was dropped from capability names, but MonadHold/MonadSample kept it.
  • FunctorMaybe deprecated for Witherable's Filterable (0.6.0.0) (FunctorMaybe.hs): a home-grown class replaced by the ecosystem one.
  • mergeIntMap → mergeInt (0.10.0.0); distributeListOverDynPure → distributeListOverDyn.
  • Unreleased: PostBuild deprecated — "Use now of MonadHold instead" — and getPostBuild is now now; NotReady "is an artifact from early reflex development and no longer has a purpose"; SpiderHost lost its MonadSample/MonadHold instances because "Each sample, hold or subscribeEvent in SpiderHost used to run at its own instant, so you could miss an occurrence of now." A capability class was retired once a primitive (now, added to MonadHold in 0.7.2/0.8.0) subsumed it.
  • reflex-dom: TextInput/textInput deprecated — "Use 'inputElement' directly" (Widget/Input.hs L55); MonadWidget survives in Reflex.Dom.Old as an 18-constraint synonym class (Old.hs L105–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:

haskell
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 a

Every 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 the Chan and 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 eOfE or switchDyn listens 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. Adjustable says 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.hs L43–L48). When runWithReplace/networkHold replaces 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 ThreadId in an IORef and killThread it from a performEvent_. GHC's throwTo "does not return until the exception has been raised in the target thread" (base Control.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) and Dynamic (notification). Dynamic exists because neither half suffices for a UI: a widget needs the current value at build time and an event to patch the DOM. Pure fmap over a Dynamic keeps both in sync without a hold, which a Behavior+Event pair could not.
  • Conflation: Dynamic's updated fires 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"), and UniqDynamic makes this the type's invariant (Reflex/Dynamic.hs L114–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 Event node is shared by all subscribers (cacheEvent), and fan makes keyed multicast cheap. There is no share() operator because there is nothing unshared.
  • Sampling freshness: tag (current d) e sees the old value at a coincidence; tagPromptlyDyn d e the 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, generally tag (current d) e is preferred." (Haddock markup normalised) (Reflex/Dynamic.hs L259–L270).
  • Cycles are declared with mdo/rec (MonadFix m): a foldDyn can read an event defined below it. Delaying through current/hold breaks the instant; reading through updated/Promptly creates a same-frame dependency, which is where EventLoopExceptions 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 TestPlan class (plan :: [(Word, a)] -> m (Event t a) schedules occurrences at frame numbers) and run on Pure 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-party reflex-test-host (0.1.2.3, 2023-11-25), whose runAppSimple :: (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.Time calls forkIO and Concurrent.delay directly, so a test of debounce sleeps for real and is timing-sensitive. A deterministic debounce test means writing the operator over an injected lag (the throttleBatchWithLag shape) and passing a lag that is a test-controlled trigger. Reflex has nothing like TestClock.

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.

haskell
{-# 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 status

Trace (frames F1, F2, …; each user action or thread callback is one frame):

  • Keystroke during debounce. F1: _inputElement_input fires; gate samples enabled (True); count makes gen = n+1 and attachPromptlyDyn tags the occurrence n+1 in the same frame; durB is sampled (300 ms); after the frame, performEventAsync forks a sleeper. The previous sleeper (tag n) is still asleep. When it wakes it fires frame F2, where attachWithMaybe sees current 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's gen increments; after the frame the action runs stop (killThread on 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 (performEventAsync without the IORef), the old hash would run to completion and its result would be dropped by the generation filter.
  • Interval change. Editing ivl updates its Dynamic; durB changes at the end of that frame. A pending sleeper keeps the duration it was forked with; the next keystroke uses the new one. With library debounce plus networkHold instead, the pending query would be lost.
  • Toggle off. btn fires; enabled becomes False at frame end; off fires 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. gate sees 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, Incremental patches, keyed fan.
  • 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, Incremental merges.
  • 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 (switchHold short, switchHoldPromptly long and warned against).

Weaknesses ​

  • No cancellation: async work is fire-and-forget, latest-wins is discard-on-arrival, and Adjustable admits IO cannot be undone.
  • No virtual time: Reflex.Time hard-codes forkIO + 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; the t parameter 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 Promptly variants 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 ​

DecisionRationaleTrade-off
Event/Behavior/Dynamic as three typesSeparate notification from sampling; Dynamic gives a UI both at onceThree variants of every combinator (tag/tagPromptlyDyn, hold/holdDyn)
Engine behind a type-indexed class (Reflex t)Swappable engines, a pure reference, timeline isolationt everywhere; dictionary passing unless specialised (SPECIALIZE, -fexpose-overloaded-unfoldings)
Capability classes (MonadHold, PerformEvent, …)Constraints document and restrict what a function may doConstraint explosion; opaque errors; bundles like MonadWidget erase the precision again
Frames with height-ordered merges, end-of-frame holdGlitch-free, deterministic simultaneityRun-time causality-loop detection; Promptly coincidence semantics are subtle
Effects only via performEvent, results in a later frameKeeps propagation pureEvery async step is an extra frame; no ownership of forked work
Latest-wins = switch (unsubscribe), not cancelPurely semantic; needs no runtime supportStale work runs to completion; generation counters everywhere
Time from forkIO + wall clockSimple; works on GHC and GHCJSNo virtual time; non-deterministic tests
GC + weak references for teardownNo explicit dispose in user codeLeaks and finalizer races; teardown timing unspecified
Retire classes once a primitive subsumes them (PostBuild → now)Fewer capabilities, closer to the denotationBreaking churn across releases

Sources ​