{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE DeriveGeneric #-}

{- | An effect that provides a record of 'String' values ("traces") aggregate during the execution of a given computation.

Predefined carriers:

* "Control.Carrier.Trace.Printing", which logs to stderr in a 'Control.Monad.IO.Class.MonadIO' context.
* "Control.Carrier.Trace.Returning", which aggregates all traces in a @[String].
* "Control.Carrier.Trace.Ignoring", which discards all traced values.

@since 0.1.0.0
-}

module Control.Effect.Trace
( -- * Trace effect
  Trace(..)
, trace
  -- * Re-exports
, Algebra
, Effect
, Has
, run
) where

import Control.Algebra
import GHC.Generics (Generic1)

-- | @since 0.1.0.0
data Trace m k = Trace
  { Trace m k -> String
traceMessage :: String
  , Trace m k -> m k
traceCont    :: m k
  }
  deriving (a -> Trace m b -> Trace m a
(a -> b) -> Trace m a -> Trace m b
(forall a b. (a -> b) -> Trace m a -> Trace m b)
-> (forall a b. a -> Trace m b -> Trace m a) -> Functor (Trace m)
forall a b. a -> Trace m b -> Trace m a
forall a b. (a -> b) -> Trace m a -> Trace m b
forall (m :: * -> *) a b. Functor m => a -> Trace m b -> Trace m a
forall (m :: * -> *) a b.
Functor m =>
(a -> b) -> Trace m a -> Trace m b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> Trace m b -> Trace m a
$c<$ :: forall (m :: * -> *) a b. Functor m => a -> Trace m b -> Trace m a
fmap :: (a -> b) -> Trace m a -> Trace m b
$cfmap :: forall (m :: * -> *) a b.
Functor m =>
(a -> b) -> Trace m a -> Trace m b
Functor, (forall a. Trace m a -> Rep1 (Trace m) a)
-> (forall a. Rep1 (Trace m) a -> Trace m a) -> Generic1 (Trace m)
forall a. Rep1 (Trace m) a -> Trace m a
forall a. Trace m a -> Rep1 (Trace m) a
forall k (f :: k -> *).
(forall (a :: k). f a -> Rep1 f a)
-> (forall (a :: k). Rep1 f a -> f a) -> Generic1 f
forall (m :: * -> *) a. Rep1 (Trace m) a -> Trace m a
forall (m :: * -> *) a. Trace m a -> Rep1 (Trace m) a
$cto1 :: forall (m :: * -> *) a. Rep1 (Trace m) a -> Trace m a
$cfrom1 :: forall (m :: * -> *) a. Trace m a -> Rep1 (Trace m) a
Generic1)

instance HFunctor Trace
instance Effect   Trace

-- | Append a message to the trace log.
--
-- @since 0.1.0.0
trace :: Has Trace sig m => String -> m ()
trace :: String -> m ()
trace message :: String
message = Trace m () -> m ()
forall (eff :: (* -> *) -> * -> *) (sig :: (* -> *) -> * -> *)
       (m :: * -> *) a.
(Member eff sig, Algebra sig m) =>
eff m a -> m a
send (String -> m () -> Trace m ()
forall (m :: * -> *) k. String -> m k -> Trace m k
Trace String
message (() -> m ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()))