#if __GLASGOW_HASKELL__ >= 702 && __GLASGOW_HASKELL <= 706 && defined(MIN_VERSION_comonad) && !(MIN_VERSION_comonad(3,0,3))
#endif
module Data.Functor.Extend
(
Extend(..)
) where
import Prelude hiding (id, (.))
import Control.Category
import Control.Monad.Trans.Identity
import Data.Functor.Identity
import Data.Semigroup
import Data.List (tails)
import Data.List.NonEmpty (NonEmpty(..), toList)
#ifdef MIN_VERSION_containers
import Data.Sequence (Seq)
import qualified Data.Sequence as Seq
import Data.Tree
#endif
#ifdef MIN_VERSION_comonad
import Data.Functor.Coproduct
import Control.Comonad.Trans.Env
import Control.Comonad.Trans.Store
import Control.Comonad.Trans.Traced
#endif
class Functor w => Extend w where
duplicated :: w a -> w (w a)
extended :: (w a -> b) -> w a -> w b
extended f = fmap f . duplicated
duplicated = extended id
#if __GLASGOW_HASKELL__ >= 708
#endif
instance Extend [] where
duplicated = init . tails
instance Extend Maybe where
duplicated Nothing = Nothing
duplicated j = Just j
instance Extend (Either a) where
duplicated (Left a) = Left a
duplicated r = Right r
instance Extend ((,)e) where
duplicated p = (fst p, p)
instance Semigroup m => Extend ((->)m) where
duplicated f m = f . (<>) m
#ifdef MIN_VERSION_containers
instance Extend Seq where
duplicated l = Seq.take (Seq.length l) (Seq.tails l)
instance Extend Tree where
duplicated w@(Node _ as) = Node w (map duplicated as)
#endif
#ifdef MIN_VERSION_comonad
instance (Extend f, Extend g) => Extend (Coproduct f g) where
extended f = Coproduct . coproduct
(Left . extended (f . Coproduct . Left))
(Right . extended (f . Coproduct . Right))
instance Extend w => Extend (EnvT e w) where
duplicated (EnvT e wa) = EnvT e (extended (EnvT e) wa)
instance Extend w => Extend (StoreT s w) where
duplicated (StoreT wf s) = StoreT (extended StoreT wf) s
extended f (StoreT wf s) = StoreT (extended (\wf' s' -> f (StoreT wf' s')) wf) s
instance (Extend w, Semigroup m) => Extend (TracedT m w) where
extended f = TracedT . extended (\wf m -> f (TracedT (fmap (. (<>) m) wf))) . runTracedT
#endif
instance Extend Identity where
duplicated = Identity
instance Extend w => Extend (IdentityT w) where
extended f (IdentityT m) = IdentityT (extended (f . IdentityT) m)
instance Extend NonEmpty where
extended f w@ ~(_ :| aas) = f w :| case aas of
[] -> []
(a:as) -> toList (extended f (a :| as))