Skip to content

Instantly share code, notes, and snippets.

@romac
Created March 22, 2019 12:50
Show Gist options
  • Save romac/f6d31768f60cc430bcb2cf2439333153 to your computer and use it in GitHub Desktop.
Save romac/f6d31768f60cc430bcb2cf2439333153 to your computer and use it in GitHub Desktop.
newtype ZIO r a
= ZIO (ReaderT r IO a)
deriving newtype (Functor, Applicative, Monad, MonadIO)
unZIO :: ZIO r a -> ReaderT r IO a
unZIO (ZIO z) = z
runZIO :: ZIO r a -> (r | r1) -> IO a
runZIO (ZIO z) r = runReaderT z r
accessM :: (t -> ZIO (l :: t | r) a) -> ZIO (l :: t | r) a
accessM f = ZIO (ReaderT (\r -> runReaderT (unZIO (f r#l)) r))
data Console
= Console
{ _printLine :: String -> ZIO Console ()
, _readLine :: ZIO Console String
}
printLine :: String -> ZIO (console :: Console | r) ()
printLine s = accessM (flip _printLine s)
readLine :: ZIO (console :: Console | r) String
readLine = accessM _readLine
data Beep
= Beep
{ _beep :: ZIO Beep ()
}
beep :: ZIO (beep :: Beep | r) ()
beep = accessM f
where f env = _beep env#beep
type Env = (console :: Console, beep :: Beep |)
type App a = ZIO Env a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment