Module

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

class Return :: Symbol -> Type -> Constraintclass Return (sym :: Symbol) a  where

Type class for returning to a state in a transition handler.

Used with return @"StateName" to specify the target state after a transition.

Members

  • return :: a

    Returns to the specified state.

    For states with payloads:

    return @"StateName" { field: value }
    

    For states with empty payloads:

    return @"StateName"
    

Instances

#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 :: a

    Returns 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 stateOut

Creates 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 stateOut

Creates 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) => a

Creates 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) => a

Creates 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) => a

Creates 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 => a

Creates 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 TransitCore

A transit core containing all matches (transitions) in a state machine.

Constructors

Instances

#StateName Source

type StateName = String

Type alias for state names (runtime).

#MsgName Source

type MsgName = String

Type alias for message names (runtime).

#Match Source

data Match

A 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

#GuardName Source

type GuardName = String

Type alias for guard names (runtime).

#getStateNames Source

getStateNames :: TransitCore -> Array StateName

Extracts 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 Match

Gets 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 Transit

Base 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

#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

#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

#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

#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 StateNode

Graph representation of a state machine.

Nodes represent states, and edges represent transitions with their associated messages and optional guard conditions.

#mkStateGraph Source

mkStateGraph :: TransitCore -> StateGraph

Converts 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.