Portability portable provisional Edward Kmett Trustworthy

Data.Profunctor

Description

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

Synopsis

# Profunctors

class Profunctor p whereSource

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
```

Methods

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

Map over both arguments at the same time.

``dimap` f g ≡ `lmap` f `.` `rmap` g`

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

Map the first argument contravariantly.

``lmap` f ≡ `dimap` f `id``

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

Map the second argument covariantly.

``rmap` ≡ `dimap` `id``

Instances

 Profunctor (->) Monad m => Profunctor (Kleisli m) Functor w => Profunctor (Cokleisli w) Profunctor (Tagged *) Arrow p => Profunctor (WrappedArrow p) Functor f => Profunctor (DownStar f) Functor f => Profunctor (UpStar f)

## Profunctorial Strength

class Profunctor p => Strong p whereSource

Generalizing upstar of a strong `Functor`

Minimal complete definition: `first'` or `second'`

Note: Every `Functor` in Haskell is strong.

Methods

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

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

Instances

 Strong (->) (Profunctor (Kleisli m), Monad m) => Strong (Kleisli m) (Profunctor (WrappedArrow p), Arrow p) => Strong (WrappedArrow p) (Profunctor (UpStar m), Functor m) => Strong (UpStar m)

class Profunctor p => Choice p whereSource

The generalization of `DownStar` of a "costrong" `Functor`

Minimal complete definition: `left'` or `right'`

Note: We use `traverse` and `extract` as approximate costrength as needed.

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 (->) (Profunctor (Kleisli m), Monad m) => Choice (Kleisli m) (Profunctor (Cokleisli w), Comonad w) => Choice (Cokleisli w) `extract` approximates `costrength` Choice (Tagged *) (Profunctor (WrappedArrow p), ArrowChoice p) => Choice (WrappedArrow p) (Profunctor (DownStar w), Traversable w) => Choice (DownStar w) `sequence` approximates `costrength`

## Common Profunctors

newtype UpStar f d c Source

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

Constructors

 UpStar FieldsrunUpStar :: d -> f c

Instances

 Functor f => Profunctor (UpStar f) (Profunctor (UpStar m), Functor m) => Strong (UpStar m) Functor f => Functor (UpStar f a)

newtype DownStar f d c Source

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

Constructors

 DownStar FieldsrunDownStar :: f d -> c

Instances

 Functor f => Profunctor (DownStar f) (Profunctor (DownStar w), Traversable w) => Choice (DownStar w) `sequence` approximates `costrength` Functor (DownStar f a)

newtype WrappedArrow p a b Source

Wrap an arrow for use as a `Profunctor`.

Constructors

 WrapArrow FieldsunwrapArrow :: p a b

Instances

 (Category (WrappedArrow p), Arrow p) => Arrow (WrappedArrow p) (Arrow (WrappedArrow p), ArrowZero p) => ArrowZero (WrappedArrow p) (Arrow (WrappedArrow p), ArrowChoice p) => ArrowChoice (WrappedArrow p) (Arrow (WrappedArrow p), ArrowApply p) => ArrowApply (WrappedArrow p) (Arrow (WrappedArrow p), ArrowLoop p) => ArrowLoop (WrappedArrow p) Category p => Category (WrappedArrow p) Arrow p => Profunctor (WrappedArrow p) (Profunctor (WrappedArrow p), ArrowChoice p) => Choice (WrappedArrow p) (Profunctor (WrappedArrow p), Arrow p) => Strong (WrappedArrow p)