comonad-transformers 3.0.1 → 3.0.2
raw patch · 4 files changed
+140/−22 lines, 4 filesdep ~basePVP: major bump suggested
API removals or changes: PVP suggests a major version bump
Dependency ranges changed: base
API changes (from Hackage documentation)
- Control.Comonad.Trans.Env: local :: (e -> e) -> EnvT e w a -> EnvT e w a
+ Control.Comonad.Trans.Env: local :: (e -> e') -> EnvT e w a -> EnvT e' w a
Files
- comonad-transformers.cabal +1/−1
- src/Control/Comonad/Trans/Env.hs +49/−6
- src/Control/Comonad/Trans/Store.hs +73/−11
- src/Control/Comonad/Trans/Traced.hs +17/−4
comonad-transformers.cabal view
@@ -1,6 +1,6 @@ name: comonad-transformers category: Control, Comonads-version: 3.0.1+version: 3.0.2 license: BSD3 cabal-version: >= 1.6 license-file: LICENSE
src/Control/Comonad/Trans/Env.hs view
@@ -1,19 +1,45 @@ {-# LANGUAGE CPP #-} {-# LANGUAGE FlexibleContexts #-}+#if __GLASGOW_HASKELL__ >= 707+{-# LANGUAGE StandaloneDeriving, DeriveDataTypeable #-}+#endif ----------------------------------------------------------------------------- -- | -- Module : Control.Comonad.Trans.Env--- Copyright : (C) 2008-2011 Edward Kmett+-- Copyright : (C) 2008-2013 Edward Kmett -- License : BSD-style (see the file LICENSE) -- -- Maintainer : Edward Kmett <ekmett@gmail.com> -- Stability : provisional -- Portability : portable ----- The environment comonad transformer (aka coreader).--- This adds an extra value that can be accessed in the environment.+-- The environment comonad holds a value along with some retrievable context. ----- Left adjoint to the reader comonad.+-- This module specifies the environment comonad transformer (aka coreader),+-- which is left adjoint to the reader comonad.+--+-- The following sets up an experiment that retains its initial value in the+-- background:+--+-- >>> let initial = env 0 0+--+-- Extract simply retrieves the value:+--+-- >>> extract initial+-- 0+--+-- Play around with the value, in our case producing a negative value:+--+-- >>> let experiment = fmap (+ 10) initial+-- >>> extract experiment+-- 10+--+-- Oh noes, something went wrong, 10 isn't very negative! Better restore the+-- initial value using the default:+--+-- >>> let initialRestored = experiment =>> ask+-- >>> extract initialRestored+-- 0 ---------------------------------------------------------------------------- module Control.Comonad.Trans.Env (@@ -42,8 +68,14 @@ import Data.Semigroup #ifdef __GLASGOW_HASKELL__+#if __GLASGOW_HASKELL__ >= 707+#define Typeable1 Typeable+#endif import Data.Data +#if __GLASGOW_HASKELL__ >= 707+deriving instance Typeable EnvT+#else instance (Typeable s, Typeable1 w) => Typeable1 (EnvT s w) where typeOf1 dswa = mkTyConApp envTTyCon [typeOf (s dswa), typeOf1 (w dswa)] where@@ -51,6 +83,7 @@ s = undefined w :: EnvT s w a -> w a w = undefined+#endif envTTyCon :: TyCon #if __GLASGOW_HASKELL__ < 704@@ -60,8 +93,10 @@ #endif {-# NOINLINE envTTyCon #-} +#if __GLASGOW_HASKELL__ < 707 instance (Typeable s, Typeable1 w, Typeable a) => Typeable (EnvT s w a) where typeOf = typeOfDefault+#endif instance ( Data e@@ -89,6 +124,8 @@ type Env e = EnvT e Identity data EnvT e w a = EnvT e (w a) ++-- | Create an Env using an environment and a value env :: e -> a -> Env e a env e a = EnvT e (Identity a) @@ -111,6 +148,8 @@ instance ComonadTrans (EnvT e) where lower (EnvT _ wa) = wa +-- | Gets rid of the environment. This differs from 'extract' in that it will+-- not continue extracting the value from the contained comonad. lowerEnvT :: EnvT e w a -> w a lowerEnvT (EnvT _ wa) = wa @@ -129,12 +168,16 @@ instance Traversable w => Traversable (EnvT e w) where traverse f (EnvT e w) = EnvT e <$> traverse f w +-- | Retrieves the environment. ask :: EnvT e w a -> e ask (EnvT e _) = e +-- | Like 'ask', but modifies the resulting value with a function.+--+-- > asks = f . ask asks :: (e -> f) -> EnvT e w a -> f asks f (EnvT e _) = f e -local :: (e -> e) -> EnvT e w a -> EnvT e w a+-- | Modifies the environment using the specified function.+local :: (e -> e') -> EnvT e w a -> EnvT e' w a local f (EnvT e wa) = EnvT (f e) wa-
src/Control/Comonad/Trans/Store.hs view
@@ -1,16 +1,46 @@ {-# LANGUAGE CPP #-}+#if __GLASGOW_HASKELL__ >= 707+{-# LANGUAGE StandaloneDeriving, DeriveDataTypeable #-}+#endif ----------------------------------------------------------------------------- -- | -- Module : Control.Comonad.Trans.Store--- Copyright : (C) 2008-2011 Edward Kmett+-- Copyright : (C) 2008-2013 Edward Kmett -- License : BSD-style (see the file LICENSE) -- -- Maintainer : Edward Kmett <ekmett@gmail.com> -- Stability : provisional -- Portability : portable ----- The strict store (state-in-context/costate) comonad transformer is subject to the laws: --+-- The store comonad holds a constant value along with a modifiable /accessor/+-- function, which maps the /stored value/ to the /focus/.+--+-- This module defines the strict store (aka state-in-context/costate) comonad+-- transformer.+--+-- @stored value = (1, 5)@, @accessor = fst@, @resulting focus = 1@:+--+-- > storeTuple :: Store (Int, Int) Int+-- > storeTuple = store fst (1, 5)+--+-- Add something to the focus:+--+-- > addToFocus :: Int -> Store (Int, Int) Int+-- > addToFocus x wa = x + extract wa+-- > added3 = extend (addToFocus 3) storeTuple+--+-- The focus of added3 is now @1 + 3 = 4@. However, this action changed only+-- the accessor function and therefore the focus but not the stored value:+--+-- >>> pos added3+-- (1, 5)+-- >>> extract added3+-- 4+--+-- The strict store (state-in-context/costate) comonad transformer is subject+-- to the laws:+-- -- > x = seek (pos x) x -- > y = pos (seek y x) -- > seek y x = seek y (seek z x)@@ -42,6 +72,10 @@ #ifdef __GLASGOW_HASKELL__ import Data.Typeable++#if __GLASGOW_HASKELL__ >= 707+deriving instance Typeable StoreT+#else instance (Typeable s, Typeable1 w) => Typeable1 (StoreT s w) where typeOf1 dswa = mkTyConApp storeTTyCon [typeOf (s dswa), typeOf1 (w dswa)] where@@ -62,8 +96,11 @@ {-# NOINLINE storeTTyCon #-} #endif +#endif+ type Store s = StoreT s Identity +-- | Create a Store using an accessor function and a stored value store :: (s -> a) -> s -> Store s a store f s = StoreT (Identity f) s @@ -75,9 +112,6 @@ runStoreT :: StoreT s w a -> (w (s -> a), s) runStoreT (StoreT wf s) = (wf, s) -experiment :: (Comonad w, Functor f) => (s -> f s) -> StoreT s w a -> f a-experiment f (StoreT wf s) = extract wf <$> f s- instance Functor w => Functor (StoreT s w) where fmap f (StoreT wf s) = StoreT (fmap (f .) wf) s @@ -101,34 +135,62 @@ extract (StoreT wf s) = extract wf s instance ComonadTrans (StoreT s) where- lower (StoreT f s) = fmap ($s) f+ lower (StoreT f s) = fmap ($ s) f instance ComonadHoist (StoreT s) where cohoist (StoreT f s) = StoreT (Identity (extract f)) s --- | Read the current position+-- | Read the stored value+--+-- >>> pos $ store fst (1,5)+-- (1,5)+-- pos :: StoreT s w a -> s pos (StoreT _ s) = s --- | Seek to an absolute location+-- | Set the stored value --+-- >>> pos . seek (3,7) $ store fst (1,5)+-- (3,7)+--+-- Seek satisfies the law+-- -- > seek s = peek s . duplicate seek :: Comonad w => s -> StoreT s w a -> StoreT s w a seek s ~(StoreT f _) = StoreT f s --- | Seek to a relative location+-- | Modify the stored value --+-- >>> pos . seeks swap $ store fst (1,5)+-- (5,1)+--+-- Seeks satisfies the law+-- -- > seeks f = peeks f . duplicate seeks :: Comonad w => (s -> s) -> StoreT s w a -> StoreT s w a seeks f ~(StoreT g s) = StoreT g (f s) --- | Peek at a value at a given absolute location+-- | Peek at what the current focus would be for a different stored value --+-- Peek satisfies the law+-- -- > peek x . extend (peek y) = peek y peek :: Comonad w => s -> StoreT s w a -> a peek s (StoreT g _) = extract g s --- | Peek at a value at a given relative location++-- | Peek at what the current focus would be if the stored value was+-- modified by some function peeks :: Comonad w => (s -> s) -> StoreT s w a -> a peeks f ~(StoreT g s) = extract g (f s) +-- | Applies a functor-valued function to the stored value, and then uses the+-- new accessor to read the resulting focus.+--+-- >>> let f x = if x > 0 then Just (x^2) else Nothing+-- >>> experiment f $ store (+1) 2+-- Just 5+-- >>> experiment f $ store (+1) (-2)+-- Nothing+experiment :: (Comonad w, Functor f) => (s -> f s) -> StoreT s w a -> f a+experiment f (StoreT wf s) = extract wf <$> f s
src/Control/Comonad/Trans/Traced.hs view
@@ -1,15 +1,23 @@+{-# LANGUAGE CPP #-}+#if __GLASGOW_HASKELL__ >= 707+{-# LANGUAGE StandaloneDeriving, DeriveDataTypeable #-}+#endif ----------------------------------------------------------------------------- -- | -- Module : Control.Comonad.Trans.Traced--- Copyright : (C) 2008-2011 Edward Kmett+-- Copyright : (C) 2008-2013 Edward Kmett -- License : BSD-style (see the file LICENSE) -- -- Maintainer : Edward Kmett <ekmett@gmail.com> -- Stability : provisional -- Portability : portable ----- The trace comonad transformer (aka the cowriter or exponential comonad transformer).+-- The trace comonad builds up a result by prepending monoidal values to each+-- other. --+-- This module specifies the traced comonad transformer (aka the cowriter or+-- exponential comonad transformer).+-- ---------------------------------------------------------------------------- module Control.Comonad.Trans.Traced (@@ -70,13 +78,13 @@ extract (TracedT wf) = extract wf mempty instance Monoid m => ComonadTrans (TracedT m) where- lower = fmap ($mempty) . runTracedT+ lower = fmap ($ mempty) . runTracedT instance Monoid m => ComonadHoist (TracedT m) where cohoist = traced . extract . runTracedT instance Distributive w => Distributive (TracedT m w) where- distribute = TracedT . fmap (\tma m -> fmap ($m) tma) . collect runTracedT+ distribute = TracedT . fmap (\tma m -> fmap ($ m) tma) . collect runTracedT trace :: Comonad w => m -> TracedT m w a -> a trace m (TracedT wf) = extract wf m@@ -92,6 +100,9 @@ #ifdef __GLASGOW_HASKELL__ +#if __GLASGOW_HASKELL__ >= 707+deriving instance Typeable TracedT+#else instance (Typeable s, Typeable1 w) => Typeable1 (TracedT s w) where typeOf1 dswa = mkTyConApp tracedTTyCon [typeOf (s dswa), typeOf1 (w dswa)] where@@ -107,5 +118,7 @@ tracedTTyCon = mkTyCon3 "comonad-transformers" "Control.Comonad.Trans.Traced" "TracedT" #endif {-# NOINLINE tracedTTyCon #-}++#endif #endif