Transit
- Package
- purescript-transit
- Repository
- m-bock/purescript-transit
Main API for building type-safe state machines.
This module provides the core functions for creating state machine update functions from type-level specifications.
#Return Source
#ReturnVia Source
class ReturnVia :: Symbol -> Symbol -> Type -> Constraintclass ReturnVia (symGuard :: Symbol) (sym :: Symbol) a where
Type class for returning to a state via a guard condition.
Used with returnVia @"GuardName" @"StateName" to specify a conditional
transition with a guard.
Members
returnVia :: aReturns to the specified state via a guard condition.
For states with payloads:
returnVia @"GuardName" @"StateName" { field: value }For states with empty payloads:
returnVia @"GuardName" @"StateName"
Instances
#match Source
match :: forall @symStateIn @symMsgIn stateIn msgIn stateOut. (stateIn -> msgIn -> stateOut) -> MatchImpl symStateIn symMsgIn stateIn msgIn Identity stateOutCreates a pure match handler for a state transition.
The handler function receives the current state and message, and returns
the new state. Use matchM for monadic handlers.
Example:
match @"DoorOpen" @"Close" \_ _ -> return @"DoorClosed"
#matchM Source
matchM :: forall @symStateIn @symMsgIn m stateIn msgIn stateOut. (stateIn -> msgIn -> m stateOut) -> MatchImpl symStateIn symMsgIn stateIn msgIn m stateOutCreates a monadic match handler for a state transition.
The handler function receives the current state and message, and returns
the new state in a monadic context. Use match for pure handlers.
Example:
matchM @"DoorOpen" @"Close" \_ _ -> do
Console.log "Closing door"
pure $ return @"DoorClosed"
#mkUpdate Source
mkUpdate :: forall @spec tcore msg state args a. IsTransitSpec spec tcore => CurryN args (Variant state -> Variant msg -> Variant state) a => MkUpdate tcore Identity MaybeChurch args (Variant msg) (Variant state) => aCreates a pure update function.
Returns state, silently returning the unchanged state on illegal
transitions. Use mkUpdateEither if you need error handling.
Example:
update :: State -> Msg -> State
update = mkUpdate @MyTransit ...
#mkUpdateMaybe Source
mkUpdateMaybe :: forall @spec tcore msg state args a. IsTransitSpec spec tcore => CurryN args (Variant state -> Variant msg -> Maybe (Variant state)) a => MkUpdate tcore Identity Maybe args (Variant msg) (Variant state) => aCreates a pure update function with error handling.
Returns Maybe state, returning Nothing for illegal transition requests.
Example:
update :: State -> Msg -> Maybe State
update = mkUpdateEither @MyTransit ...
#mkUpdateMaybeM Source
mkUpdateMaybeM :: forall @spec tcore msg state args m a. IsTransitSpec spec tcore => CurryN args (Variant state -> Variant msg -> m (Maybe (Variant state))) a => MkUpdate tcore m Maybe args (Variant msg) (Variant state) => aCreates a monadic update function with error handling.
Returns m (Maybe state), returning Nothing for illegal transition requests.
Example:
update :: State -> Msg -> Effect (Maybe State)
update = mkUpdateEitherM @MyTransit ...
#mkUpdateM Source
mkUpdateM :: forall @spec tcore msg state args m a. IsTransitSpec spec tcore => CurryN args (Variant state -> Variant msg -> m (Variant state)) a => MkUpdate tcore m MaybeChurch args (Variant msg) (Variant state) => Functor m => aCreates a monadic update function.
Returns m state, silently returning the unchanged state on illegal
transitions. Use mkUpdateEitherM if you need error handling.
Example:
update :: State -> Msg -> Effect State
update = mkUpdateM @MyTransit ...
Re-exports from Transit.Core
#TransitCore Source
newtype TransitCoreA transit core containing all matches (transitions) in a state machine.
Constructors
Instances
Show TransitCoreEq TransitCoreGeneric TransitCore _Reflectable (MkTransitCoreTL Nil') TransitCore(Reflectable (MkTransitCoreTL transitions) TransitCore, Reflectable transition Match) => Reflectable (MkTransitCoreTL (Cons' transition transitions)) TransitCore
#Match Source
data MatchA match represents a transition from a state triggered by a message.
- First
StateName: Source state name - Second
MsgName: Message name Array Return: Possible return states (may include guards)
Constructors
Instances
Show MatchEq MatchGeneric Match _(IsSymbol stateName, IsSymbol msgName) => Reflectable (MkMatchTL stateName msgName Nil') Match(IsSymbol stateName, IsSymbol msgName, Reflectable (MkMatchTL stateName msgName returns) Match, Reflectable ret Return) => Reflectable (MkMatchTL stateName msgName (Cons' ret returns)) Match
#getStateNames Source
getStateNames :: TransitCore -> Array StateNameExtracts all unique state names from a transit core.
Returns all states that appear as either source states or target states in any match, with duplicates removed.
#getMatchesForState Source
getMatchesForState :: StateName -> TransitCore -> Array MatchGets all matches (transitions) that originate from a given state.
Returns an array of all matches where the source state matches the provided state name.
Re-exports from Transit.DSL
#Transit Source
data TransitBase type for transit specifications (empty specification).
Start all transit specifications with Transit, then add matches using :*.
Example:
type MyTransit =
Transit
:* ("State1" :@ "Msg1" >| "State2")
:* ("State2" :@ "Msg2" >| "State3")
Instances
IsTransitSpec Transit (MkTransitCoreTL Nil')Empty transit specification (no matches).
ToTransitCore Transit (MkTransitCoreTL Nil')(ToTransitCore xs (MkTransitCoreTL ys)) => ToTransitCore (AddMatch Transit xs) (MkTransitCoreTL ys)
#AddIn Source
data AddIn :: forall k1 k2. k1 -> k2 -> Typedata AddIn a b
Internal type for adding an input (bidirectional edge).
Used with the |< operator.
Instances
(ToTransitCore xs (MkTransitCoreTL ys)) => ToTransitCore (AddMatch (AddOut (AddIn s1 ms) s2) xs) (MkTransitCoreTL (Cons' (MkMatchTL s1 ms (Cons' (MkReturnTL s2) Nil')) (Cons' (MkMatchTL s2 ms (Cons' (MkReturnTL s1) Nil')) ys)))ToTransitCore (AddOut (AddIn s1 ms) s2) (MkTransitCoreTL (Cons' (MkMatchTL s1 ms (Cons' (MkReturnTL s2) Nil')) (Cons' (MkMatchTL s2 ms (Cons' (MkReturnTL s1) Nil')) Nil')))ToMatch (AddOut (AddIn x y) z) (MkMatchTL x y Nil')
#ToMatch Source
class ToMatch :: forall k. k -> MatchTL -> Constraintclass ToMatch dsl a | dsl -> a
Type class that converts DSL match expressions to match specifications.
Handles conversion of state-message combinations and return states into the underlying match type-level representation.
Instances
#ToReturn Source
class ToReturn :: forall k. k -> ReturnTL -> Constraintclass ToReturn dsl a | dsl -> a
Type class that converts DSL return expressions to return specifications.
Handles conversion of state names and guard conditions into the underlying return type-level representation.
Instances
ToReturn (WithGuard g s) (MkReturnViaTL g s)ToReturn s (MkReturnTL s)
#ToTransitCore Source
class ToTransitCore :: forall k. k -> TransitCoreTL -> Constraintclass ToTransitCore dsl a | dsl -> a
Type class that converts DSL expressions to transit core specifications.
This class is used internally to transform the DSL syntax into the underlying transit core type-level representation.
Instances
ToTransitCore Transit (MkTransitCoreTL Nil')(ToTransitCore xs (MkTransitCoreTL ys)) => ToTransitCore (AddMatch Transit xs) (MkTransitCoreTL ys)(ToTransitCore xs (MkTransitCoreTL ys)) => ToTransitCore (AddMatch (AddOut (AddIn s1 ms) s2) xs) (MkTransitCoreTL (Cons' (MkMatchTL s1 ms (Cons' (MkReturnTL s2) Nil')) (Cons' (MkMatchTL s2 ms (Cons' (MkReturnTL s1) Nil')) ys)))ToTransitCore (AddOut (AddIn s1 ms) s2) (MkTransitCoreTL (Cons' (MkMatchTL s1 ms (Cons' (MkReturnTL s2) Nil')) (Cons' (MkMatchTL s2 ms (Cons' (MkReturnTL s1) Nil')) Nil')))(ToMatch x t, ToTransitCore xs (MkTransitCoreTL ys)) => ToTransitCore (AddMatch x xs) (MkTransitCoreTL (Cons' t ys))(ToMatch x t) => ToTransitCore x (MkTransitCoreTL (Cons' t Nil'))
#type (|<) Source
Operator alias for Transit.DSL.AddIn (left-associative / precedence 5)
Infix operator for creating bidirectional edges.
Example: "State1" |< "Msg" >| "State2" creates edges in both directions.
#type (>|) Source
Operator alias for Transit.DSL.AddOut (left-associative / precedence 5)
Infix operator for adding return states to a match.
Example: "State1" :@ "Msg1" >| "State2" >| "State3"
#type (:@) Source
Operator alias for Transit.DSL.StateWithMsg (left-associative / precedence 5)
Infix operator for combining a state with a message.
Example: "State1" :@ "Msg1"
#type (:?) Source
Operator alias for Transit.DSL.WithGuard (left-associative / precedence 9)
Infix operator for adding guard conditions to returns.
Example: "guard" :? "State2"
#type (:*) Source
Operator alias for Transit.DSL.AddMatch (right-associative / precedence 0)
Infix operator for adding matches to a transit specification.
Right-associative, allowing Transit to appear first for clean indentation.
Example: Transit :* ("State1" :@ "Msg1" >| "State2")
Re-exports from Transit.StateGraph
#StateGraph Source
type StateGraph = Graph EdgeStateTransition StateNodeGraph representation of a state machine.
Nodes represent states, and edges represent transitions with their associated messages and optional guard conditions.
#mkStateGraph Source
mkStateGraph :: TransitCore -> StateGraphConverts a transit core specification into a graph representation.
Each match in the transit core becomes one or more edges in the graph, with each return state creating a separate edge. Guard conditions are preserved in the edge labels.
- Modules
- Transit
- Transit.
Class. CheckReturn - Transit.
Class. CurryN - Transit.
Class. MkHandlerLookup - Transit.
Class. MkUpdate - Transit.
Core - Transit.
DSL - Transit.
Data. DotLang - Transit.
Data. Graph - Transit.
Data. Html - Transit.
Data. MaybeChurch - Transit.
Data. Table - Transit.
HandlerLookup - Transit.
Render. Graphviz - Transit.
Render. Theme - Transit.
Render. TransitionTable - Transit.
StateGraph - Transit.
VariantUtils
Returns to the specified state.
For states with payloads:
For states with empty payloads: