module Control.Comonad.Trans.Traced
(
Traced
, traced
, runTraced
, TracedT(..)
, trace
, listen
, listens
, censor
) where
import Control.Comonad
import Control.Comonad.Apply
import Control.Comonad.Hoist.Class
import Control.Comonad.Trans.Class
import Data.Functor
import Data.Functor.Apply
import Data.Functor.Identity
import Data.Monoid
import Data.Semigroup
import Data.Typeable
type Traced m = TracedT m Identity
traced :: (m -> a) -> Traced m a
traced f = TracedT (Identity f)
runTraced :: Monoid m => Traced m a -> m -> a
runTraced (TracedT (Identity f)) = f
newtype TracedT m w a = TracedT { runTracedT :: w (m -> a) }
instance Functor w => Functor (TracedT m w) where
fmap g = TracedT . fmap (g .) . runTracedT
instance (Extend w, Semigroup m) => Extend (TracedT m w) where
extend f = TracedT . extend (\wf m -> f (TracedT (fmap (. (<>) m) wf))) . runTracedT
instance (Comonad w, Semigroup m, Monoid m) => Comonad (TracedT m w) where
extract (TracedT wf) = extract wf mempty
instance (Semigroup m, Monoid m) => ComonadTrans (TracedT m) where
lower = fmap ($mempty) . runTracedT
instance (Semigroup m, Monoid m) => ComonadHoist (TracedT m) where
cohoist = traced . extract . runTracedT
instance (Apply w, Semigroup m, Monoid m) => Apply (TracedT m w) where
TracedT wf <.> TracedT wa = TracedT ((\mf ma m -> (mf m) (ma m)) <$> wf <.> wa)
instance (ComonadApply w, Semigroup m, Monoid m) => ComonadApply (TracedT m w)
trace :: (Comonad w, Monoid m) => m -> TracedT m w a -> a
trace m (TracedT wf) = extract wf m
listen :: Functor w => TracedT m w a -> TracedT m w (a, m)
listen = TracedT . fmap (\f m -> (f m, m)) . runTracedT
listens :: Functor w => (m -> b) -> TracedT m w a -> TracedT m w (a, b)
listens g = TracedT . fmap (\f m -> (f m, g m)) . runTracedT
censor :: Functor w => (m -> m) -> TracedT m w a -> TracedT m w a
censor g = TracedT . fmap (. g) . runTracedT
#ifdef __GLASGOW_HASKELL__
instance (Typeable s, Typeable1 w) => Typeable1 (TracedT s w) where
typeOf1 dswa = mkTyConApp tracedTTyCon [typeOf (s dswa), typeOf1 (w dswa)]
where
s :: TracedT s w a -> s
s = undefined
w :: TracedT s w a -> w a
w = undefined
tracedTTyCon :: TyCon
tracedTTyCon = mkTyCon "Control.Comonad.Trans.Traced.TracedT"
#endif