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

-- | A carrier for the 'Trace' effect that prints all traced results to stderr.
--
-- @since 1.0.0.0
module Control.Carrier.Trace.Printing
( -- * Trace carrier
  runTrace
, TraceC(..)
  -- * Trace effect
, module Control.Effect.Trace
) where

import Control.Algebra
import Control.Applicative (Alternative)
import Control.Effect.Trace
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 System.IO

-- | Run a 'Trace' effect, printing traces to 'stderr'.
--
-- @
-- 'runTrace' ('trace' s) = 'liftIO' ('putStrLn' s)
-- @
-- @
-- 'runTrace' ('pure' a) = 'pure' a
-- @
--
-- @since 1.0.0.0
runTrace :: TraceC m a -> m a
runTrace :: forall (m :: * -> *) a. TraceC m a -> m a
runTrace (TraceC m a
m) = m a
m
{-# INLINE runTrace #-}

-- | @since 1.0.0.0
newtype TraceC m a = TraceC (m a)
  deriving (Applicative (TraceC m)
Applicative (TraceC m) =>
(forall a. TraceC m a)
-> (forall a. TraceC m a -> TraceC m a -> TraceC m a)
-> (forall a. TraceC m a -> TraceC m [a])
-> (forall a. TraceC m a -> TraceC m [a])
-> Alternative (TraceC m)
forall a. TraceC m a
forall a. TraceC m a -> TraceC m [a]
forall a. TraceC m a -> TraceC m a -> TraceC 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
forall (m :: * -> *). Alternative m => Applicative (TraceC m)
forall (m :: * -> *) a. Alternative m => TraceC m a
forall (m :: * -> *) a. Alternative m => TraceC m a -> TraceC m [a]
forall (m :: * -> *) a.
Alternative m =>
TraceC m a -> TraceC m a -> TraceC m a
$cempty :: forall (m :: * -> *) a. Alternative m => TraceC m a
empty :: forall a. TraceC m a
$c<|> :: forall (m :: * -> *) a.
Alternative m =>
TraceC m a -> TraceC m a -> TraceC m a
<|> :: forall a. TraceC m a -> TraceC m a -> TraceC m a
$csome :: forall (m :: * -> *) a. Alternative m => TraceC m a -> TraceC m [a]
some :: forall a. TraceC m a -> TraceC m [a]
$cmany :: forall (m :: * -> *) a. Alternative m => TraceC m a -> TraceC m [a]
many :: forall a. TraceC m a -> TraceC m [a]
Alternative, Functor (TraceC m)
Functor (TraceC m) =>
(forall a. a -> TraceC m a)
-> (forall a b. TraceC m (a -> b) -> TraceC m a -> TraceC m b)
-> (forall a b c.
    (a -> b -> c) -> TraceC m a -> TraceC m b -> TraceC m c)
-> (forall a b. TraceC m a -> TraceC m b -> TraceC m b)
-> (forall a b. TraceC m a -> TraceC m b -> TraceC m a)
-> Applicative (TraceC m)
forall a. a -> TraceC m a
forall a b. TraceC m a -> TraceC m b -> TraceC m a
forall a b. TraceC m a -> TraceC m b -> TraceC m b
forall a b. TraceC m (a -> b) -> TraceC m a -> TraceC m b
forall a b c.
(a -> b -> c) -> TraceC m a -> TraceC m b -> TraceC 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 (m :: * -> *). Applicative m => Functor (TraceC m)
forall (m :: * -> *) a. Applicative m => a -> TraceC m a
forall (m :: * -> *) a b.
Applicative m =>
TraceC m a -> TraceC m b -> TraceC m a
forall (m :: * -> *) a b.
Applicative m =>
TraceC m a -> TraceC m b -> TraceC m b
forall (m :: * -> *) a b.
Applicative m =>
TraceC m (a -> b) -> TraceC m a -> TraceC m b
forall (m :: * -> *) a b c.
Applicative m =>
(a -> b -> c) -> TraceC m a -> TraceC m b -> TraceC m c
$cpure :: forall (m :: * -> *) a. Applicative m => a -> TraceC m a
pure :: forall a. a -> TraceC m a
$c<*> :: forall (m :: * -> *) a b.
Applicative m =>
TraceC m (a -> b) -> TraceC m a -> TraceC m b
<*> :: forall a b. TraceC m (a -> b) -> TraceC m a -> TraceC m b
$cliftA2 :: forall (m :: * -> *) a b c.
Applicative m =>
(a -> b -> c) -> TraceC m a -> TraceC m b -> TraceC m c
liftA2 :: forall a b c.
(a -> b -> c) -> TraceC m a -> TraceC m b -> TraceC m c
$c*> :: forall (m :: * -> *) a b.
Applicative m =>
TraceC m a -> TraceC m b -> TraceC m b
*> :: forall a b. TraceC m a -> TraceC m b -> TraceC m b
$c<* :: forall (m :: * -> *) a b.
Applicative m =>
TraceC m a -> TraceC m b -> TraceC m a
<* :: forall a b. TraceC m a -> TraceC m b -> TraceC m a
Applicative, (forall a b. (a -> b) -> TraceC m a -> TraceC m b)
-> (forall a b. a -> TraceC m b -> TraceC m a)
-> Functor (TraceC m)
forall a b. a -> TraceC m b -> TraceC m a
forall a b. (a -> b) -> TraceC m a -> TraceC m b
forall (m :: * -> *) a b.
Functor m =>
a -> TraceC m b -> TraceC m a
forall (m :: * -> *) a b.
Functor m =>
(a -> b) -> TraceC m a -> TraceC 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 (m :: * -> *) a b.
Functor m =>
(a -> b) -> TraceC m a -> TraceC m b
fmap :: forall a b. (a -> b) -> TraceC m a -> TraceC m b
$c<$ :: forall (m :: * -> *) a b.
Functor m =>
a -> TraceC m b -> TraceC m a
<$ :: forall a b. a -> TraceC m b -> TraceC m a
Functor, Applicative (TraceC m)
Applicative (TraceC m) =>
(forall a b. TraceC m a -> (a -> TraceC m b) -> TraceC m b)
-> (forall a b. TraceC m a -> TraceC m b -> TraceC m b)
-> (forall a. a -> TraceC m a)
-> Monad (TraceC m)
forall a. a -> TraceC m a
forall a b. TraceC m a -> TraceC m b -> TraceC m b
forall a b. TraceC m a -> (a -> TraceC m b) -> TraceC m b
forall (m :: * -> *). Monad m => Applicative (TraceC m)
forall (m :: * -> *) a. Monad m => a -> TraceC m a
forall (m :: * -> *) a b.
Monad m =>
TraceC m a -> TraceC m b -> TraceC m b
forall (m :: * -> *) a b.
Monad m =>
TraceC m a -> (a -> TraceC m b) -> TraceC 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 (m :: * -> *) a b.
Monad m =>
TraceC m a -> (a -> TraceC m b) -> TraceC m b
>>= :: forall a b. TraceC m a -> (a -> TraceC m b) -> TraceC m b
$c>> :: forall (m :: * -> *) a b.
Monad m =>
TraceC m a -> TraceC m b -> TraceC m b
>> :: forall a b. TraceC m a -> TraceC m b -> TraceC m b
$creturn :: forall (m :: * -> *) a. Monad m => a -> TraceC m a
return :: forall a. a -> TraceC m a
Monad, Monad (TraceC m)
Monad (TraceC m) =>
(forall a. String -> TraceC m a) -> MonadFail (TraceC m)
forall a. String -> TraceC m a
forall (m :: * -> *).
Monad m =>
(forall a. String -> m a) -> MonadFail m
forall (m :: * -> *). MonadFail m => Monad (TraceC m)
forall (m :: * -> *) a. MonadFail m => String -> TraceC m a
$cfail :: forall (m :: * -> *) a. MonadFail m => String -> TraceC m a
fail :: forall a. String -> TraceC m a
Fail.MonadFail, Monad (TraceC m)
Monad (TraceC m) =>
(forall a. (a -> TraceC m a) -> TraceC m a) -> MonadFix (TraceC m)
forall a. (a -> TraceC m a) -> TraceC m a
forall (m :: * -> *).
Monad m =>
(forall a. (a -> m a) -> m a) -> MonadFix m
forall (m :: * -> *). MonadFix m => Monad (TraceC m)
forall (m :: * -> *) a.
MonadFix m =>
(a -> TraceC m a) -> TraceC m a
$cmfix :: forall (m :: * -> *) a.
MonadFix m =>
(a -> TraceC m a) -> TraceC m a
mfix :: forall a. (a -> TraceC m a) -> TraceC m a
MonadFix, Monad (TraceC m)
Monad (TraceC m) =>
(forall a. IO a -> TraceC m a) -> MonadIO (TraceC m)
forall a. IO a -> TraceC m a
forall (m :: * -> *).
Monad m =>
(forall a. IO a -> m a) -> MonadIO m
forall (m :: * -> *). MonadIO m => Monad (TraceC m)
forall (m :: * -> *) a. MonadIO m => IO a -> TraceC m a
$cliftIO :: forall (m :: * -> *) a. MonadIO m => IO a -> TraceC m a
liftIO :: forall a. IO a -> TraceC m a
MonadIO, Monad (TraceC m)
Alternative (TraceC m)
(Alternative (TraceC m), Monad (TraceC m)) =>
(forall a. TraceC m a)
-> (forall a. TraceC m a -> TraceC m a -> TraceC m a)
-> MonadPlus (TraceC m)
forall a. TraceC m a
forall a. TraceC m a -> TraceC m a -> TraceC m a
forall (m :: * -> *). MonadPlus m => Monad (TraceC m)
forall (m :: * -> *). MonadPlus m => Alternative (TraceC m)
forall (m :: * -> *) a. MonadPlus m => TraceC m a
forall (m :: * -> *) a.
MonadPlus m =>
TraceC m a -> TraceC m a -> TraceC m a
forall (m :: * -> *).
(Alternative m, Monad m) =>
(forall a. m a) -> (forall a. m a -> m a -> m a) -> MonadPlus m
$cmzero :: forall (m :: * -> *) a. MonadPlus m => TraceC m a
mzero :: forall a. TraceC m a
$cmplus :: forall (m :: * -> *) a.
MonadPlus m =>
TraceC m a -> TraceC m a -> TraceC m a
mplus :: forall a. TraceC m a -> TraceC m a -> TraceC m a
MonadPlus)

instance MonadTrans TraceC where
  lift :: forall (m :: * -> *) a. Monad m => m a -> TraceC m a
lift = m a -> TraceC m a
forall (m :: * -> *) a. m a -> TraceC m a
TraceC
  {-# INLINE lift #-}

instance (MonadIO m, Algebra sig m) => Algebra (Trace :+: sig) (TraceC m) where
  alg :: forall (ctx :: * -> *) (n :: * -> *) a.
Functor ctx =>
Handler ctx n (TraceC m)
-> (:+:) Trace sig n a -> ctx () -> TraceC m (ctx a)
alg Handler ctx n (TraceC m)
hdl (:+:) Trace sig n a
sig ctx ()
ctx = case (:+:) Trace sig n a
sig of
    L (Trace String
s) -> ctx a
ctx ()
ctx ctx a -> TraceC m () -> TraceC m (ctx a)
forall a b. a -> TraceC m b -> TraceC m a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ IO () -> TraceC m ()
forall a. IO a -> TraceC m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (Handle -> String -> IO ()
hPutStrLn Handle
stderr String
s)
    R sig n a
other     -> m (ctx a) -> TraceC m (ctx a)
forall (m :: * -> *) a. m a -> TraceC m a
TraceC (Handler ctx n m -> sig n a -> ctx () -> m (ctx a)
forall (ctx :: * -> *) (n :: * -> *) a.
Functor ctx =>
Handler ctx n m -> sig n a -> ctx () -> 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 (TraceC m (ctx x) -> m (ctx x)
forall (m :: * -> *) a. TraceC m a -> m a
runTrace (TraceC m (ctx x) -> m (ctx x))
-> (ctx (n x) -> TraceC m (ctx x)) -> ctx (n x) -> m (ctx x)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ctx (n x) -> TraceC m (ctx x)
Handler ctx n (TraceC m)
hdl) sig n a
other ctx ()
ctx)
  {-# INLINE alg #-}