{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}

{- | A carrier for 'Accum' effects.
This carrier performs its append operations strictly and thus avoids the space leaks inherent in lazy writer monads.
These appends are left-associative; as such, @[]@ is a poor choice of monoid for computations that entail many calls to 'add'.
The [Seq](http://hackage.haskell.org/package/containersdocs/Data-Sequence.html) or [DList](http://hackage.haskell.org/package/dlist) monoids may be a superior choice.
This carrier also uses an 'IORef' to store its accumulator, which allows it a 'MonadUnliftIO' instance, but precludes backtracking when run in conjunction with 'Control.Effect.NonDet'.

@since 1.1.2.0
-}

module Control.Carrier.Accum.IORef
( -- * Accum carrier
  runAccum
, execAccum
, evalAccum
, AccumC(AccumC)
  -- * Accum effect
, 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

-- | Run an 'Accum' effect with a 'Semigroup'-based log.
--
-- @
-- 'runAccum' w0 ('pure' a) = 'pure' (w0, a)
-- @
-- @
-- 'runAccum' w0 ('add' w) = 'pure' (w0 <> w, ())
-- @
-- @
-- 'runAccum' w0 ('add' w >> 'look') = 'pure' (w0 <> w, w0 <> w)
-- @
--
-- @since 1.1.2.0
runAccum :: MonadIO m => w -> AccumC w m a -> m (w, a)
runAccum :: forall (m :: * -> *) w a.
MonadIO m =>
w -> AccumC w m a -> m (w, a)
runAccum w
start AccumC w m a
go = do
  ref <- IO (IORef w) -> m (IORef w)
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (w -> IO (IORef w)
forall a. a -> IO (IORef a)
newIORef w
start)
  result <- runReader ref . runAccumC $ go
  final <- liftIO (readIORef ref)
  pure (final, result)
{-# INLINE runAccum #-}

-- | Run a 'Accum' effect with a 'Semigroup'-based log,
--   producing the final log and discarding the result value.
--
-- @
-- 'execAccum' w = 'fmap' 'fst' . 'runAccum' w
-- @
--
-- @since 1.1.2.0
execAccum :: MonadIO m => w -> AccumC w m a -> m w
execAccum :: forall (m :: * -> *) w a. MonadIO m => w -> AccumC w m a -> m w
execAccum w
w = ((w, a) -> w) -> m (w, a) -> m w
forall a b. (a -> b) -> m a -> m b
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 #-}

-- | Run a 'Accum' effect with a 'Semigroup'-based log,
--   producing the result value and discarding the final log.
--
-- @
-- 'evalAccum' w = 'fmap' 'snd' . 'runAccum' w
-- @
--
-- @since 1.1.2.0
evalAccum :: MonadIO m => w -> AccumC w m a -> m a
evalAccum :: forall (m :: * -> *) w a. MonadIO m => w -> AccumC w m a -> m a
evalAccum w
w = ((w, a) -> a) -> m (w, a) -> m a
forall a b. (a -> b) -> m a -> m b
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 #-}

-- | @since 1.1.2.0
newtype AccumC w m a = AccumC { forall w (m :: * -> *) a. AccumC w m a -> ReaderC (IORef w) m a
runAccumC :: ReaderC (IORef w) m a }
  deriving (Applicative (AccumC w m)
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)
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
$cempty :: forall w (m :: * -> *) a. Alternative m => AccumC w m a
empty :: forall a. AccumC w m a
$c<|> :: forall w (m :: * -> *) a.
Alternative m =>
AccumC w m a -> AccumC w m a -> AccumC w m a
<|> :: forall a. AccumC w m a -> AccumC w m a -> AccumC w m a
$csome :: forall w (m :: * -> *) a.
Alternative m =>
AccumC w m a -> AccumC w m [a]
some :: forall a. AccumC w m a -> AccumC w m [a]
$cmany :: forall w (m :: * -> *) a.
Alternative m =>
AccumC w m a -> AccumC w m [a]
many :: forall a. AccumC w m a -> AccumC w m [a]
Alternative, Functor (AccumC w m)
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)
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
$cpure :: forall w (m :: * -> *) a. Applicative m => a -> AccumC w m a
pure :: forall a. a -> AccumC w m a
$c<*> :: forall w (m :: * -> *) a b.
Applicative m =>
AccumC w m (a -> b) -> AccumC w m a -> AccumC w m b
<*> :: forall a b. AccumC w m (a -> b) -> AccumC w m a -> AccumC w m b
$cliftA2 :: forall w (m :: * -> *) a b c.
Applicative m =>
(a -> b -> c) -> AccumC w m a -> AccumC w m b -> AccumC w m c
liftA2 :: forall a b c.
(a -> b -> c) -> AccumC w m a -> AccumC w m b -> AccumC w m c
$c*> :: forall w (m :: * -> *) a b.
Applicative m =>
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 b
$c<* :: forall w (m :: * -> *) a b.
Applicative m =>
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 a
Applicative, (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
$cfmap :: forall w (m :: * -> *) a b.
Functor m =>
(a -> b) -> AccumC w m a -> AccumC w m b
fmap :: forall a b. (a -> b) -> AccumC w m a -> AccumC w m b
$c<$ :: forall w (m :: * -> *) a b.
Functor m =>
a -> AccumC w m b -> AccumC w m a
<$ :: forall a b. a -> AccumC w m b -> AccumC w m a
Functor, Applicative (AccumC w m)
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)
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
$c>>= :: forall w (m :: * -> *) a b.
Monad m =>
AccumC w m a -> (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
$c>> :: forall w (m :: * -> *) a b.
Monad m =>
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 b
$creturn :: forall w (m :: * -> *) a. Monad m => a -> AccumC w m a
return :: forall a. a -> AccumC w m a
Monad, Monad (AccumC w m)
Monad (AccumC w m) =>
(forall a. String -> AccumC w m a) -> MonadFail (AccumC w m)
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
$cfail :: forall w (m :: * -> *) a. MonadFail m => String -> AccumC w m a
fail :: forall a. String -> AccumC w m a
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)
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
$cmfix :: forall w (m :: * -> *) a.
MonadFix m =>
(a -> AccumC w m a) -> AccumC w m a
mfix :: forall a. (a -> AccumC w m a) -> AccumC w m a
MonadFix, Monad (AccumC w m)
Monad (AccumC w m) =>
(forall a. IO a -> AccumC w m a) -> MonadIO (AccumC w m)
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
$cliftIO :: forall w (m :: * -> *) a. MonadIO m => IO a -> AccumC w m a
liftIO :: forall a. IO a -> AccumC w m a
MonadIO, Monad (AccumC w m)
Alternative (AccumC w m)
(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)
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
$cmzero :: forall w (m :: * -> *) a. (Alternative m, Monad m) => AccumC w m a
mzero :: forall 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
mplus :: forall a. AccumC w m a -> AccumC w m a -> AccumC w m a
MonadPlus, (forall (m :: * -> *). Monad m => Monad (AccumC w m)) =>
(forall (m :: * -> *) a. Monad m => m a -> AccumC w m a)
-> MonadTrans (AccumC w)
forall w (m :: * -> *). Monad m => Monad (AccumC w m)
forall w (m :: * -> *) a. Monad m => m a -> AccumC w m a
forall (m :: * -> *). Monad m => Monad (AccumC w m)
forall (m :: * -> *) a. Monad m => m a -> AccumC w m a
forall (t :: (* -> *) -> * -> *).
(forall (m :: * -> *). Monad m => Monad (t m)) =>
(forall (m :: * -> *) a. Monad m => m a -> t m a) -> MonadTrans t
$clift :: forall w (m :: * -> *) a. Monad m => m a -> AccumC w m a
lift :: forall (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 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
$cwithRunInIO :: forall w (m :: * -> *) b.
MonadUnliftIO m =>
((forall a. AccumC w m a -> IO a) -> IO b) -> AccumC w m b
withRunInIO :: forall b.
((forall a. AccumC w m a -> IO a) -> IO b) -> AccumC w m b
MonadUnliftIO)

instance (Algebra sig m, S.Semigroup w, MonadIO m) => Algebra (Accum w :+: sig) (AccumC w m) where
  alg :: forall (ctx :: * -> *) (n :: * -> *) a.
Functor ctx =>
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
      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
ask @(IORef w))
      (<$ ctx) <$> case accum of
        Add w
w' -> IO a -> AccumC w m a
forall a. IO a -> AccumC w m a
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 a -> AccumC w m a
forall a. IO a -> AccumC w m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IORef a -> IO a
forall a. IORef a -> IO a
readIORef IORef w
IORef a
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 (ctx :: * -> *) (n :: * -> *) a.
Functor ctx =>
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 #-}