{-# LANGUAGE RecursiveDo #-}
module Polysemy.Reader.More
  (
    module Polysemy.Reader
    -- * Interpretations
  , runReaderFixSem
  ) where

import Polysemy
import Polysemy.Reader
import Polysemy.Fixpoint

------------------------------------------------------------------------------
-- | Runs a 'Reader' effect by running a monadic action /once/, after the
-- 'Sem' has completed, and then providing the result to each request
-- recursively.
runReaderFixSem :: forall i r a
                 . Member Fixpoint r
                => Sem r i
                -> Sem (Reader i ': r) a
                -> Sem r a
runReaderFixSem :: Sem r i -> Sem (Reader i : r) a -> Sem r a
runReaderFixSem Sem r i
m Sem (Reader i : r) a
sem = do
  rec
    a
a <- i -> Sem (Reader i : r) a -> Sem r a
forall i (r :: [(* -> *) -> * -> *]) a.
i -> Sem (Reader i : r) a -> Sem r a
runReader i
i Sem (Reader i : r) a
sem
    i
i <- Sem r i
m
  a -> Sem r a
forall (m :: * -> *) a. Monad m => a -> m a
return a
a
{-# INLINE runReaderFixSem #-}