-
Notifications
You must be signed in to change notification settings - Fork 0
/
Trans.hs
97 lines (72 loc) · 2.52 KB
/
Trans.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE UnicodeSyntax #-}
module ZIO.Trans (
EIO(..), ZIO(..)
, UEIO, URIO, UZIO, Task, RIO
, elift, ezlift, zlift
, uelift, uzlift
, euUnlift, zuUnlift
, mapEError, mapZError
, runEIO, runZIO
, withEIO, withZIO
, module Control.Monad.Except
, module Control.Monad.Reader
, module UnexceptionalIO
, module UnexceptionalIO.Trans
) where
import Control.Monad.Except
import Control.Monad.Reader hiding (lift)
import Data.Void (Void, absurd)
import UnexceptionalIO hiding (fromIO, lift, run)
import UnexceptionalIO.Trans (UIO, fromIO, run)
-- | Corresponds to IO[E, A] in Scala
newtype EIO e a = EIO { _unEIO :: ExceptT e UIO a }
deriving ( Functor, Applicative, Monad, MonadError e, MonadFix, Unexceptional )
newtype ZIO r e a = ZIO { _unZIO :: ReaderT r (EIO e) a }
deriving ( Functor, Applicative, Monad, MonadError e, MonadFix, MonadReader r, Unexceptional )
type UEIO a = EIO Void a
type URIO r a = ZIO r Void a
type UZIO a = ZIO Void Void a
type Task a = ZIO Void SomeNonPseudoException a
type RIO r a = ZIO r SomeNonPseudoException a
elift :: IO a -> EIO SomeNonPseudoException a
elift = EIO . fromIO
zlift :: IO a -> ZIO r SomeNonPseudoException a
zlift = ZIO . lift . elift
ezlift :: forall r e a. EIO e a -> ZIO r e a
ezlift = ZIO . lift
uelift :: ∀ e a. UIO a -> EIO e a
uelift = EIO . lift
uzlift :: ∀ r e a. UIO a -> ZIO r e a
uzlift = ezlift . uelift
euUnlift :: UEIO a -> UIO a
euUnlift ueio = (either absurd id) <$> ((runExceptT . _unEIO) ueio)
zuUnlift :: UZIO a -> UIO a
zuUnlift = euUnlift . flip runReaderT undefined . _unZIO
runEIO :: MonadIO m => EIO e a -> (e -> m a) -> m a
runEIO eio handler = do
resEi :: Either e a <- (run . runExceptT . _unEIO) eio
either handler pure resEi
withEIO :: (e -> e') -> EIO e a -> EIO e' a
withEIO h = EIO
. withExceptT h
. _unEIO
mapEError :: (e -> e') -> EIO e a -> EIO e' a
mapEError = withEIO
runZIO :: MonadIO m => ZIO r e a -> r -> (e -> m a) -> m a
runZIO app env handler = do
let eio = runReaderT (_unZIO app) env
runEIO eio handler
withZIO :: r -> (e -> e') -> ZIO r e a -> ZIO r e' a
withZIO r h = ezlift
. EIO
. withExceptT h
. _unEIO
. flip runReaderT r
. _unZIO
mapZError :: (e -> e') -> ZIO r e a -> ZIO r e' a
mapZError h m = do
r <- ask
withZIO r h m