# Difference between revisions of "Functor"

m (Fixed type mistake in fmap synonym) |
Maniaphobic (talk | contribs) m (I corrected punctuation) |
||

(9 intermediate revisions by 6 users not shown) | |||

Line 1: | Line 1: | ||

The <strong><hask>Functor</hask></strong> typeclass represents the mathematical functor: a mapping between categories in the context of category theory. In practice a <hask>functor</hask> represents a type that can be mapped over. |
The <strong><hask>Functor</hask></strong> typeclass represents the mathematical functor: a mapping between categories in the context of category theory. In practice a <hask>functor</hask> represents a type that can be mapped over. |
||

− | See also [[ |
+ | See also [[Applicative functor]] which is a special case of <hask>Functor</hask> |

== Packages == |
== Packages == |
||

Line 24: | Line 24: | ||

== Description == |
== Description == |
||

− | An abstract datatype <hask>f a</hask>, which has the ability for |
+ | An abstract datatype <hask>f a</hask>, which has the ability for its value(s) to be mapped over, can become an instance of the <hask>Functor</hask> typeclass. That is to say, a new <hask>Functor</hask>, <hask>f b</hask>, can be made from <hask>f a</hask> by transforming all of its value(s), whilst leaving the structure of <hask>f</hask> itself unmodified. |

Declaring <hask>f</hask> an instance of <hask>Functor</hask> allows functions relating to mapping to be used on structures of type <hask>f a</hask> for all <hask>a</hask>. |
Declaring <hask>f</hask> an instance of <hask>Functor</hask> allows functions relating to mapping to be used on structures of type <hask>f a</hask> for all <hask>a</hask>. |
||

− | Functors are |
+ | Functors are required 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 === |
=== Functor Laws === |
||

Line 39: | Line 39: | ||

: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. |
: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. |
||

− | 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 by itself does not modify the values in the functor, only the function. The structure of the functor remains unchanged and only the values are modified. <hask>fmap</hask> returns an identical functor as the original, with |
+ | 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 by itself does not modify the values in the functor, only the function. The structure of the functor remains unchanged and only the values are modified. <hask>fmap</hask> returns an identical functor as the original, with its values swapped to the result of calling a given function with the original value as an argument. |

== Methods == |
== Methods == |
||

[[Data.Functor.fmap|<pre>fmap :: (a -> b) -> f a -> f b</pre>]] |
[[Data.Functor.fmap|<pre>fmap :: (a -> b) -> f a -> f b</pre>]] |
||

− | : Create a new <hask>f b</hask> |
+ | : Create a new <hask>f b</hask> from an <hask>f a</hask> using the results of calling a function on every value in the <hask>f a</hask>. |

<pre>(<$) :: a -> f b -> f a</pre> |
<pre>(<$) :: a -> f b -> f a</pre> |
||

− | : Create a new <hask>f a</hask> |
+ | : Create a new <hask>f a</hask> from an <hask>f b</hask> by replacing all of the values in the <hask>f b</hask> by a given value of type <hask>a</hask>. |

== Related Functions == |
== Related Functions == |
||

Line 52: | Line 52: | ||

<pre>($>) :: f a -> b -> f b</pre> |
<pre>($>) :: f a -> b -> f b</pre> |
||

: Create a new <hask>f b</hask>, from an <hask>f a</hask> by replacing all of the values in the <hask>f a</hask> by a given value of type <hask>b</hask>. |
: Create a new <hask>f b</hask>, from an <hask>f a</hask> by replacing all of the values in the <hask>f a</hask> by a given value of type <hask>b</hask>. |
||

− | <pre>(<$>) :: (a -> |
+ | <pre>(<$>) :: (a -> b) -> f a -> f b</pre> |

: An infix synonym for [[Data.Functor.fmap]] |
: An infix synonym for [[Data.Functor.fmap]] |
||

[[Data.Functor.void|<pre>void :: Functor f => f a -> f ()</pre>]] |
[[Data.Functor.void|<pre>void :: Functor f => f a -> f ()</pre>]] |

## Latest revision as of 20:58, 10 November 2019

The ** Functor** typeclass represents the mathematical functor: a mapping between categories in the context of category theory. In practice a

`functor`

represents a type that can be mapped over.
See also Applicative functor which is a special case of `Functor`

## Contents

## 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 its 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 its 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 required 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.

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 by itself does not modify the values in the functor, only the function. The structure of the functor remains unchanged and only the values are modified. `fmap`

returns an identical functor as the original, with its 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 an`f a`

using the results of calling a function on every value in the`f a`

.

(<$) :: a -> f b -> f a

- Create a new
`f a`

from an`f b`

by replacing all of the values in the`f b`

by a given value of type`a`

.

## Related Functions

($>) :: f a -> b -> f b

- Create a new
`f b`

, from an`f a`

by replacing all of the values in the`f a`

by a given value of type`b`

.

(<$>) :: (a -> b) -> f a -> f b

- An infix synonym for Data.Functor.fmap

void :: Functor f => f a -> f ()

- create a new
`f ()`

from an`f a`

by replacing all of the values in the`f a`

by`()`