{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
module Control.Carrier.Accum.IORef
(
runAccum
, execAccum
, evalAccum
, AccumC(AccumC)
, module Control.Effect.Accum
) where
import Control.Algebra
import Control.Applicative (Alternative(..))
import Control.Effect.Accum
import Control.Monad (MonadPlus(..))
import Control.Monad.Fail as Fail
import Control.Monad.Fix
import Control.Monad.IO.Class
import Control.Monad.Trans.Class
import Data.IORef
import qualified Data.Semigroup as S
import Control.Monad.IO.Unlift (MonadUnliftIO)
import Control.Carrier.Reader
runAccum :: MonadIO m => w -> AccumC w m a -> m (w, a)
runAccum :: w -> AccumC w m a -> m (w, a)
runAccum w
start AccumC w m a
go = do
IORef w
ref <- IO (IORef w) -> m (IORef w)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (w -> IO (IORef w)
forall a. a -> IO (IORef a)
newIORef w
start)
a
result <- IORef w -> ReaderC (IORef w) m a -> m a
forall r (m :: * -> *) a. r -> ReaderC r m a -> m a
runReader IORef w
ref (ReaderC (IORef w) m a -> m a)
-> (AccumC w m a -> ReaderC (IORef w) m a) -> AccumC w m a -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AccumC w m a -> ReaderC (IORef w) m a
forall w (m :: * -> *) a. AccumC w m a -> ReaderC (IORef w) m a
runAccumC (AccumC w m a -> m a) -> AccumC w m a -> m a
forall a b. (a -> b) -> a -> b
$ AccumC w m a
go
w
final <- IO w -> m w
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IORef w -> IO w
forall a. IORef a -> IO a
readIORef IORef w
ref)
(w, a) -> m (w, a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (w
final, a
result)
{-# INLINE runAccum #-}
execAccum :: MonadIO m => w -> AccumC w m a -> m w
execAccum :: w -> AccumC w m a -> m w
execAccum w
w = ((w, a) -> w) -> m (w, a) -> m w
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (w, a) -> w
forall a b. (a, b) -> a
fst (m (w, a) -> m w)
-> (AccumC w m a -> m (w, a)) -> AccumC w m a -> m w
forall b c a. (b -> c) -> (a -> b) -> a -> c
. w -> AccumC w m a -> m (w, a)
forall (m :: * -> *) w a.
MonadIO m =>
w -> AccumC w m a -> m (w, a)
runAccum w
w
{-# INLINE execAccum #-}
evalAccum :: MonadIO m => w -> AccumC w m a -> m a
evalAccum :: w -> AccumC w m a -> m a
evalAccum w
w = ((w, a) -> a) -> m (w, a) -> m a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (w, a) -> a
forall a b. (a, b) -> b
snd (m (w, a) -> m a)
-> (AccumC w m a -> m (w, a)) -> AccumC w m a -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. w -> AccumC w m a -> m (w, a)
forall (m :: * -> *) w a.
MonadIO m =>
w -> AccumC w m a -> m (w, a)
runAccum w
w
{-# INLINE evalAccum #-}
newtype AccumC w m a = AccumC { AccumC w m a -> ReaderC (IORef w) m a
runAccumC :: ReaderC (IORef w) m a }
deriving (Applicative (AccumC w m)
AccumC w m a
Applicative (AccumC w m)
-> (forall a. AccumC w m a)
-> (forall a. AccumC w m a -> AccumC w m a -> AccumC w m a)
-> (forall a. AccumC w m a -> AccumC w m [a])
-> (forall a. AccumC w m a -> AccumC w m [a])
-> Alternative (AccumC w m)
AccumC w m a -> AccumC w m a -> AccumC w m a
AccumC w m a -> AccumC w m [a]
AccumC w m a -> AccumC w m [a]
forall a. AccumC w m a
forall a. AccumC w m a -> AccumC w m [a]
forall a. AccumC w m a -> AccumC w m a -> AccumC w m a
forall w (m :: * -> *). Alternative m => Applicative (AccumC w m)
forall w (m :: * -> *) a. Alternative m => AccumC w m a
forall w (m :: * -> *) a.
Alternative m =>
AccumC w m a -> AccumC w m [a]
forall w (m :: * -> *) a.
Alternative m =>
AccumC w m a -> AccumC w m a -> AccumC w m a
forall (f :: * -> *).
Applicative f
-> (forall a. f a)
-> (forall a. f a -> f a -> f a)
-> (forall a. f a -> f [a])
-> (forall a. f a -> f [a])
-> Alternative f
many :: AccumC w m a -> AccumC w m [a]
$cmany :: forall w (m :: * -> *) a.
Alternative m =>
AccumC w m a -> AccumC w m [a]
some :: AccumC w m a -> AccumC w m [a]
$csome :: forall w (m :: * -> *) a.
Alternative m =>
AccumC w m a -> AccumC w m [a]
<|> :: AccumC w m a -> AccumC w m a -> AccumC w m a
$c<|> :: forall w (m :: * -> *) a.
Alternative m =>
AccumC w m a -> AccumC w m a -> AccumC w m a
empty :: AccumC w m a
$cempty :: forall w (m :: * -> *) a. Alternative m => AccumC w m a
$cp1Alternative :: forall w (m :: * -> *). Alternative m => Applicative (AccumC w m)
Alternative, Functor (AccumC w m)
a -> AccumC w m a
Functor (AccumC w m)
-> (forall a. a -> AccumC w m a)
-> (forall a b.
AccumC w m (a -> b) -> AccumC w m a -> AccumC w m b)
-> (forall a b c.
(a -> b -> c) -> AccumC w m a -> AccumC w m b -> AccumC w m c)
-> (forall a b. AccumC w m a -> AccumC w m b -> AccumC w m b)
-> (forall a b. AccumC w m a -> AccumC w m b -> AccumC w m a)
-> Applicative (AccumC w m)
AccumC w m a -> AccumC w m b -> AccumC w m b
AccumC w m a -> AccumC w m b -> AccumC w m a
AccumC w m (a -> b) -> AccumC w m a -> AccumC w m b
(a -> b -> c) -> AccumC w m a -> AccumC w m b -> AccumC w m c
forall a. a -> AccumC w m a
forall a b. AccumC w m a -> AccumC w m b -> AccumC w m a
forall a b. AccumC w m a -> AccumC w m b -> AccumC w m b
forall a b. AccumC w m (a -> b) -> AccumC w m a -> AccumC w m b
forall a b c.
(a -> b -> c) -> AccumC w m a -> AccumC w m b -> AccumC w m c
forall w (m :: * -> *). Applicative m => Functor (AccumC w m)
forall w (m :: * -> *) a. Applicative m => a -> AccumC w m a
forall w (m :: * -> *) a b.
Applicative m =>
AccumC w m a -> AccumC w m b -> AccumC w m a
forall w (m :: * -> *) a b.
Applicative m =>
AccumC w m a -> AccumC w m b -> AccumC w m b
forall w (m :: * -> *) a b.
Applicative m =>
AccumC w m (a -> b) -> AccumC w m a -> AccumC w m b
forall w (m :: * -> *) a b c.
Applicative m =>
(a -> b -> c) -> AccumC w m a -> AccumC w m b -> AccumC w m c
forall (f :: * -> *).
Functor f
-> (forall a. a -> f a)
-> (forall a b. f (a -> b) -> f a -> f b)
-> (forall a b c. (a -> b -> c) -> f a -> f b -> f c)
-> (forall a b. f a -> f b -> f b)
-> (forall a b. f a -> f b -> f a)
-> Applicative f
<* :: AccumC w m a -> AccumC w m b -> AccumC w m a
$c<* :: forall w (m :: * -> *) a b.
Applicative m =>
AccumC w m a -> AccumC w m b -> AccumC w m a
*> :: AccumC w m a -> AccumC w m b -> AccumC w m b
$c*> :: forall w (m :: * -> *) a b.
Applicative m =>
AccumC w m a -> AccumC w m b -> AccumC w m b
liftA2 :: (a -> b -> c) -> AccumC w m a -> AccumC w m b -> AccumC w m c
$cliftA2 :: forall w (m :: * -> *) a b c.
Applicative m =>
(a -> b -> c) -> AccumC w m a -> AccumC w m b -> AccumC w m c
<*> :: AccumC w m (a -> b) -> AccumC w m a -> AccumC w m b
$c<*> :: forall w (m :: * -> *) a b.
Applicative m =>
AccumC w m (a -> b) -> AccumC w m a -> AccumC w m b
pure :: a -> AccumC w m a
$cpure :: forall w (m :: * -> *) a. Applicative m => a -> AccumC w m a
$cp1Applicative :: forall w (m :: * -> *). Applicative m => Functor (AccumC w m)
Applicative, a -> AccumC w m b -> AccumC w m a
(a -> b) -> AccumC w m a -> AccumC w m b
(forall a b. (a -> b) -> AccumC w m a -> AccumC w m b)
-> (forall a b. a -> AccumC w m b -> AccumC w m a)
-> Functor (AccumC w m)
forall a b. a -> AccumC w m b -> AccumC w m a
forall a b. (a -> b) -> AccumC w m a -> AccumC w m b
forall w (m :: * -> *) a b.
Functor m =>
a -> AccumC w m b -> AccumC w m a
forall w (m :: * -> *) a b.
Functor m =>
(a -> b) -> AccumC w m a -> AccumC w m b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> AccumC w m b -> AccumC w m a
$c<$ :: forall w (m :: * -> *) a b.
Functor m =>
a -> AccumC w m b -> AccumC w m a
fmap :: (a -> b) -> AccumC w m a -> AccumC w m b
$cfmap :: forall w (m :: * -> *) a b.
Functor m =>
(a -> b) -> AccumC w m a -> AccumC w m b
Functor, Applicative (AccumC w m)
a -> AccumC w m a
Applicative (AccumC w m)
-> (forall a b.
AccumC w m a -> (a -> AccumC w m b) -> AccumC w m b)
-> (forall a b. AccumC w m a -> AccumC w m b -> AccumC w m b)
-> (forall a. a -> AccumC w m a)
-> Monad (AccumC w m)
AccumC w m a -> (a -> AccumC w m b) -> AccumC w m b
AccumC w m a -> AccumC w m b -> AccumC w m b
forall a. a -> AccumC w m a
forall a b. AccumC w m a -> AccumC w m b -> AccumC w m b
forall a b. AccumC w m a -> (a -> AccumC w m b) -> AccumC w m b
forall w (m :: * -> *). Monad m => Applicative (AccumC w m)
forall w (m :: * -> *) a. Monad m => a -> AccumC w m a
forall w (m :: * -> *) a b.
Monad m =>
AccumC w m a -> AccumC w m b -> AccumC w m b
forall w (m :: * -> *) a b.
Monad m =>
AccumC w m a -> (a -> AccumC w m b) -> AccumC w m b
forall (m :: * -> *).
Applicative m
-> (forall a b. m a -> (a -> m b) -> m b)
-> (forall a b. m a -> m b -> m b)
-> (forall a. a -> m a)
-> Monad m
return :: a -> AccumC w m a
$creturn :: forall w (m :: * -> *) a. Monad m => a -> AccumC w m a
>> :: AccumC w m a -> AccumC w m b -> AccumC w m b
$c>> :: forall w (m :: * -> *) a b.
Monad m =>
AccumC w m a -> AccumC w m b -> AccumC w m b
>>= :: AccumC w m a -> (a -> AccumC w m b) -> AccumC w m b
$c>>= :: forall w (m :: * -> *) a b.
Monad m =>
AccumC w m a -> (a -> AccumC w m b) -> AccumC w m b
$cp1Monad :: forall w (m :: * -> *). Monad m => Applicative (AccumC w m)
Monad, Monad (AccumC w m)
Monad (AccumC w m)
-> (forall a. String -> AccumC w m a) -> MonadFail (AccumC w m)
String -> AccumC w m a
forall a. String -> AccumC w m a
forall w (m :: * -> *). MonadFail m => Monad (AccumC w m)
forall w (m :: * -> *) a. MonadFail m => String -> AccumC w m a
forall (m :: * -> *).
Monad m -> (forall a. String -> m a) -> MonadFail m
fail :: String -> AccumC w m a
$cfail :: forall w (m :: * -> *) a. MonadFail m => String -> AccumC w m a
$cp1MonadFail :: forall w (m :: * -> *). MonadFail m => Monad (AccumC w m)
Fail.MonadFail, Monad (AccumC w m)
Monad (AccumC w m)
-> (forall a. (a -> AccumC w m a) -> AccumC w m a)
-> MonadFix (AccumC w m)
(a -> AccumC w m a) -> AccumC w m a
forall a. (a -> AccumC w m a) -> AccumC w m a
forall w (m :: * -> *). MonadFix m => Monad (AccumC w m)
forall w (m :: * -> *) a.
MonadFix m =>
(a -> AccumC w m a) -> AccumC w m a
forall (m :: * -> *).
Monad m -> (forall a. (a -> m a) -> m a) -> MonadFix m
mfix :: (a -> AccumC w m a) -> AccumC w m a
$cmfix :: forall w (m :: * -> *) a.
MonadFix m =>
(a -> AccumC w m a) -> AccumC w m a
$cp1MonadFix :: forall w (m :: * -> *). MonadFix m => Monad (AccumC w m)
MonadFix, Monad (AccumC w m)
Monad (AccumC w m)
-> (forall a. IO a -> AccumC w m a) -> MonadIO (AccumC w m)
IO a -> AccumC w m a
forall a. IO a -> AccumC w m a
forall w (m :: * -> *). MonadIO m => Monad (AccumC w m)
forall w (m :: * -> *) a. MonadIO m => IO a -> AccumC w m a
forall (m :: * -> *).
Monad m -> (forall a. IO a -> m a) -> MonadIO m
liftIO :: IO a -> AccumC w m a
$cliftIO :: forall w (m :: * -> *) a. MonadIO m => IO a -> AccumC w m a
$cp1MonadIO :: forall w (m :: * -> *). MonadIO m => Monad (AccumC w m)
MonadIO, Monad (AccumC w m)
Alternative (AccumC w m)
AccumC w m a
Alternative (AccumC w m)
-> Monad (AccumC w m)
-> (forall a. AccumC w m a)
-> (forall a. AccumC w m a -> AccumC w m a -> AccumC w m a)
-> MonadPlus (AccumC w m)
AccumC w m a -> AccumC w m a -> AccumC w m a
forall a. AccumC w m a
forall a. AccumC w m a -> AccumC w m a -> AccumC w m a
forall w (m :: * -> *).
(Alternative m, Monad m) =>
Monad (AccumC w m)
forall w (m :: * -> *).
(Alternative m, Monad m) =>
Alternative (AccumC w m)
forall w (m :: * -> *) a. (Alternative m, Monad m) => AccumC w m a
forall w (m :: * -> *) a.
(Alternative m, Monad m) =>
AccumC w m a -> AccumC w m a -> AccumC w m a
forall (m :: * -> *).
Alternative m
-> Monad m
-> (forall a. m a)
-> (forall a. m a -> m a -> m a)
-> MonadPlus m
mplus :: AccumC w m a -> AccumC w m a -> AccumC w m a
$cmplus :: forall w (m :: * -> *) a.
(Alternative m, Monad m) =>
AccumC w m a -> AccumC w m a -> AccumC w m a
mzero :: AccumC w m a
$cmzero :: forall w (m :: * -> *) a. (Alternative m, Monad m) => AccumC w m a
$cp2MonadPlus :: forall w (m :: * -> *).
(Alternative m, Monad m) =>
Monad (AccumC w m)
$cp1MonadPlus :: forall w (m :: * -> *).
(Alternative m, Monad m) =>
Alternative (AccumC w m)
MonadPlus, m a -> AccumC w m a
(forall (m :: * -> *) a. Monad m => m a -> AccumC w m a)
-> MonadTrans (AccumC w)
forall w (m :: * -> *) a. Monad m => m a -> AccumC w m a
forall (m :: * -> *) a. Monad m => m a -> AccumC w m a
forall (t :: (* -> *) -> * -> *).
(forall (m :: * -> *) a. Monad m => m a -> t m a) -> MonadTrans t
lift :: m a -> AccumC w m a
$clift :: forall w (m :: * -> *) a. Monad m => m a -> AccumC w m a
MonadTrans, MonadIO (AccumC w m)
MonadIO (AccumC w m)
-> (forall b.
((forall a. AccumC w m a -> IO a) -> IO b) -> AccumC w m b)
-> MonadUnliftIO (AccumC w m)
((forall a. AccumC w m a -> IO a) -> IO b) -> AccumC w m b
forall b.
((forall a. AccumC w m a -> IO a) -> IO b) -> AccumC w m b
forall w (m :: * -> *). MonadUnliftIO m => MonadIO (AccumC w m)
forall w (m :: * -> *) b.
MonadUnliftIO m =>
((forall a. AccumC w m a -> IO a) -> IO b) -> AccumC w m b
forall (m :: * -> *).
MonadIO m
-> (forall b. ((forall a. m a -> IO a) -> IO b) -> m b)
-> MonadUnliftIO m
withRunInIO :: ((forall a. AccumC w m a -> IO a) -> IO b) -> AccumC w m b
$cwithRunInIO :: forall w (m :: * -> *) b.
MonadUnliftIO m =>
((forall a. AccumC w m a -> IO a) -> IO b) -> AccumC w m b
$cp1MonadUnliftIO :: forall w (m :: * -> *). MonadUnliftIO m => MonadIO (AccumC w m)
MonadUnliftIO)
instance (Algebra sig m, S.Semigroup w, MonadIO m) => Algebra (Accum w :+: sig) (AccumC w m) where
alg :: Handler ctx n (AccumC w m)
-> (:+:) (Accum w) sig n a -> ctx () -> AccumC w m (ctx a)
alg Handler ctx n (AccumC w m)
hdl (:+:) (Accum w) sig n a
sig ctx ()
ctx = case (:+:) (Accum w) sig n a
sig of
L Accum w n a
accum -> do
IORef w
ref <- ReaderC (IORef w) m (IORef w) -> AccumC w m (IORef w)
forall w (m :: * -> *) a. ReaderC (IORef w) m a -> AccumC w m a
AccumC (forall r (sig :: (* -> *) -> * -> *) (m :: * -> *).
Has (Reader r) sig m =>
m r
forall (sig :: (* -> *) -> * -> *) (m :: * -> *).
Has (Reader (IORef w)) sig m =>
m (IORef w)
ask @(IORef w))
(a -> ctx () -> ctx a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ ctx ()
ctx) (a -> ctx a) -> AccumC w m a -> AccumC w m (ctx a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> case Accum w n a
accum of
Add w
w' -> IO () -> AccumC w m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IORef w -> (w -> w) -> IO ()
forall a. IORef a -> (a -> a) -> IO ()
modifyIORef' IORef w
ref (w -> w -> w
forall a. Semigroup a => a -> a -> a
S.<> w
w'))
Accum w n a
Look -> IO w -> AccumC w m w
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IORef w -> IO w
forall a. IORef a -> IO a
readIORef IORef w
ref)
R sig n a
other -> ReaderC (IORef w) m (ctx a) -> AccumC w m (ctx a)
forall w (m :: * -> *) a. ReaderC (IORef w) m a -> AccumC w m a
AccumC (Handler ctx n (ReaderC (IORef w) m)
-> (:+:) (Reader (IORef w)) sig n a
-> ctx ()
-> ReaderC (IORef w) m (ctx a)
forall (sig :: (* -> *) -> * -> *) (m :: * -> *) (ctx :: * -> *)
(n :: * -> *) a.
(Algebra sig m, Functor ctx) =>
Handler ctx n m -> sig n a -> ctx () -> m (ctx a)
alg (AccumC w m (ctx x) -> ReaderC (IORef w) m (ctx x)
forall w (m :: * -> *) a. AccumC w m a -> ReaderC (IORef w) m a
runAccumC (AccumC w m (ctx x) -> ReaderC (IORef w) m (ctx x))
-> (ctx (n x) -> AccumC w m (ctx x))
-> ctx (n x)
-> ReaderC (IORef w) m (ctx x)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ctx (n x) -> AccumC w m (ctx x)
Handler ctx n (AccumC w m)
hdl) (sig n a -> (:+:) (Reader (IORef w)) sig n a
forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
(m :: * -> *) k.
g m k -> (:+:) f g m k
R sig n a
other) ctx ()
ctx)
{-# INLINE alg #-}