# Functor

### From HaskellWiki

(Difference between revisions)

Artyom Kazak (Talk | contribs) m (fix broken link) |
(→Description) |
||

(One intermediate revision by one user 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 | + | |

− | + | See also [[Control.Applicative]] a special case of <hask>Functor f</hask> | |

− | + | ||

− | + | == Packages == | |

+ | * (base) Prelude | ||

+ | * (base) Data.Functor | ||

+ | * (base) Control.Monad | ||

− | + | == Syntax == | |

− | + | <pre> | |

− | + | class Functor f where | |

+ | fmap :: (a -> b) -> f a -> f b | ||

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

+ | </pre> | ||

+ | === Minimal Complete Definition === | ||

− | + | <pre> | |

+ | fmap | ||

+ | </pre> | ||

− | + | == Description == | |

+ | An abstract datatype <hask>f a</hask>, which has the ability for it's 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 it's 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>. | ||

+ | |||

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

+ | :<pre>fmap id = id</pre> | ||

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

+ | :<pre>fmap (f . g) == fmap f . fmap g</pre> | ||

+ | :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. <hask>fmap</hask> 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 == | ||

+ | |||

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

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

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

+ | |||

+ | == Related Functions == | ||

+ | |||

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

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

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

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

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

+ | : create a new <hask>f ()</hask> from an <hask>f a</hask> by replacing all of the values in the <hask>f a</hask> by <hask>()</hask> | ||

+ | |||

+ | == Further reading == | ||

+ | |||

+ | * [http://www.haskellforall.com/2012/09/the-functor-design-pattern.html The functor design pattern] | ||

[[Category:Functor|*]] | [[Category:Functor|*]] |

## Revision as of 10:27, 14 November 2017

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

Functor

functor

Functor f

## Contents |

## 1 Packages

- (base) Prelude
- (base) Data.Functor
- (base) Control.Monad

## 2 Syntax

class Functor f where fmap :: (a -> b) -> f a -> f b (<$) :: a -> f b -> f a

### 2.1 Minimal Complete Definition

fmap

## 3 Description

An abstract datatypef a

Functor

Functor

f b

f a

f

f

Functor

f a

a

Functor

fmap

### 3.1 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.

fmap

## 4 Methods

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

- Create a new , from anf busing the results of calling a function on every value in thef a.f a

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

- Create a new , from anf bby replacing all of the values in thef aby a given value.f a

## 5 Related Functions

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

- Create a new , from anf aby replacing all of the values in thef bby a given value.f b

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

- An infix synonym for Data.Functor.fmap

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

- create a new from anf ()by replacing all of the values in thef abyf a()