profunctors-4.0.1: Profunctors

Portability | portable
Stability | provisional
Maintainer | Edward Kmett <ekmett@gmail.com>
Safe Haskell | Unsafe

Data.Profunctor.Unsafe

Contents

- 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

This module includes unsafe composition operators that are useful in practice when it comes to generating optimal core in GHC.

If you import this module you are taking upon yourself the obligation that you will only call the operators with `#` in their names with functions that are operationally identity such as `newtype` constructors or the field accessor of a `newtype`.

If you are ever in doubt, use `rmap` or `lmap`.

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
  - (#.) :: (b -> c) -> p a b -> p a c
  - (.#) :: p b c -> (a -> b) -> p a c

# 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

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

(#.) :: (b -> c) -> p a b -> p a cSource

Strictly map the second argument argument covariantly with a function that is assumed operationally to be a cast, such as a newtype constructor.

Note: This operation is explicitly unsafe since an implementation may choose to use `unsafeCoerce` to implement this combinator and it has no way to validate that your function meets the requirements.

If you implement this combinator with `unsafeCoerce`, then you are taking upon yourself the obligation that you don't use GADT-like tricks to distinguish values.

If you import Data.Profunctor.Unsafe you are taking upon yourself the obligation that you will only call this with a first argument that is operationally identity.

The semantics of this function with respect to bottoms should match the default definition:

    (#.) ≡ \f -> \p -> p `seq` rmap f p

(.#) :: p b c -> (a -> b) -> p a cSource

Strictly map the first argument argument contravariantly with a function that is assumed operationally to be a cast, such as a newtype constructor.

Note: This operation is explicitly unsafe since an implementation may choose to use `unsafeCoerce` to implement this combinator and it has no way to validate that your function meets the requirements.

If you implement this combinator with `unsafeCoerce`, then you are taking upon yourself the obligation that you don't use GADT-like tricks to distinguish values.

If you import Data.Profunctor.Unsafe you are taking upon yourself the obligation that you will only call this with a second argument that is operationally identity.

    (.#) ≡ \p -> p `seq` \f -> lmap f p

Instances

Profunctor (->) |
Monad m => Profunctor (Kleisli m) |
Functor w => Profunctor (Cokleisli w) |
Profunctor (Tagged *) |
Profunctor (Forget r) |
Arrow p => Profunctor (WrappedArrow p) |
Functor f => Profunctor (DownStar f) |
Functor f => Profunctor (UpStar f) |
(Profunctor p, Profunctor q) => Profunctor (Procompose p q) |
(Profunctor p, Profunctor q) => Profunctor (Rift p q) |