{-# 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
  IORef w
ref <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (forall a. a -> IO (IORef a)
newIORef w
start)
  a
result <- forall r (m :: * -> *) a. r -> ReaderC r m a -> m a
runReader IORef w
ref forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall w (m :: * -> *) a. AccumC w m a -> ReaderC (IORef w) m a
runAccumC forall a b. (a -> b) -> a -> b
$ AccumC w m a
go
  w
final <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (forall a. IORef a -> IO a
readIORef IORef w
ref)
  forall (f :: * -> *) a. Applicative f => a -> f a
pure (w
final, a
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 = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a b. (a, b) -> a
fst forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a b. (a, b) -> b
snd forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 (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 :: 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]
some :: forall a. AccumC w m a -> AccumC w m [a]
$csome :: forall w (m :: * -> *) a.
Alternative m =>
AccumC w m a -> AccumC w m [a]
<|> :: forall 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 :: forall a. AccumC w m a
$cempty :: forall w (m :: * -> *) a. Alternative m => AccumC w m a
Alternative, 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
<* :: forall a b. 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
*> :: 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 b
liftA2 :: forall a b c.
(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
<*> :: forall a b. 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 :: forall a. a -> AccumC w m a
$cpure :: forall w (m :: * -> *) a. Applicative m => a -> AccumC w m a
Applicative, 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
<$ :: forall a b. 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 :: forall a b. (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, 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 :: forall a. a -> AccumC w m a
$creturn :: forall w (m :: * -> *) a. Monad m => a -> AccumC w m a
>> :: forall a b. 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
>>= :: 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 -> (a -> AccumC w m b) -> AccumC w m b
Monad, 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 :: forall a. String -> AccumC w m a
$cfail :: forall w (m :: * -> *) a. MonadFail m => String -> AccumC w m a
Fail.MonadFail, 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 :: forall a. (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
MonadFix, 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 :: forall a. IO a -> AccumC w m a
$cliftIO :: forall w (m :: * -> *) a. MonadIO m => IO a -> AccumC w m a
MonadIO, 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 :: forall a. 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 :: forall a. AccumC w m a
$cmzero :: forall w (m :: * -> *) a. (Alternative m, Monad m) => AccumC w m a
MonadPlus, 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 :: forall (m :: * -> *) a. Monad m => m a -> AccumC w m a
$clift :: forall w (m :: * -> *) a. Monad m => m a -> AccumC w m a
MonadTrans, 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 b.
((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
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
      IORef w
ref <- 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))
      (forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ ctx ()
ctx) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> case Accum w n a
accum of
        Add w
w' -> forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (forall a. IORef a -> (a -> a) -> IO ()
modifyIORef' IORef w
ref (forall a. Semigroup a => a -> a -> a
S.<> w
w'))
        Accum w n a
Look   -> forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (forall a. IORef a -> IO a
readIORef IORef w
ref)
    R sig n a
other  -> forall w (m :: * -> *) a. ReaderC (IORef w) m a -> AccumC w m a
AccumC (forall (sig :: (* -> *) -> * -> *) (m :: * -> *) (ctx :: * -> *)
       (n :: * -> *) a.
(Algebra sig m, Functor ctx) =>
Handler ctx n m -> sig n a -> ctx () -> m (ctx a)
alg (forall w (m :: * -> *) a. AccumC w m a -> ReaderC (IORef w) m a
runAccumC forall b c a. (b -> c) -> (a -> b) -> a -> c
. Handler ctx n (AccumC w m)
hdl) (forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
       (m :: * -> *) k.
g m k -> (:+:) f g m k
R sig n a
other) ctx ()
ctx)
  {-# INLINE alg #-}