/
Trans.purs
36 lines (26 loc) · 1.35 KB
/
Trans.purs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
-- | This module defines the cowriter comonad transformer, `TracedT`.
module Control.Comonad.Traced.Trans where
import Prelude
import Control.Comonad (class Comonad, extract)
import Control.Comonad.Trans.Class (class ComonadTrans)
import Control.Extend (class Extend, (<<=))
import Data.Newtype (class Newtype)
-- | The cowriter comonad transformer.
-- |
-- | This comonad transformer extends the context of a value in the base comonad so that the value
-- | depends on a monoidal position of type `t`.
-- |
-- | The `ComonadTraced` type class describes the operations supported by this comonad.
newtype TracedT t w a = TracedT (w (t -> a))
-- | Unwrap a value in the `TracedT` comonad.
runTracedT :: forall w a t. TracedT t w a -> w (t -> a)
runTracedT (TracedT w) = w
derive instance newtypeTracedT :: Newtype (TracedT t w a) _
instance functorTracedT :: Functor w => Functor (TracedT t w) where
map f (TracedT w) = TracedT ((\g t -> f $ g t) <$> w)
instance extendTracedT :: (Extend w, Semigroup t) => Extend (TracedT t w) where
extend f (TracedT w) = TracedT ((\w' t -> f $ TracedT ((\h t' -> h $ t <> t') <$> w')) <<= w)
instance comonadTracedT :: (Comonad w, Monoid t) => Comonad (TracedT t w) where
extract (TracedT w) = extract w mempty
instance comonadTransTracedT :: Monoid t => ComonadTrans (TracedT t) where
lower (TracedT w) = (\f -> f mempty) <$> w