Skip to content

State machines (statecharts)

wireform-state-machine is a typed statechart library with a chart specification that lives at the type level. The compiler rejects errors that a string-keyed machine would otherwise find only at runtime:

  • a transition targeting a missing state, a duplicate state key, an undeclared event, or OnDone outside an Invoke is a TypeError that names the chart, the offending key, and the valid alternatives;
  • each name role — states, events, guards, actions, services, invoke ids, and done-data outputs — is a separate sum type used as a kind, so an event in a state position is a kind error;
  • guards, actions, services, and output producers live in completeness-checked registries, so missing, duplicate, or foreign implementation keys are compile errors;
  • events carry typed payloads, context is a typed machine-wide value, and final output has its declared Haskell type.

The runtime implements SCXML statecharts: compound and parallel states, shallow/deep history, guarded transitions, entry/exit/transition actions, eventless (Always) and delayed (After) transitions, promise/callback/child invocations with OnDone/OnError, done events with data, wildcard and root-level handlers, internal transitions, and typed machine output.

data ToggleState = Off | On
data ToggleEvent = FLIP
deriveKeyKind ''ToggleState
deriveKeyKind ''ToggleEvent
type Toggle :: ChartSpec ToggleState ToggleEvent NoKey NoKey NoKey NoKey NoKey
type Toggle =
Chart "toggle" () ()
'[ 'FLIP ::: () ]
'[ State 'Off '[ On 'FLIP ==> To 'On ]
, State 'On '[ On 'FLIP ==> To 'Off ]
]
'Off
impl :: ChartImpl IO Toggle
impl = chartImpl RNil RNil RNil RNil (const ())

This catalogue page maps the package. The State machines guide contains the tutorial and API guide:

ModuleWhat lives there
StateMachineumbrella re-export of the whole surface
StateMachine.Keysingleton keys: SKey, KeyKind/KnownKey, reify/demote, deriveKeyKind
StateMachine.Specthe type-level chart DSL
StateMachine.Validatewell-formedness TypeErrors (ValidChart)
StateMachine.Reifydemotion of the spec to the runtime chart (KnownChart)
StateMachine.Eventtyped events + typed lifecycle channels; decoding for named external events (decodeEvent)
StateMachine.Registrycompleteness-checked guard/action/service/output registration
StateMachine.Machinethe abstract machine value, chartImpl
StateMachine.Steppure SCXML macrostep semantics
StateMachine.Persistsnapshots, restore, recovery strategies
StateMachine.Interpretthe IO interpreter (timers, services, actors)
StateMachine.Debugdeterministic simulation, trace rendering, chart lints
StateMachine.Render.*Stately config, Mermaid, DOT, self-contained HTML
Terminal window
cabal run example-traffic

runs the traffic-light demo through a pedestrian cycle, a power outage with history restoration, snapshot/restore, stale-snapshot rejection, Recovery restart, typed final output, and all four renderers.