module Control.Eff.Reader.Lazy( Reader (..)
, ask
, local
, reader
, runReader
) where
import Control.Applicative ((<$>))
import Data.Typeable
import Control.Eff
newtype Reader e v = Reader (e -> v)
deriving (Typeable, Functor)
ask :: (Typeable e, Member (Reader e) r) => Eff r e
ask = send (inj . Reader)
local :: (Typeable e, Member (Reader e) r)
=> (e -> e)
-> Eff r a
-> Eff r a
local f m = do
e <- f <$> ask
let loop (Val x) = return x
loop (E u) = interpose u loop (\(Reader k) -> loop (k e))
loop (admin m)
reader :: (Typeable e, Member (Reader e) r) => (e -> a) -> Eff r a
reader f = f <$> ask
runReader :: Typeable e => Eff (Reader e :> r) w -> e -> Eff r w
runReader m e = loop (admin m)
where
loop (Val x) = return x
loop (E u) = handleRelay u loop (\(Reader k) -> loop (k e))