Agda-2.5.2: A dependently typed functional programming language and proof assistant

Safe Haskell | Safe
Language | Haskell2010

Agda.Utils.Functor

Contents

- The constant functor.
- Maybe. Can only be traversed into pointed functors.
- Other disjoint sum types, like lists etc.

Description

Utilities for functors.

Synopsis

- ($>) :: Functor f => f a -> b -> f b
- (<.>) :: Functor m => (b -> c) -> (a -> m b) -> a -> m c
- for :: Functor m => m a -> (a -> b) -> m b
- (<&>) :: Functor m => m a -> (a -> b) -> m b
- class Functor t => Decoration t where
- dmap :: Decoration t => (a -> b) -> t a -> t b
- dget :: Decoration t => t a -> a
- (<$>) :: Functor f => (a -> b) -> f a -> f b

# Documentation

($>) :: Functor f => f a -> b -> f b infixr 4 #

(<.>) :: Functor m => (b -> c) -> (a -> m b) -> a -> m c infixr 9 #

Composition: pure function after functorial (monadic) function.

for :: Functor m => m a -> (a -> b) -> m b #

The true pure `for` loop. `for` is a misnomer, it should be `forA`.

(<&>) :: Functor m => m a -> (a -> b) -> m b infix 4 #

Infix version of `for`.

class Functor t => Decoration t where #

A decoration is a functor that is traversable into any functor.

The `Functor` superclass is given because of the limitations of the Haskell class system. `traverseF` actually implies functoriality.

Minimal complete definition: `traverseF` or `distributeF`.

Methods

traverseF :: Functor m => (a -> m b) -> t a -> m (t b) #

`traverseF` is the defining property.

distributeF :: Functor m => t (m a) -> m (t a) #

Decorations commute into any functor.

Instances

Decoration Identity # | The identity functor is a decoration.
Methods traverseF :: Functor m => (a -> m b) -> Identity a -> m (Identity b) # distributeF :: Functor m => Identity (m a) -> m (Identity a) #
Decoration Ranged # |
Methods traverseF :: Functor m => (a -> m b) -> Ranged a -> m (Ranged b) # distributeF :: Functor m => Ranged (m a) -> m (Ranged a) #
Decoration Dom # |
Methods traverseF :: Functor m => (a -> m b) -> Dom a -> m (Dom b) # distributeF :: Functor m => Dom (m a) -> m (Dom a) #
Decoration Arg # |
Methods traverseF :: Functor m => (a -> m b) -> Arg a -> m (Arg b) # distributeF :: Functor m => Arg (m a) -> m (Arg a) #
Decoration WithHiding # |
Methods traverseF :: Functor m => (a -> m b) -> WithHiding a -> m (WithHiding b) # distributeF :: Functor m => WithHiding (m a) -> m (WithHiding a) #
Decoration Type' # |
Methods traverseF :: Functor m => (a -> m b) -> Type' a -> m (Type' b) # distributeF :: Functor m => Type' (m a) -> m (Type' a) #
Decoration Abs # |
Methods traverseF :: Functor m => (a -> m b) -> Abs a -> m (Abs b) # distributeF :: Functor m => Abs (m a) -> m (Abs a) #
Decoration Local # |
Methods traverseF :: Functor m => (a -> m b) -> Local a -> m (Local b) # distributeF :: Functor m => Local (m a) -> m (Local a) #
Decoration Open # |
Methods traverseF :: Functor m => (a -> m b) -> Open a -> m (Open b) # distributeF :: Functor m => Open (m a) -> m (Open a) #
Decoration Masked # |
Methods traverseF :: Functor m => (a -> m b) -> Masked a -> m (Masked b) # distributeF :: Functor m => Masked (m a) -> m (Masked a) #
Decoration ((,) a) # | A typical decoration is pairing with some stuff.
Methods traverseF :: Functor m => (a -> m b) -> (a, a) -> m (a, b) # distributeF :: Functor m => (a, m a) -> m (a, a) #
Decoration (Named name) # |
Methods traverseF :: Functor m => (a -> m b) -> Named name a -> m (Named name b) # distributeF :: Functor m => Named name (m a) -> m (Named name a) #
(Decoration d, Decoration t) => Decoration (Compose * * d t) # | Decorations compose. (Thus, they form a category.)
Methods traverseF :: Functor m => (a -> m b) -> Compose * * d t a -> m (Compose * * d t b) # distributeF :: Functor m => Compose * * d t (m a) -> m (Compose * * d t a) #

dmap :: Decoration t => (a -> b) -> t a -> t b #

Any decoration is traversable with `traverse = traverseF`. Just like any `Traversable` is a functor, so is any decoration, given by just `traverseF`, a functor.

dget :: Decoration t => t a -> a #

Any decoration is a lens. `set` is a special case of `dmap`.

# The constant functor.

# Maybe. Can only be traversed into pointed functors.

# Other disjoint sum types, like lists etc.

(<$>) :: Functor f => (a -> b) -> f a -> f b infixl 4 #

An infix synonym for `fmap`.

The name of this operator is an allusion to `$`. Note the similarities between their types:

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

Whereas `$` is function application, `<$>` is function application lifted over a `Functor`.

#### Examples

Convert from a ``Maybe` `Int`` to a ``Maybe` `String`` using `show`:

    >>> show <$> Nothing
    Nothing
    >>> show <$> Just 3
    Just "3"

Convert from an ``Either` `Int` `Int`` to an ``Either` `Int`` `String` using `show`:

    >>> show <$> Left 17
    Left 17
    >>> show <$> Right 17
    Right "17"

Double each element of a list:

    >>> (*2) <$> [1,2,3]
    [2,4,6]

Apply `even` to the second element of a pair:

    >>> even <$> (2,2)
    (2,True)