A type f is a Functor if it provides a function fmap which, given any types a and b
lets you apply any function from (a -> b) to turn an f a into an f b, preserving the
structure of f. Furthermore f needs to adhere to the following:
Note, that the second law follows from the free theorem of the type fmap and the first law, so you need only check that the former condition holds. See these articles by School of Haskell or David Luposchainsky for an explanation.
Methods
fmap :: (a -> b) -> f a -> f bfmap is used to apply a function of type
(a -> b)to a value of typef a, where f is a functor, to produce a value of typef b. Note that for any type constructor with more than one parameter (e.g.,Either), only the last type parameter can be modified with fmap (e.g.,bin `Either a b`).Some type constructors with two parameters or more have a
instance that allows both the last and the penultimate parameters to be mapped over.Data.BifunctorExamples
Convert from a
Maybe Intto aMaybe Stringusing show:Example2 expressions fmap show NothingNothingfmap show (Just 3)Just "3"
Convert from an
Either Int Intto anEither Int Stringusing show:Example2 expressions fmap show (Left 17)Left 17fmap show (Right 17)Right "17"
Double each element of a list:
Example1 expression fmap (*2) [1,2,3][2,4,6]
Apply even to the second element of a pair:
Example1 expression fmap even (2,2)(2,True)
It may seem surprising that the function is only applied to the last element of the tuple compared to the list example above which applies it to every element in the list. To understand, remember that tuples are type constructors with multiple type parameters: a tuple of 3 elements
(a,b,c)can also be written(,,) a b cand itsFunctorinstance is defined forFunctor ((,,) a b)(i.e., only the third parameter is free to be mapped over withfmap).It explains why
fmapcan be used with tuples containing values of different types as in the following example:Example1 expression fmap even ("hello", 1.0, 4)("hello",1.0,True)
(<$) :: a -> f b -> f ainfixl 4Replace all locations in the input with the same value. The default definition is
fmap . const, but this may be overridden with a more efficient version.Examples
Perform a computation with Maybe and replace the result with a constant value if it is Just:
Example2 expressions 'a' <$ Just 2Just 'a''a' <$ NothingNothing
Instances100Functor, …
Functor ComplexDefined in base-4.20.2.0 · Data.ComplexFunctor FirstDefined in base-4.20.2.0 · Data.SemigroupFunctor LastDefined in base-4.20.2.0 · Data.SemigroupFunctor MaxDefined in base-4.20.2.0 · Data.SemigroupFunctor MinDefined in base-4.20.2.0 · Data.SemigroupFunctor ArgDescrDefined in base-4.20.2.0 · System.Console.GetOptFunctor ArgOrderDefined in base-4.20.2.0 · System.Console.GetOptFunctor OptDescrDefined in base-4.20.2.0 · System.Console.GetOptFunctor PutDefined in bytestring-0.12.2.0 · Data.ByteString.Builder.InternalFunctor NonEmptyDefined in ghc-internal-9.1003.0 · GHC.Internal.BaseFunctor STMDefined in ghc-internal-9.1003.0 · GHC.Internal.Conc.SyncFunctor HandlerDefined in ghc-internal-9.1003.0 · GHC.Internal.Control.ExceptionFunctor IdentityDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Functor.IdentityFunctor FirstDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.MonoidFunctor LastDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.MonoidFunctor DownDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.OrdFunctor DualDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalFunctor ProductDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalFunctor SumDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalFunctor ZipListDefined in ghc-internal-9.1003.0 · GHC.Internal.Functor.ZipListFunctor NoIODefined in ghc-internal-9.1003.0 · GHC.Internal.GHCiFunctor Par1Defined in ghc-internal-9.1003.0 · GHC.Internal.GenericsFunctor MaybeDefined in ghc-internal-9.1003.0 · GHC.Internal.BaseFunctor PDefined in ghc-internal-9.1003.0 · GHC.Internal.Text.ParserCombinators.ReadPFunctor ReadPDefined in ghc-internal-9.1003.0 · GHC.Internal.Text.ParserCombinators.ReadPFunctor ReadPrecDefined in ghc-internal-9.1003.0 · GHC.Internal.Text.ParserCombinators.ReadPrecFunctor SoloDefined in ghc-internal-9.1003.0 · GHC.Internal.BaseFunctor IODefined in ghc-internal-9.1003.0 · GHC.Internal.BaseFunctor AnnotDetailsDefined in pretty-1.1.3.6 · Text.PrettyPrint.Annotated.HughesPJFunctor DocDefined in pretty-1.1.3.6 · Text.PrettyPrint.Annotated.HughesPJFunctor SpanDefined in pretty-1.1.3.6 · Text.PrettyPrint.Annotated.HughesPJFunctor STMDefined in stm-2.5.3.1 · Control.Sequential.STMFunctor PprMDefined in template-haskell-2.22.0.0 · Language.Haskell.TH.PprLibFunctor QDefined in template-haskell-2.22.0.0 · Language.Haskell.TH.SyntaxFunctor TyVarBndrDefined in template-haskell-2.22.0.0 · Language.Haskell.TH.SyntaxFunctor []Defined in ghc-internal-9.1003.0 · GHC.Internal.BaseFunctor ProxyDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.ProxyFunctor U1Defined in ghc-internal-9.1003.0 · GHC.Internal.GenericsFunctor V1Defined in ghc-internal-9.1003.0 · GHC.Internal.GenericsFunctor (Arg a)Defined in base-4.20.2.0 · Data.SemigroupFunctor (Array i)Defined in ghc-internal-9.1003.0 · GHC.Internal.ArrFunctor (ST s)Defined in ghc-internal-9.1003.0 · GHC.Internal.Control.Monad.ST.Lazy.ImpFunctor (Either a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.EitherFunctor (StateL s)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Functor.UtilsFunctor (StateR s)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Functor.UtilsFunctor (ST s)Defined in ghc-internal-9.1003.0 · GHC.Internal.STFunctor (Tuple2 a)Defined in ghc-internal-9.1003.0 · GHC.Internal.BaseFunctor f => Functor (Lift f)Defined in transformers-0.6.1.1 · Control.Applicative.LiftFunctor m => Functor (MaybeT m)Defined in transformers-0.6.1.1 · Control.Monad.Trans.MaybeMonad m => Functor (WrappedMonad m)Defined in base-4.20.2.0 · Control.ApplicativeMonad m => Functor (Handler m)Defined in exceptions-0.10.9 · Control.Monad.CatchMonad m => Functor (CatchT m)Defined in exceptions-0.10.9 · Control.Monad.Catch.PureArrow a => Functor (ArrowMonad a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Control.ArrowFunctor (Const m)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Functor.ConstFunctor (URec Char)Defined in ghc-internal-9.1003.0 · GHC.Internal.GenericsFunctor (URec Double)Defined in ghc-internal-9.1003.0 · GHC.Internal.GenericsFunctor (URec Float)Defined in ghc-internal-9.1003.0 · GHC.Internal.GenericsFunctor (URec Int)Defined in ghc-internal-9.1003.0 · GHC.Internal.GenericsFunctor (URec Word)Defined in ghc-internal-9.1003.0 · GHC.Internal.GenericsFunctor (URec (Ptr ()))Defined in ghc-internal-9.1003.0 · GHC.Internal.GenericsFunctor (Tuple3 a b)Defined in ghc-internal-9.1003.0 · GHC.Internal.BaseFunctor (Constant a)Defined in transformers-0.6.1.1 · Data.Functor.ConstantFunctor (t m) => Functor (LiftingAccum t m)Defined in mtl-2.3.1 · Control.Monad.AccumFunctor (t m) => Functor (LiftingSelect t m)Defined in mtl-2.3.1 · Control.Monad.SelectFunctor f => Functor (Ap f)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.MonoidFunctor f => Functor (Alt f)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.InternalFunctor f => Functor (Rec1 f)Defined in ghc-internal-9.1003.0 · GHC.Internal.GenericsFunctor f => Functor (Backwards f)Defined in transformers-0.6.1.1 · Control.Applicative.BackwardsDerived instance.
Functor f => Functor (Reverse f)Defined in transformers-0.6.1.1 · Data.Functor.ReverseDerived instance.
Functor m => Functor (Kleisli m a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Control.ArrowFunctor m => Functor (AccumT w m)Defined in transformers-0.6.1.1 · Control.Monad.Trans.AccumFunctor m => Functor (ExceptT e m)Defined in transformers-0.6.1.1 · Control.Monad.Trans.ExceptFunctor m => Functor (IdentityT m)Defined in transformers-0.6.1.1 · Control.Monad.Trans.IdentityFunctor m => Functor (ReaderT r m)Defined in transformers-0.6.1.1 · Control.Monad.Trans.ReaderFunctor m => Functor (SelectT r m)Defined in transformers-0.6.1.1 · Control.Monad.Trans.SelectFunctor m => Functor (StateT s m)Defined in transformers-0.6.1.1 · Control.Monad.Trans.State.LazyFunctor m => Functor (StateT s m)Defined in transformers-0.6.1.1 · Control.Monad.Trans.State.StrictFunctor m => Functor (WriterT w m)Defined in transformers-0.6.1.1 · Control.Monad.Trans.Writer.CPSFunctor m => Functor (WriterT w m)Defined in transformers-0.6.1.1 · Control.Monad.Trans.Writer.LazyFunctor m => Functor (WriterT w m)Defined in transformers-0.6.1.1 · Control.Monad.Trans.Writer.StrictMonad m => Functor (StateT s m)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Functor.UtilsArrow a => Functor (WrappedArrow a b)Defined in base-4.20.2.0 · Control.Applicative(Generic1 f, Functor (Rep1 f)) => Functor (Generically1 f)Defined in ghc-internal-9.1003.0 · GHC.Internal.GenericsFunctor (K1 i c)Defined in ghc-internal-9.1003.0 · GHC.Internal.GenericsFunctor (Tuple4 a b c)Defined in ghc-internal-9.1003.0 · GHC.Internal.BaseFunctor (ContT r m)Defined in transformers-0.6.1.1 · Control.Monad.Trans.ContFunctor ((->) r)Defined in ghc-internal-9.1003.0 · GHC.Internal.Base(Functor f, Functor g) => Functor (Product f g)Defined in base-4.20.2.0 · Data.Functor.Product(Functor f, Functor g) => Functor (Sum f g)Defined in base-4.20.2.0 · Data.Functor.Sum(Functor f, Functor g) => Functor (f :*: g)Defined in ghc-internal-9.1003.0 · GHC.Internal.Generics(Functor f, Functor g) => Functor (f :+: g)Defined in ghc-internal-9.1003.0 · GHC.Internal.GenericsFunctor (Tuple5 a b c d)Defined in ghc-internal-9.1003.0 · GHC.Internal.BaseFunctor f => Functor (M1 i c f)Defined in ghc-internal-9.1003.0 · GHC.Internal.GenericsFunctor m => Functor (RWST r w s m)Defined in transformers-0.6.1.1 · Control.Monad.Trans.RWS.CPSFunctor m => Functor (RWST r w s m)Defined in transformers-0.6.1.1 · Control.Monad.Trans.RWS.LazyFunctor m => Functor (RWST r w s m)Defined in transformers-0.6.1.1 · Control.Monad.Trans.RWS.Strict(Functor f, Functor g) => Functor (Compose f g)Defined in base-4.20.2.0 · Data.Functor.Compose(Functor f, Functor g) => Functor (f :.: g)Defined in ghc-internal-9.1003.0 · GHC.Internal.GenericsFunctor (Tuple6 a b c d e)Defined in ghc-internal-9.1003.0 · GHC.Internal.BaseFunctor (Tuple7 a b c d e f)Defined in ghc-internal-9.1003.0 · GHC.Internal.Base