profunctors-5.1.2: Profunctors

Copyright | (C) 2011-2013 Edward Kmett,
License | BSD-style (see the file LICENSE)
Maintainer | Edward Kmett <ekmett@gmail.com>
Stability | provisional
Portability | portable
Safe Haskell | Trustworthy
Language | Haskell2010

Data.Profunctor

Contents

- Profunctors
  - Profunctorial Strength
  - Profunctorial Costrength
  - Common Profunctors

Description

For a good explanation of profunctors in Haskell see Dan Piponi's article:

http://blog.sigfpe.com/2011/07/profunctors-in-haskell.html

For more information on strength and costrength, see:

http://comonad.com/reader/2008/deriving-strength-from-laziness/

Synopsis

- class Profunctor p where
  - dimap :: (a -> b) -> (c -> d) -> p b c -> p a d
  - lmap :: (a -> b) -> p b c -> p a c
  - rmap :: (b -> c) -> p a b -> p a c
- class Profunctor p => Strong p where
  - first' :: p a b -> p (a, c) (b, c)
  - second' :: p a b -> p (c, a) (c, b)
- class Profunctor p => Choice p where
  - left' :: p a b -> p (Either a c) (Either b c)
  - right' :: p a b -> p (Either c a) (Either c b)
- class Profunctor p => Costrong p where
  - unfirst :: p (a, d) (b, d) -> p a b
  - unsecond :: p (d, a) (d, b) -> p a b
- class Profunctor p => Cochoice p where
  - unleft :: p (Either a d) (Either b d) -> p a b
  - unright :: p (Either d a) (Either d b) -> p a b
- newtype Star f d c = Star {
  - runStar :: d -> f c
  }
- newtype Costar f d c = Costar {
  - runCostar :: f d -> c
  }
- newtype WrappedArrow p a b = WrapArrow {
  - unwrapArrow :: p a b
  }
- newtype Forget r a b = Forget {
  - runForget :: a -> r
  }
- type (:->) p q = forall a b. p a b -> q a b

# Profunctors

class Profunctor p where Source

Formally, the class `Profunctor` represents a profunctor from `Hask` -> `Hask`.

Intuitively it is a bifunctor where the first argument is contravariant and the second argument is covariant.

You can define a `Profunctor` by either defining `dimap` or by defining both `lmap` and `rmap`.

If you supply `dimap`, you should ensure that:

    dimap id id ≡ id

If you supply `lmap` and `rmap`, ensure:

    lmap id ≡ id
    rmap id ≡ id

If you supply both, you should also ensure:

    dimap f g ≡ lmap f . rmap g

These ensure by parametricity:

    dimap (f . g) (h . i) ≡ dimap g h . dimap f i
    lmap (f . g) ≡ lmap g . lmap f
    rmap (f . g) ≡ rmap f . rmap g

Minimal complete definition

dimap | lmap, rmap

Methods

dimap :: (a -> b) -> (c -> d) -> p b c -> p a d Source

Map over both arguments at the same time.

    dimap f g ≡ lmap f . rmap g

lmap :: (a -> b) -> p b c -> p a c Source

Map the first argument contravariantly.

    lmap f ≡ dimap f id

rmap :: (b -> c) -> p a b -> p a c Source

Map the second argument covariantly.

    rmap ≡ dimap id

Instances

Profunctor (->) Source |
Monad m => Profunctor (Kleisli m) Source |
Functor f => Profunctor (Joker f) Source |
Contravariant f => Profunctor (Clown f) Source |
Functor w => Profunctor (Cokleisli w) Source |
Profunctor (Tagged *) Source |
Profunctor (Forget r) Source |
Arrow p => Profunctor (WrappedArrow p) Source |
Functor f => Profunctor (Costar f) Source |
Functor f => Profunctor (Star f) Source |
Profunctor p => Profunctor (Environment p) Source |
Profunctor p => Profunctor (Closure p) Source |
Profunctor p => Profunctor (Codensity p) Source |
Profunctor p => Profunctor (Copastro p) Source |
Profunctor p => Profunctor (Cotambara p) Source |
Profunctor p => Profunctor (Pastro p) Source |
Profunctor p => Profunctor (Tambara p) Source |
(Functor f, Profunctor p) => Profunctor (Cayley f p) Source |
(Profunctor p, Profunctor q) => Profunctor (Rift p q) Source |
(Profunctor p, Profunctor q) => Profunctor (Procompose p q) Source |
(Profunctor p, Profunctor q) => Profunctor (Ran p q) Source |

## Profunctorial Strength

class Profunctor p => Strong p where Source

Generalizing `Star` of a strong `Functor`

Note: Every `Functor` in Haskell is strong with respect to `(,)`.

This describes profunctor strength with respect to the product structure of Hask.

http://www-kb.is.s.u-tokyo.ac.jp/~asada/papers/arrStrMnd.pdf

Minimal complete definition

first' | second'

Methods

first' :: p a b -> p (a, c) (b, c) Source

second' :: p a b -> p (c, a) (c, b) Source

Instances

Strong (->) Source |
Monad m => Strong (Kleisli m) Source |
Strong (Forget r) Source |
Arrow p => Strong (WrappedArrow p) Source | `Arrow` is `Strong` `Category`
Functor m => Strong (Star m) Source |
Strong p => Strong (Closure p) Source |
Profunctor p => Strong (Tambara p) Source |
(Functor f, Strong p) => Strong (Cayley f p) Source |
(Strong p, Strong q) => Strong (Procompose p q) Source |

class Profunctor p => Choice p where Source

The generalization of `Costar` of `Functor` that is strong with respect to `Either`.

Note: This is also a notion of strength, except with regards to another monoidal structure that we can choose to equip Hask with: the cocartesian coproduct.

Minimal complete definition

left' | right'

Methods

left' :: p a b -> p (Either a c) (Either b c) Source

right' :: p a b -> p (Either c a) (Either c b) Source

Instances

Choice (->) Source |
Monad m => Choice (Kleisli m) Source |
Comonad w => Choice (Cokleisli w) Source | `extract` approximates `costrength`
Choice (Tagged *) Source |
Monoid r => Choice (Forget r) Source |
ArrowChoice p => Choice (WrappedArrow p) Source |
Traversable w => Choice (Costar w) Source |
Applicative f => Choice (Star f) Source |
Profunctor p => Choice (Cotambara p) Source |
Choice p => Choice (Tambara p) Source |
(Functor f, Choice p) => Choice (Cayley f p) Source |
(Choice p, Choice q) => Choice (Procompose p q) Source |

## Profunctorial Costrength

class Profunctor p => Costrong p where Source

Analogous to `ArrowLoop`, `loop` = `unfirst`

Minimal complete definition

unfirst | unsecond

Methods

unfirst :: p (a, d) (b, d) -> p a b Source

unsecond :: p (d, a) (d, b) -> p a b Source

Instances

Costrong (->) Source |
MonadFix m => Costrong (Kleisli m) Source |
Functor f => Costrong (Cokleisli f) Source |
Costrong (Tagged *) Source |
ArrowLoop p => Costrong (WrappedArrow p) Source |
Functor f => Costrong (Costar f) Source |
(Corepresentable p, Corepresentable q) => Costrong (Procompose p q) Source |

class Profunctor p => Cochoice p where Source

Minimal complete definition

unleft | unright

Methods

unleft :: p (Either a d) (Either b d) -> p a b Source

unright :: p (Either d a) (Either d b) -> p a b Source

Instances

Cochoice (->) Source |
Applicative f => Cochoice (Costar f) Source |
Traversable f => Cochoice (Star f) Source |

## Common Profunctors

newtype Star f d c Source

Lift a `Functor` into a `Profunctor` (forwards).

Constructors

Star |

Fields

runStar :: d -> f c

Instances

Functor f => Profunctor (Star f) Source |
Traversable f => Cochoice (Star f) Source |
Applicative f => Choice (Star f) Source |
Functor m => Strong (Star m) Source |
Distributive f => Closed (Star f) Source |
Functor f => Representable (Star f) Source |
Functor f => Sieve (Star f) f Source |
Monad f => Monad (Star f a) Source |
Functor f => Functor (Star f a) Source |
Applicative f => Applicative (Star f a) Source |
Alternative f => Alternative (Star f a) Source |
MonadPlus f => MonadPlus (Star f a) Source |
Distributive f => Distributive (Star f a) Source |
type Rep (Star f) = f Source |

newtype Costar f d c Source

Lift a `Functor` into a `Profunctor` (backwards).

Constructors

Costar |

Fields

runCostar :: f d -> c

Instances

Functor f => Profunctor (Costar f) Source |
Applicative f => Cochoice (Costar f) Source |
Functor f => Costrong (Costar f) Source |
Traversable w => Choice (Costar w) Source |
Functor f => Closed (Costar f) Source |
Functor f => Corepresentable (Costar f) Source |
Functor f => Cosieve (Costar f) f Source |
Monad (Costar f a) Source |
Functor (Costar f a) Source |
Applicative (Costar f a) Source |
Distributive (Costar f d) Source |
type Corep (Costar f) = f Source |

newtype WrappedArrow p a b Source

Wrap an arrow for use as a `Profunctor`.

Constructors

WrapArrow |

Fields

unwrapArrow :: p a b

Instances

Category * p => Category * (WrappedArrow p) Source |
Arrow p => Arrow (WrappedArrow p) Source |
ArrowZero p => ArrowZero (WrappedArrow p) Source |
ArrowChoice p => ArrowChoice (WrappedArrow p) Source |
ArrowApply p => ArrowApply (WrappedArrow p) Source |
ArrowLoop p => ArrowLoop (WrappedArrow p) Source |
Arrow p => Profunctor (WrappedArrow p) Source |
ArrowLoop p => Costrong (WrappedArrow p) Source |
ArrowChoice p => Choice (WrappedArrow p) Source |
Arrow p => Strong (WrappedArrow p) Source | `Arrow` is `Strong` `Category`

newtype Forget r a b Source

Constructors

Forget |

Fields

runForget :: a -> r

Instances

Profunctor (Forget r) Source |
Monoid r => Choice (Forget r) Source |
Strong (Forget r) Source |
Representable (Forget r) Source |
Sieve (Forget r) (Const r) Source |
Functor (Forget r a) Source |
Foldable (Forget r a) Source |
Traversable (Forget r a) Source |
type Rep (Forget r) = Const r Source |

type (:->) p q = forall a b. p a b -> q a b infixr 0 Source