run a state machine

consume an input to get an output and a new machine
This commit is contained in:
Marco Perone 2023-01-17 09:41:59 +01:00 committed by Marco Perone
parent f049b7f719
commit 952dba9c73
2 changed files with 34 additions and 0 deletions

View File

@ -116,3 +116,24 @@ identity =
{ initialState = InitialState STuple0
, action = ActionResult
}
-- ** Run a machine
-- | Given an `input`, run the machine to get an output and a new version of
-- the machine
runBaseMachine
:: BaseMachine topology input output
-> input
-> (output, BaseMachine topology input output)
runBaseMachine (BaseMachine (InitialState initialState) action) input =
let
actionResult = action initialState input
in
case actionResult of
(ActionResult finalState output) ->
( output
, BaseMachine
{ initialState = InitialState finalState
, action = action
}
)

View File

@ -67,3 +67,16 @@ instance Choice StateMachine where
right' :: StateMachine a b -> StateMachine (Either c a) (Either c b)
right' (Basic baseMachine) = Basic $ right' baseMachine
right' (Compose machine1 machine2) = Compose (right' machine1) (right' machine2)
-- * Run a state machine
-- | Given an `input`, run the machine to get an output and a new version of
-- the machine
run :: StateMachine a b -> a -> (b, StateMachine a b)
run (Basic baseMachine) a = Basic <$> runBaseMachine baseMachine a
run (Compose machine1 machine2) a =
let
(output1, machine1') = run machine1 a
(output2, machine2') = run machine2 output1
in
(output2, Compose machine1' machine2')