mirror of
https://github.com/marcosh/crem.git
synced 2024-09-11 12:36:19 +03:00
run a state machine
consume an input to get an output and a new machine
This commit is contained in:
parent
f049b7f719
commit
952dba9c73
@ -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
|
||||
}
|
||||
)
|
||||
|
@ -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')
|
||||
|
Loading…
Reference in New Issue
Block a user