{-# LANGUAGE CPP #-}
#if defined(__GLASGOW_HASKELL__) && __GLASGOW_HASKELL__ >= 702
{-# LANGUAGE Safe #-}
#endif
------------------------------------------------------------------------------- |-- Module : Data.Functor.Extend-- Copyright : (C) 2011 Edward Kmett-- License : BSD-style (see the file LICENSE)---- Maintainer : Edward Kmett <ekmett@gmail.com>-- Stability : provisional-- Portability : portable------------------------------------------------------------------------------
module Data.Functor.Extend
( -- * Extendable Functors-- $definition
Extend(..)
) where
import Prelude hiding (id, (.))
import Control.Category
import Control.Comonad.Trans.Env
import Control.Comonad.Trans.Store
import Control.Comonad.Trans.Traced
import Control.Monad.Trans.Identity
import Data.Functor.Coproduct
import Data.Functor.Identity
import Data.Semigroup
import Data.List (tails)
import Data.List.NonEmpty (NonEmpty(..), toList)
import Data.Sequence (Seq)
import qualified Data.Sequence as Seq
import Data.Tree
class Functor w =>Extendw where
-- |-- > duplicated = extended id-- > fmap (fmap f) . duplicated = duplicated . fmap fduplicated :: wa -> w (wa)
-- |-- > extended f = fmap f . duplicatedextended :: (wa -> b) -> wa -> wbextendedf = fmapf.duplicatedduplicated = extendedid-- * Extends for Prelude types:---- Instances: While Data.Functor.Extend.Instances would be symmetric-- to the definition of Control.Monad.Instances in base, the reason-- the latter exists is because of Haskell 98 specifying the types-- @'Either' a@, @((,)m)@ and @((->)e)@ and the class Monad without-- having the foresight to require or allow instances between them.---- Here Haskell 98 says nothing about Extend, so we can include the-- instances directly avoiding the wart of orphan instances.
instance Extend[] where
duplicated = init.tails
instance ExtendMaybe where
duplicatedNothing = Nothingduplicatedj = Justj
instance Extend (Eithera) where
duplicated (Lefta) = Leftaduplicatedr = Rightr
instance Extend ((,)e) where
duplicatedp = (fstp, p)
instance Semigroup m =>Extend ((->)m) where
duplicatedfm = f.(<>)m
instance ExtendSeq where
duplicatedl = Seq.take (Seq.lengthl) (Seq.tailsl)
instance ExtendTree where
duplicatedw@(Node _ as) = Nodew (mapduplicatedas)
instance (Extend f, Extend g) =>Extend (Coproductfg) where
extendedf = Coproduct.coproduct
(Left.extended (f.Coproduct.Left))
(Right.extended (f.Coproduct.Right))
instance Extend w =>Extend (EnvTew) where
duplicated (EnvTewa) = EnvTe (extended (EnvTe) wa)
instance Extend w =>Extend (StoreTsw) where
duplicated (StoreTwfs) = StoreT (extendedStoreTwf) sextendedf (StoreTwfs) = StoreT (extended (\wf's' -> f (StoreTwf's')) wf) s
instance (Extend w, Semigroup m) =>Extend (TracedTmw) where
extendedf = TracedT.extended (\wfm -> f (TracedT (fmap (.(<>)m) wf))) .runTracedT-- I can't fix the world-- instance (Monoid m, Extend n) => Extend (ReaderT m n)-- duplicate f m = f . mappend m-- * Extends for types from 'transformers'.---- This isn't really a transformer, so i have no compunction about including the instance here.---- TODO: Petition to move Data.Functor.Identity into base
instance ExtendIdentity where
duplicated = Identity-- Provided to avoid an orphan instance. Not proposed to standardize.-- If Extend moved to base, consider moving instance into transformers?
instance Extend w =>Extend (IdentityTw) where
extendedf (IdentityTm) = IdentityT (extended (f.IdentityT) m)
instance ExtendNonEmpty where
extendedfw@ ~(_ :| aas) = fw:| case aas of
[] -> []
(a:as) -> toList (extendedf (a:|as))
-- $definition-- There are two ways to define an 'Extend' instance:---- I. Provide definitions for 'extended'-- satisfying this law:---- > extended f . extended g = extended (f . extended g)---- II. Alternately, you may choose to provide definitions for 'duplicated'-- satisfying this law:---- > duplicated . duplicated = fmap duplicated . duplicated---- You may of course, choose to define both 'duplicated' /and/ 'extended'.-- In that case you must also satisfy these laws:---- > extended f = fmap f . duplicated-- > duplicated = extended id---- These are the default definitions of 'extended' and 'duplicated'.