module Control.Monad.Schedule.Yield where

-- base
import qualified Control.Concurrent as Concurrent
import Control.Monad.IO.Class
import Data.Functor.Identity (Identity (runIdentity))

-- monad-schedule
import Control.Monad.Schedule.Class
import Control.Monad.Schedule.Trans

-- * 'YieldT'

-- | A monad for scheduling with cooperative concurrency.
type YieldT = ScheduleT ()

type Yield = YieldT Identity

-- | Let another thread wake up.
yield :: Monad m => YieldT m ()
yield :: forall (m :: * -> *). Monad m => YieldT m ()
yield = forall (m :: * -> *) diff. Monad m => diff -> ScheduleT diff m ()
wait ()

runYieldT :: Monad m => YieldT m a -> m a
runYieldT :: forall (m :: * -> *) a. Monad m => YieldT m a -> m a
runYieldT = forall (m :: * -> *) diff a.
Monad m =>
(diff -> m ()) -> ScheduleT diff m a -> m a
runScheduleT forall a b. (a -> b) -> a -> b
$ forall a b. a -> b -> a
const forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. Monad m => a -> m a
return ()

runYield :: Yield a -> a
runYield :: forall a. Yield a -> a
runYield = forall a. Identity a -> a
runIdentity forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. Monad m => YieldT m a -> m a
runYieldT

-- | Run a 'YieldT' value in a 'MonadIO',
--   interpreting 'yield's as GHC concurrency yields.
runYieldIO
  :: MonadIO m
  => YieldT m a -> m a
runYieldIO :: forall (m :: * -> *) a. MonadIO m => YieldT m a -> m a
runYieldIO = forall (m :: * -> *) diff a.
Monad m =>
(diff -> m ()) -> ScheduleT diff m a -> m a
runScheduleT forall a b. (a -> b) -> a -> b
$ forall a b. a -> b -> a
const forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO ()
Concurrent.yield