Functor: Difference between revisions
(MDS style description of functor calss) |
|||
Line 29: | Line 29: | ||
Functors are requred to obey certain laws in regards to their mapping. Ensuring instances of <hask>Functor</hask> obey these laws means the behaviour of <hask>fmap</hask> remains predictable. | Functors are requred to obey certain laws in regards to their mapping. Ensuring instances of <hask>Functor</hask> obey these laws means the behaviour of <hask>fmap</hask> remains predictable. | ||
=== Functor Laws === | |||
;Functors must preserve identity morphisms: | ;Functors must preserve identity morphisms: |
Revision as of 10:27, 14 November 2017
The Functor
functor
represents a type that can be mapped over.
See also Control.Applicative a special case of Functor f
Packages
- (base) Prelude
- (base) Data.Functor
- (base) Control.Monad
Syntax
class Functor f where fmap :: (a -> b) -> f a -> f b (<$) :: a -> f b -> f a
Minimal Complete Definition
fmap
Description
An abstract datatype f a
, which has the ability for it's value(s) to be mapped over can become an instance of the Functor
typeclass. That is to say, a new Functor
, f b
can be made from f a
by transforming all of it's value(s), whilst leaving the structure of f
itself unmodified.
Declaring f
an instance of Functor
allows functions relating to mapping to be used on structures of type f a
for all a
.
Functors are requred to obey certain laws in regards to their mapping. Ensuring instances of Functor
obey these laws means the behaviour of fmap
remains predictable.
Functor Laws
- Functors must preserve identity morphisms
fmap id = id
- When performing the mapping operation, if the values in the functor are mapped to themselves, the result will be an unmodified functor.
- Functors preserve composition of morphisms
fmap (f . g) == fmap f . fmap g
- If two sequential mapping operations are performed one after the other using two functions, the result should be the same as a single mapping operation with one function that is equivalent to applying the first function to the result of the second. Essentially this means the structure of the functor must remain unchanged, but the values contained in the functor may be modified by the mapping.
These two laws ensure that functors behave the way they were intended. The values of the functor are only modified by the function provided to the mapping operation. The mapping operation itelf does not modify the values in the functor. The structure of the functor remains unchanged and only the values are modified. fmap
returns an identical functor as the original, with it's values swapped to the result of calling a given function with the original value as an argument.
Methods
fmap :: (a -> b) -> f a -> f b
- Create a new
f b
, from anf a
using the results of calling a function on every value in thef a
.
(<$) :: a -> f b -> f a
- Create a new
f b
, from anf a
by replacing all of the values in thef a
by a given value.
Related Functions
($>) :: a -> f b -> f a
- Create a new
f a
, from anf b
by replacing all of the values in thef b
by a given value.
(<$>) :: a -> f b -> f a
- An infix synonym for Data.Functor.fmap
void :: Functor f => f a -> f ()
- create a new
f ()
from anf a
by replacing all of the values in thef a
by()