{-# LANGUAGE TupleSections #-} {-# LANGUAGE GeneralizedNewtypeDeriving #-} module Control.Monad.Schedule.Sequence where -- base import Control.Arrow ((>>>)) import Control.Monad.IO.Class import Data.Functor.Identity import qualified Data.List.NonEmpty as NonEmpty -- transformers import Control.Monad.Trans.Class -- monad-schedule import Control.Monad.Schedule.Class -- | Any monad can be trivially scheduled by executing all actions sequentially. newtype SequenceT m a = SequenceT { SequenceT m a -> m a unSequence :: m a } deriving (a -> SequenceT m b -> SequenceT m a (a -> b) -> SequenceT m a -> SequenceT m b (forall a b. (a -> b) -> SequenceT m a -> SequenceT m b) -> (forall a b. a -> SequenceT m b -> SequenceT m a) -> Functor (SequenceT m) forall a b. a -> SequenceT m b -> SequenceT m a forall a b. (a -> b) -> SequenceT m a -> SequenceT m b forall (m :: * -> *) a b. Functor m => a -> SequenceT m b -> SequenceT m a forall (m :: * -> *) a b. Functor m => (a -> b) -> SequenceT m a -> SequenceT m b forall (f :: * -> *). (forall a b. (a -> b) -> f a -> f b) -> (forall a b. a -> f b -> f a) -> Functor f <$ :: a -> SequenceT m b -> SequenceT m a $c<$ :: forall (m :: * -> *) a b. Functor m => a -> SequenceT m b -> SequenceT m a fmap :: (a -> b) -> SequenceT m a -> SequenceT m b $cfmap :: forall (m :: * -> *) a b. Functor m => (a -> b) -> SequenceT m a -> SequenceT m b Functor, Functor (SequenceT m) a -> SequenceT m a Functor (SequenceT m) -> (forall a. a -> SequenceT m a) -> (forall a b. SequenceT m (a -> b) -> SequenceT m a -> SequenceT m b) -> (forall a b c. (a -> b -> c) -> SequenceT m a -> SequenceT m b -> SequenceT m c) -> (forall a b. SequenceT m a -> SequenceT m b -> SequenceT m b) -> (forall a b. SequenceT m a -> SequenceT m b -> SequenceT m a) -> Applicative (SequenceT m) SequenceT m a -> SequenceT m b -> SequenceT m b SequenceT m a -> SequenceT m b -> SequenceT m a SequenceT m (a -> b) -> SequenceT m a -> SequenceT m b (a -> b -> c) -> SequenceT m a -> SequenceT m b -> SequenceT m c forall a. a -> SequenceT m a forall a b. SequenceT m a -> SequenceT m b -> SequenceT m a forall a b. SequenceT m a -> SequenceT m b -> SequenceT m b forall a b. SequenceT m (a -> b) -> SequenceT m a -> SequenceT m b forall a b c. (a -> b -> c) -> SequenceT m a -> SequenceT m b -> SequenceT m c forall (f :: * -> *). Functor f -> (forall a. a -> f a) -> (forall a b. f (a -> b) -> f a -> f b) -> (forall a b c. (a -> b -> c) -> f a -> f b -> f c) -> (forall a b. f a -> f b -> f b) -> (forall a b. f a -> f b -> f a) -> Applicative f forall (m :: * -> *). Applicative m => Functor (SequenceT m) forall (m :: * -> *) a. Applicative m => a -> SequenceT m a forall (m :: * -> *) a b. Applicative m => SequenceT m a -> SequenceT m b -> SequenceT m a forall (m :: * -> *) a b. Applicative m => SequenceT m a -> SequenceT m b -> SequenceT m b forall (m :: * -> *) a b. Applicative m => SequenceT m (a -> b) -> SequenceT m a -> SequenceT m b forall (m :: * -> *) a b c. Applicative m => (a -> b -> c) -> SequenceT m a -> SequenceT m b -> SequenceT m c <* :: SequenceT m a -> SequenceT m b -> SequenceT m a $c<* :: forall (m :: * -> *) a b. Applicative m => SequenceT m a -> SequenceT m b -> SequenceT m a *> :: SequenceT m a -> SequenceT m b -> SequenceT m b $c*> :: forall (m :: * -> *) a b. Applicative m => SequenceT m a -> SequenceT m b -> SequenceT m b liftA2 :: (a -> b -> c) -> SequenceT m a -> SequenceT m b -> SequenceT m c $cliftA2 :: forall (m :: * -> *) a b c. Applicative m => (a -> b -> c) -> SequenceT m a -> SequenceT m b -> SequenceT m c <*> :: SequenceT m (a -> b) -> SequenceT m a -> SequenceT m b $c<*> :: forall (m :: * -> *) a b. Applicative m => SequenceT m (a -> b) -> SequenceT m a -> SequenceT m b pure :: a -> SequenceT m a $cpure :: forall (m :: * -> *) a. Applicative m => a -> SequenceT m a $cp1Applicative :: forall (m :: * -> *). Applicative m => Functor (SequenceT m) Applicative, Applicative (SequenceT m) a -> SequenceT m a Applicative (SequenceT m) -> (forall a b. SequenceT m a -> (a -> SequenceT m b) -> SequenceT m b) -> (forall a b. SequenceT m a -> SequenceT m b -> SequenceT m b) -> (forall a. a -> SequenceT m a) -> Monad (SequenceT m) SequenceT m a -> (a -> SequenceT m b) -> SequenceT m b SequenceT m a -> SequenceT m b -> SequenceT m b forall a. a -> SequenceT m a forall a b. SequenceT m a -> SequenceT m b -> SequenceT m b forall a b. SequenceT m a -> (a -> SequenceT m b) -> SequenceT m b forall (m :: * -> *). Monad m => Applicative (SequenceT m) forall (m :: * -> *) a. Monad m => a -> SequenceT m a forall (m :: * -> *) a b. Monad m => SequenceT m a -> SequenceT m b -> SequenceT m b forall (m :: * -> *) a b. Monad m => SequenceT m a -> (a -> SequenceT m b) -> SequenceT m b forall (m :: * -> *). Applicative m -> (forall a b. m a -> (a -> m b) -> m b) -> (forall a b. m a -> m b -> m b) -> (forall a. a -> m a) -> Monad m return :: a -> SequenceT m a $creturn :: forall (m :: * -> *) a. Monad m => a -> SequenceT m a >> :: SequenceT m a -> SequenceT m b -> SequenceT m b $c>> :: forall (m :: * -> *) a b. Monad m => SequenceT m a -> SequenceT m b -> SequenceT m b >>= :: SequenceT m a -> (a -> SequenceT m b) -> SequenceT m b $c>>= :: forall (m :: * -> *) a b. Monad m => SequenceT m a -> (a -> SequenceT m b) -> SequenceT m b $cp1Monad :: forall (m :: * -> *). Monad m => Applicative (SequenceT m) Monad, Monad (SequenceT m) Monad (SequenceT m) -> (forall a. IO a -> SequenceT m a) -> MonadIO (SequenceT m) IO a -> SequenceT m a forall a. IO a -> SequenceT m a forall (m :: * -> *). Monad m -> (forall a. IO a -> m a) -> MonadIO m forall (m :: * -> *). MonadIO m => Monad (SequenceT m) forall (m :: * -> *) a. MonadIO m => IO a -> SequenceT m a liftIO :: IO a -> SequenceT m a $cliftIO :: forall (m :: * -> *) a. MonadIO m => IO a -> SequenceT m a $cp1MonadIO :: forall (m :: * -> *). MonadIO m => Monad (SequenceT m) MonadIO) instance MonadTrans SequenceT where lift :: m a -> SequenceT m a lift = m a -> SequenceT m a forall (m :: * -> *) a. m a -> SequenceT m a SequenceT -- | Execute all actions in sequence and return their result when all of them are done. -- Essentially, this is 'sequenceA'. instance Monad m => MonadSchedule (SequenceT m) where schedule :: NonEmpty (SequenceT m a) -> SequenceT m (NonEmpty a, [SequenceT m a]) schedule = NonEmpty (SequenceT m a) -> SequenceT m (NonEmpty a) forall (t :: * -> *) (f :: * -> *) a. (Traversable t, Applicative f) => t (f a) -> f (t a) sequenceA (NonEmpty (SequenceT m a) -> SequenceT m (NonEmpty a)) -> (SequenceT m (NonEmpty a) -> SequenceT m (NonEmpty a, [SequenceT m a])) -> NonEmpty (SequenceT m a) -> SequenceT m (NonEmpty a, [SequenceT m a]) forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k). Category cat => cat a b -> cat b c -> cat a c >>> (NonEmpty a -> (NonEmpty a, [SequenceT m a])) -> SequenceT m (NonEmpty a) -> SequenceT m (NonEmpty a, [SequenceT m a]) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b fmap (, []) type Sequence = SequenceT Identity