-- |-- Module : Data.Functor.Identity-- Copyright : (c) Andy Gill 2001,-- (c) Oregon Graduate Institute of Science and Technology 2001-- License : BSD-style (see the file LICENSE)---- Maintainer : ross@soi.city.ac.uk-- Stability : experimental-- Portability : portable---- The identity functor and monad.---- This trivial type constructor serves two purposes:---- * It can be used with functions parameterized by functor or monad classes.---- * It can be used as a base monad to which a series of monad-- transformers may be applied to construct a composite monad.-- Most monad transformer modules include the special case of-- applying the transformer to 'Identity'. For example, @State s@-- is an abbreviation for @StateT s 'Identity'@.
module Data.Functor.Identity (
Identity(..),
) where
import Control.Applicative
import Control.Monad
import Control.Monad.Fix
import Data.Foldable (Foldable(foldMap))
import Data.Traversable (Traversable(traverse))
-- | Identity functor and monad.
newtype Identitya = Identity { runIdentity :: a }
-- ----------------------------------------------------------------------------- Identity instances for Functor and Monad
instance FunctorIdentity where
fmapfm = Identity (f (runIdentitym))
instance FoldableIdentity where
foldMapf (Identityx) = fx
instance TraversableIdentity where
traversef (Identityx) = Identity<$>fx
instance ApplicativeIdentity where
purea = IdentityaIdentityf<*>Identityx = Identity (fx)
instance MonadIdentity where
returna = Identityam>>=k = k (runIdentitym)
instance MonadFixIdentity where
mfixf = Identity (fix (runIdentity.f))