-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Schedule an arbitrary nonempty list of automata
- Loading branch information
Showing
3 changed files
with
79 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
{-# LANGUAGE ExistentialQuantification #-} | ||
|
||
module FRP.Rhine.Schedule.Internal where | ||
|
||
-- base | ||
import Control.Arrow | ||
import Data.Function ((&)) | ||
import Data.Functor ((<&>)) | ||
import Data.Functor.Compose (Compose (..)) | ||
import Data.Kind (Type) | ||
import Data.List.NonEmpty as N | ||
|
||
-- foldable1-classes-compat | ||
import Data.Foldable1 (Foldable1 (foldrMap1)) | ||
|
||
-- sop-core | ||
import Data.SOP (HCollapse (hcollapse), HSequence (htraverse'), I (..), K (K), NP (..), NS (..), SListI, apInjs_NP, hliftA, hzipWith, unI) | ||
|
||
-- monad-schedule | ||
import Control.Monad.Schedule.Class | ||
|
||
-- automaton | ||
import Data.Stream hiding (concatS) | ||
import Data.Stream.Result | ||
|
||
newtype Step m b state = Step {getStep :: ResultStateT state m b} | ||
|
||
newtype RunningResult b state = RunningResult {getRunningResult :: Result state b} | ||
|
||
apInjs_NPNonEmpty :: (SListI xs) => NP f (x ': xs) -> NonEmpty (NS f (x ': xs)) | ||
Check warning on line 30 in rhine/src/FRP/Rhine/Schedule/Internal.hs GitHub Actions / Run hlint
|
||
apInjs_NPNonEmpty (fx :* fxs) = Z fx :| (S <$> apInjs_NP fxs) | ||
Check warning on line 31 in rhine/src/FRP/Rhine/Schedule/Internal.hs GitHub Actions / Run hlint
|
||
|
||
data Streams m b = forall state (states :: [Type]). | ||
(SListI states) => | ||
Streams | ||
{ states :: NP I (state ': states) | ||
, steps :: NP (Step m b) (state ': states) | ||
} | ||
|
||
buildStreams :: StreamT m b -> Streams m b | ||
buildStreams StreamT {state, step} = | ||
Streams | ||
{ states = I state :* Nil | ||
, steps = Step (ResultStateT step) :* Nil | ||
} | ||
|
||
consStreams :: StreamT m b -> Streams m b -> Streams m b | ||
consStreams StreamT {state, step} Streams {states, steps} = | ||
Streams | ||
{ states = I state :* states | ||
, steps = Step (ResultStateT step) :* steps | ||
} | ||
|
||
scheduleStreams :: (MonadSchedule m, Functor m, Applicative m) => Streams m b -> StreamT m (NonEmpty b) | ||
scheduleStreams Streams {states, steps} = | ||
StreamT | ||
{ state = (apInjs_NPNonEmpty states, []) | ||
, step = \(restingStates, runningStates) -> | ||
fmap (htraverse' getCompose . hzipWith (\Step {getStep} -> Compose . fmap RunningResult . getResultStateT getStep . unI) steps) restingStates | ||
& flip appendList runningStates | ||
& schedule | ||
& fmap | ||
( \(finished, running) -> | ||
let finishedStates = fmap (hliftA (I . resultState . getRunningResult)) finished | ||
outputs = | ||
finished | ||
<&> (hliftA (getRunningResult >>> output >>> K) >>> hcollapse) | ||
in Result (finishedStates, running) outputs | ||
) | ||
} | ||
|
||
scheduleStreams' :: (MonadSchedule m, Applicative m) => NonEmpty (StreamT m b) -> StreamT m (NonEmpty b) | ||
scheduleStreams' = scheduleStreams . foldrMap1 buildStreams consStreams |