Header menu logo TDesu.FSharp

StateMachine Module

Helpers for building state machines as plain F# match expressions. Define transitions as let apply state event = match state, event with ... and use goto/stay/fail to build results.

Example

 type State = Idle | Running
 type Event = Start | Stop
 type Effect = Log of string

 let apply state event =
     match state, event with
     | Idle, Start -> StateMachine.goto Running [ Log "started" ]
     | Running, Stop -> StateMachine.goto Idle [ Log "stopped" ]
     | _ -> StateMachine.fail "invalid transition"
type State = | Idle | Running
Multiple items
module Event from Microsoft.FSharp.Control

--------------------
type Event = | Start | Stop

--------------------
type Event<'T> = new: unit -> Event<'T> member Trigger: arg: 'T -> unit member Publish: IEvent<'T> with get

--------------------
type Event<'Delegate,'Args (requires delegate and 'Delegate :> Delegate and reference type)> = new: unit -> Event<'Delegate,'Args> member Trigger: sender: objnull * args: 'Args -> unit member Publish: IEvent<'Delegate,'Args> with get

--------------------
new: unit -> Event<'T>

--------------------
new: unit -> Event<'Delegate,'Args>
type Effect = | Log of string
Multiple items
val string: value: 'T -> string

--------------------
type string = System.String
val apply: state: State -> event: Event -> 'a
val state: State
val event: Event
union case State.Idle: State
union case Event.Start: Event
union case State.Running: State
union case Effect.Log: string -> Effect
union case Event.Stop: Event

Types

Type Description

TransitionResult<'TState, 'TEffect>

Result of a state machine transition.

Functions and values

Function or value Description

StateMachine.fail msg

Full Usage: StateMachine.fail msg

Parameters:
    msg : string

Returns: Result<TransitionResult<'TState, 'TEffect>, string>
Modifiers: inline
Type parameters: 'TState, 'TEffect

Fail the transition with an error message. The error message.

msg : string
Returns: Result<TransitionResult<'TState, 'TEffect>, string>

StateMachine.goto newState effects

Full Usage: StateMachine.goto newState effects

Parameters:
    newState : 'a
    effects : 'b list

Returns: Result<TransitionResult<'a, 'b>, 'c>
Modifiers: inline
Type parameters: 'a, 'b, 'c

Create a successful transition to a new state with effects. The state to transition to. Side effects to produce.

newState : 'a
effects : 'b list
Returns: Result<TransitionResult<'a, 'b>, 'c>

StateMachine.stay state effects

Full Usage: StateMachine.stay state effects

Parameters:
    state : 'a
    effects : 'b list

Returns: Result<TransitionResult<'a, 'b>, 'c>
Modifiers: inline
Type parameters: 'a, 'b, 'c

Stay in the current state, producing effects (pass [] for no effects). The current state to remain in. Side effects to produce.

state : 'a
effects : 'b list
Returns: Result<TransitionResult<'a, 'b>, 'c>

StateMachine.tryApply state result

Full Usage: StateMachine.tryApply state result

Parameters:
Returns: 'TState * Result<'TEffect list, string>

Apply a transition result, keeping state unchanged on error. Returns (newState, Ok effects) or (unchangedState, Error msg). The current state (returned unchanged on error). The transition result from your apply function.

state : 'TState
result : Result<TransitionResult<'TState, 'TEffect>, string>
Returns: 'TState * Result<'TEffect list, string>

Type something to start searching.