{-# LANGUAGE CPP #-}
{-# LANGUAGE Trustworthy #-}
{-# LANGUAGE ScopedTypeVariables #-}
-----------------------------------------------------------------------------
-- |
-- Copyright   :  (C) 2011-2018 Edward Kmett
-- License     :  BSD-style (see the file LICENSE)
--
-- Maintainer  :  Edward Kmett <ekmett@gmail.com>
-- Stability   :  provisional
-- Portability :  portable
--
-- 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'.
----------------------------------------------------------------------------
module Data.Profunctor.Unsafe
  (
  -- * Profunctors
    Profunctor(..)
  ) where

import Control.Arrow
import Control.Category
import Control.Comonad (Cokleisli(..))
import Control.Monad (liftM)
import Data.Bifunctor.Biff (Biff(..))
import Data.Bifunctor.Clown (Clown(..))
import Data.Bifunctor.Joker (Joker(..))
import Data.Bifunctor.Product (Product(..))
import Data.Bifunctor.Sum (Sum(..))
import Data.Bifunctor.Tannen (Tannen(..))
import Data.Coerce (Coercible, coerce)
#if __GLASGOW_HASKELL__ < 710
import Data.Functor
#endif
import Data.Functor.Contravariant (Contravariant(..))
import Data.Tagged
import Prelude hiding (id,(.))

infixr 9 #.
infixl 8 .#

----------------------------------------------------------------------------
-- Profunctors
----------------------------------------------------------------------------

-- | 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
-- @
class Profunctor p where
  -- | Map over both arguments at the same time.
  --
  -- @'dimap' f g ≡ 'lmap' f '.' 'rmap' g@
  dimap :: (a -> b) -> (c -> d) -> p b c -> p a d
  dimap a -> b
f c -> d
g = forall (p :: * -> * -> *) a b c.
Profunctor p =>
(a -> b) -> p b c -> p a c
lmap a -> b
f forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. forall (p :: * -> * -> *) b c a.
Profunctor p =>
(b -> c) -> p a b -> p a c
rmap c -> d
g
  {-# INLINE dimap #-}

  -- | Map the first argument contravariantly.
  --
  -- @'lmap' f ≡ 'dimap' f 'id'@
  lmap :: (a -> b) -> p b c -> p a c
  lmap a -> b
f = forall (p :: * -> * -> *) a b c d.
Profunctor p =>
(a -> b) -> (c -> d) -> p b c -> p a d
dimap a -> b
f forall {k} (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id
  {-# INLINE lmap #-}

  -- | Map the second argument covariantly.
  --
  -- @'rmap' ≡ 'dimap' 'id'@
  rmap :: (b -> c) -> p a b -> p a c
  rmap = forall (p :: * -> * -> *) a b c d.
Profunctor p =>
(a -> b) -> (c -> d) -> p b c -> p a d
dimap forall {k} (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id
  {-# INLINE rmap #-}

  -- | 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:
  --
  -- @('Profuctor.Unsafe.#.') ≡ \\_ -> \\p -> p \`seq\` 'rmap' 'coerce' p@
  (#.) :: forall a b c q. Coercible c b => q b c -> p a b -> p a c
  (#.) = \q b c
_ -> \p a b
p -> p a b
p seq :: forall a b. a -> b -> b
`seq` forall (p :: * -> * -> *) b c a.
Profunctor p =>
(b -> c) -> p a b -> p a c
rmap (coerce :: forall a b. Coercible a b => a -> b
coerce (forall {k} (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id :: c -> c) :: b -> c) p a b
p
  {-# INLINE (#.) #-}

  -- | 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' 'coerce' p@
  (.#) :: forall a b c q. Coercible b a => p b c -> q a b -> p a c
  (.#) = \p b c
p -> p b c
p seq :: forall a b. a -> b -> b
`seq` \q a b
_ -> forall (p :: * -> * -> *) a b c.
Profunctor p =>
(a -> b) -> p b c -> p a c
lmap (coerce :: forall a b. Coercible a b => a -> b
coerce (forall {k} (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id :: b -> b) :: a -> b) p b c
p
  {-# INLINE (.#) #-}

  {-# MINIMAL dimap | (lmap, rmap) #-}

instance Profunctor (->) where
  dimap :: forall a b c d. (a -> b) -> (c -> d) -> (b -> c) -> a -> d
dimap a -> b
ab c -> d
cd b -> c
bc = c -> d
cd forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. b -> c
bc forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. a -> b
ab
  {-# INLINE dimap #-}
  lmap :: forall a b c. (a -> b) -> (b -> c) -> a -> c
lmap = forall a b c. (a -> b -> c) -> b -> a -> c
flip forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
(.)
  {-# INLINE lmap #-}
  rmap :: forall b c a. (b -> c) -> (a -> b) -> a -> c
rmap = forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
(.)
  {-# INLINE rmap #-}
  #. :: forall a b c (q :: * -> * -> *).
Coercible c b =>
q b c -> (a -> b) -> a -> c
(#.) q b c
_ = coerce :: forall a b. Coercible a b => a -> b
coerce (\b
x -> b
x :: b) :: forall a b. Coercible b a => a -> b
  .# :: forall a b c (q :: * -> * -> *).
Coercible b a =>
(b -> c) -> q a b -> a -> c
(.#) b -> c
pbc q a b
_ = coerce :: forall a b. Coercible a b => a -> b
coerce b -> c
pbc
  {-# INLINE (#.) #-}
  {-# INLINE (.#) #-}

instance Profunctor Tagged where
  dimap :: forall a b c d. (a -> b) -> (c -> d) -> Tagged b c -> Tagged a d
dimap a -> b
_ c -> d
f (Tagged c
s) = forall {k} (s :: k) b. b -> Tagged s b
Tagged (c -> d
f c
s)
  {-# INLINE dimap #-}
  lmap :: forall a b c. (a -> b) -> Tagged b c -> Tagged a c
lmap a -> b
_ = forall {k1} {k2} (s :: k1) b (t :: k2). Tagged s b -> Tagged t b
retag
  {-# INLINE lmap #-}
  rmap :: forall b c a. (b -> c) -> Tagged a b -> Tagged a c
rmap = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap
  {-# INLINE rmap #-}
  #. :: forall a b c (q :: * -> * -> *).
Coercible c b =>
q b c -> Tagged a b -> Tagged a c
(#.) q b c
_ = coerce :: forall a b. Coercible a b => a -> b
coerce (\b
x -> b
x :: b) :: forall a b. Coercible b a => a -> b
  {-# INLINE (#.) #-}
  Tagged c
s .# :: forall a b c (q :: * -> * -> *).
Coercible b a =>
Tagged b c -> q a b -> Tagged a c
.# q a b
_ = forall {k} (s :: k) b. b -> Tagged s b
Tagged c
s
  {-# INLINE (.#) #-}

instance Monad m => Profunctor (Kleisli m) where
  dimap :: forall a b c d.
(a -> b) -> (c -> d) -> Kleisli m b c -> Kleisli m a d
dimap a -> b
f c -> d
g (Kleisli b -> m c
h) = forall (m :: * -> *) a b. (a -> m b) -> Kleisli m a b
Kleisli (forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM c -> d
g forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. b -> m c
h forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. a -> b
f)
  {-# INLINE dimap #-}
  lmap :: forall a b c. (a -> b) -> Kleisli m b c -> Kleisli m a c
lmap a -> b
k (Kleisli b -> m c
f) = forall (m :: * -> *) a b. (a -> m b) -> Kleisli m a b
Kleisli (b -> m c
f forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. a -> b
k)
  {-# INLINE lmap #-}
  rmap :: forall b c a. (b -> c) -> Kleisli m a b -> Kleisli m a c
rmap b -> c
k (Kleisli a -> m b
f) = forall (m :: * -> *) a b. (a -> m b) -> Kleisli m a b
Kleisli (forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM b -> c
k forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. a -> m b
f)
  {-# INLINE rmap #-}
  -- We cannot safely overload (#.) because we didn't provide the 'Monad'.
  .# :: forall a b c (q :: * -> * -> *).
Coercible b a =>
Kleisli m b c -> q a b -> Kleisli m a c
(.#) Kleisli m b c
pbc q a b
_ = coerce :: forall a b. Coercible a b => a -> b
coerce Kleisli m b c
pbc
  {-# INLINE (.#) #-}

instance Functor w => Profunctor (Cokleisli w) where
  dimap :: forall a b c d.
(a -> b) -> (c -> d) -> Cokleisli w b c -> Cokleisli w a d
dimap a -> b
f c -> d
g (Cokleisli w b -> c
h) = forall {k} (w :: k -> *) (a :: k) b. (w a -> b) -> Cokleisli w a b
Cokleisli (c -> d
g forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. w b -> c
h forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f)
  {-# INLINE dimap #-}
  lmap :: forall a b c. (a -> b) -> Cokleisli w b c -> Cokleisli w a c
lmap a -> b
k (Cokleisli w b -> c
f) = forall {k} (w :: k -> *) (a :: k) b. (w a -> b) -> Cokleisli w a b
Cokleisli (w b -> c
f forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
k)
  {-# INLINE lmap #-}
  rmap :: forall b c a. (b -> c) -> Cokleisli w a b -> Cokleisli w a c
rmap b -> c
k (Cokleisli w a -> b
f) = forall {k} (w :: k -> *) (a :: k) b. (w a -> b) -> Cokleisli w a b
Cokleisli (b -> c
k forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. w a -> b
f)
  {-# INLINE rmap #-}
  -- We cannot safely overload (.#) because we didn't provide the 'Functor'.
  #. :: forall a b c (q :: * -> * -> *).
Coercible c b =>
q b c -> Cokleisli w a b -> Cokleisli w a c
(#.) q b c
_ = coerce :: forall a b. Coercible a b => a -> b
coerce (\b
x -> b
x :: b) :: forall a b. Coercible b a => a -> b
  {-# INLINE (#.) #-}

instance Contravariant f => Profunctor (Clown f) where
  lmap :: forall a b c. (a -> b) -> Clown f b c -> Clown f a c
lmap a -> b
f (Clown f b
fa) = forall {k} {k1} (f :: k -> *) (a :: k) (b :: k1).
f a -> Clown f a b
Clown (forall (f :: * -> *) a' a.
Contravariant f =>
(a' -> a) -> f a -> f a'
contramap a -> b
f f b
fa)
  {-# INLINE lmap #-}
  rmap :: forall b c a. (b -> c) -> Clown f a b -> Clown f a c
rmap b -> c
_ (Clown f a
fa) = forall {k} {k1} (f :: k -> *) (a :: k) (b :: k1).
f a -> Clown f a b
Clown f a
fa
  {-# INLINE rmap #-}
  dimap :: forall a b c d. (a -> b) -> (c -> d) -> Clown f b c -> Clown f a d
dimap a -> b
f c -> d
_ (Clown f b
fa) = forall {k} {k1} (f :: k -> *) (a :: k) (b :: k1).
f a -> Clown f a b
Clown (forall (f :: * -> *) a' a.
Contravariant f =>
(a' -> a) -> f a -> f a'
contramap a -> b
f f b
fa)
  {-# INLINE dimap #-}

instance Functor f => Profunctor (Joker f) where
  lmap :: forall a b c. (a -> b) -> Joker f b c -> Joker f a c
lmap a -> b
_ (Joker f c
fb) = forall {k} {k1} (g :: k -> *) (a :: k1) (b :: k).
g b -> Joker g a b
Joker f c
fb
  {-# INLINE lmap #-}
  rmap :: forall b c a. (b -> c) -> Joker f a b -> Joker f a c
rmap b -> c
g (Joker f b
fb) = forall {k} {k1} (g :: k -> *) (a :: k1) (b :: k).
g b -> Joker g a b
Joker (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap b -> c
g f b
fb)
  {-# INLINE rmap #-}
  dimap :: forall a b c d. (a -> b) -> (c -> d) -> Joker f b c -> Joker f a d
dimap a -> b
_ c -> d
g (Joker f c
fb) = forall {k} {k1} (g :: k -> *) (a :: k1) (b :: k).
g b -> Joker g a b
Joker (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap c -> d
g f c
fb)
  {-# INLINE dimap #-}

instance (Profunctor p, Functor f, Functor g) => Profunctor (Biff p f g) where
  lmap :: forall a b c. (a -> b) -> Biff p f g b c -> Biff p f g a c
lmap a -> b
f (Biff p (f b) (g c)
p) = forall {k} {k1} {k2} {k3} (p :: k -> k1 -> *) (f :: k2 -> k)
       (g :: k3 -> k1) (a :: k2) (b :: k3).
p (f a) (g b) -> Biff p f g a b
Biff (forall (p :: * -> * -> *) a b c.
Profunctor p =>
(a -> b) -> p b c -> p a c
lmap (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f) p (f b) (g c)
p)
  rmap :: forall b c a. (b -> c) -> Biff p f g a b -> Biff p f g a c
rmap b -> c
g (Biff p (f a) (g b)
p) = forall {k} {k1} {k2} {k3} (p :: k -> k1 -> *) (f :: k2 -> k)
       (g :: k3 -> k1) (a :: k2) (b :: k3).
p (f a) (g b) -> Biff p f g a b
Biff (forall (p :: * -> * -> *) b c a.
Profunctor p =>
(b -> c) -> p a b -> p a c
rmap (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap b -> c
g) p (f a) (g b)
p)
  dimap :: forall a b c d.
(a -> b) -> (c -> d) -> Biff p f g b c -> Biff p f g a d
dimap a -> b
f c -> d
g (Biff p (f b) (g c)
p) = forall {k} {k1} {k2} {k3} (p :: k -> k1 -> *) (f :: k2 -> k)
       (g :: k3 -> k1) (a :: k2) (b :: k3).
p (f a) (g b) -> Biff p f g a b
Biff (forall (p :: * -> * -> *) a b c d.
Profunctor p =>
(a -> b) -> (c -> d) -> p b c -> p a d
dimap (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f) (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap c -> d
g) p (f b) (g c)
p)

instance (Profunctor p, Profunctor q) => Profunctor (Product p q) where
  lmap :: forall a b c. (a -> b) -> Product p q b c -> Product p q a c
lmap  a -> b
f   (Pair p b c
p q b c
q) = forall {k} {k1} (f :: k -> k1 -> *) (g :: k -> k1 -> *) (a :: k)
       (b :: k1).
f a b -> g a b -> Product f g a b
Pair (forall (p :: * -> * -> *) a b c.
Profunctor p =>
(a -> b) -> p b c -> p a c
lmap a -> b
f p b c
p) (forall (p :: * -> * -> *) a b c.
Profunctor p =>
(a -> b) -> p b c -> p a c
lmap a -> b
f q b c
q)
  {-# INLINE lmap #-}
  rmap :: forall b c a. (b -> c) -> Product p q a b -> Product p q a c
rmap    b -> c
g (Pair p a b
p q a b
q) = forall {k} {k1} (f :: k -> k1 -> *) (g :: k -> k1 -> *) (a :: k)
       (b :: k1).
f a b -> g a b -> Product f g a b
Pair (forall (p :: * -> * -> *) b c a.
Profunctor p =>
(b -> c) -> p a b -> p a c
rmap b -> c
g p a b
p) (forall (p :: * -> * -> *) b c a.
Profunctor p =>
(b -> c) -> p a b -> p a c
rmap b -> c
g q a b
q)
  {-# INLINE rmap #-}
  dimap :: forall a b c d.
(a -> b) -> (c -> d) -> Product p q b c -> Product p q a d
dimap a -> b
f c -> d
g (Pair p b c
p q b c
q) = forall {k} {k1} (f :: k -> k1 -> *) (g :: k -> k1 -> *) (a :: k)
       (b :: k1).
f a b -> g a b -> Product f g a b
Pair (forall (p :: * -> * -> *) a b c d.
Profunctor p =>
(a -> b) -> (c -> d) -> p b c -> p a d
dimap a -> b
f c -> d
g p b c
p) (forall (p :: * -> * -> *) a b c d.
Profunctor p =>
(a -> b) -> (c -> d) -> p b c -> p a d
dimap a -> b
f c -> d
g q b c
q)
  {-# INLINE dimap #-}
  #. :: forall a b c (q :: * -> * -> *).
Coercible c b =>
q b c -> Product p q a b -> Product p q a c
(#.) q b c
f (Pair p a b
p q a b
q) = forall {k} {k1} (f :: k -> k1 -> *) (g :: k -> k1 -> *) (a :: k)
       (b :: k1).
f a b -> g a b -> Product f g a b
Pair (q b c
f forall (p :: * -> * -> *) a b c (q :: * -> * -> *).
(Profunctor p, Coercible c b) =>
q b c -> p a b -> p a c
#. p a b
p) (q b c
f forall (p :: * -> * -> *) a b c (q :: * -> * -> *).
(Profunctor p, Coercible c b) =>
q b c -> p a b -> p a c
#. q a b
q)
  {-# INLINE (#.) #-}
  .# :: forall a b c (q :: * -> * -> *).
Coercible b a =>
Product p q b c -> q a b -> Product p q a c
(.#) (Pair p b c
p q b c
q) q a b
f = forall {k} {k1} (f :: k -> k1 -> *) (g :: k -> k1 -> *) (a :: k)
       (b :: k1).
f a b -> g a b -> Product f g a b
Pair (p b c
p forall (p :: * -> * -> *) a b c (q :: * -> * -> *).
(Profunctor p, Coercible b a) =>
p b c -> q a b -> p a c
.# q a b
f) (q b c
q forall (p :: * -> * -> *) a b c (q :: * -> * -> *).
(Profunctor p, Coercible b a) =>
p b c -> q a b -> p a c
.# q a b
f)
  {-# INLINE (.#) #-}

instance (Profunctor p, Profunctor q) => Profunctor (Sum p q) where
  lmap :: forall a b c. (a -> b) -> Sum p q b c -> Sum p q a c
lmap a -> b
f (L2 p b c
x) = forall {k} {k1} (p :: k -> k1 -> *) (q :: k -> k1 -> *) (a :: k)
       (b :: k1).
p a b -> Sum p q a b
L2 (forall (p :: * -> * -> *) a b c.
Profunctor p =>
(a -> b) -> p b c -> p a c
lmap a -> b
f p b c
x)
  lmap a -> b
f (R2 q b c
y) = forall {k} {k1} (p :: k -> k1 -> *) (q :: k -> k1 -> *) (a :: k)
       (b :: k1).
q a b -> Sum p q a b
R2 (forall (p :: * -> * -> *) a b c.
Profunctor p =>
(a -> b) -> p b c -> p a c
lmap a -> b
f q b c
y)
  {-# INLINE lmap #-}
  rmap :: forall b c a. (b -> c) -> Sum p q a b -> Sum p q a c
rmap b -> c
g (L2 p a b
x) = forall {k} {k1} (p :: k -> k1 -> *) (q :: k -> k1 -> *) (a :: k)
       (b :: k1).
p a b -> Sum p q a b
L2 (forall (p :: * -> * -> *) b c a.
Profunctor p =>
(b -> c) -> p a b -> p a c
rmap b -> c
g p a b
x)
  rmap b -> c
g (R2 q a b
y) = forall {k} {k1} (p :: k -> k1 -> *) (q :: k -> k1 -> *) (a :: k)
       (b :: k1).
q a b -> Sum p q a b
R2 (forall (p :: * -> * -> *) b c a.
Profunctor p =>
(b -> c) -> p a b -> p a c
rmap b -> c
g q a b
y)
  {-# INLINE rmap #-}
  dimap :: forall a b c d. (a -> b) -> (c -> d) -> Sum p q b c -> Sum p q a d
dimap a -> b
f c -> d
g (L2 p b c
x) = forall {k} {k1} (p :: k -> k1 -> *) (q :: k -> k1 -> *) (a :: k)
       (b :: k1).
p a b -> Sum p q a b
L2 (forall (p :: * -> * -> *) a b c d.
Profunctor p =>
(a -> b) -> (c -> d) -> p b c -> p a d
dimap a -> b
f c -> d
g p b c
x)
  dimap a -> b
f c -> d
g (R2 q b c
y) = forall {k} {k1} (p :: k -> k1 -> *) (q :: k -> k1 -> *) (a :: k)
       (b :: k1).
q a b -> Sum p q a b
R2 (forall (p :: * -> * -> *) a b c d.
Profunctor p =>
(a -> b) -> (c -> d) -> p b c -> p a d
dimap a -> b
f c -> d
g q b c
y)
  {-# INLINE dimap #-}
  q b c
f #. :: forall a b c (q :: * -> * -> *).
Coercible c b =>
q b c -> Sum p q a b -> Sum p q a c
#. L2 p a b
x = forall {k} {k1} (p :: k -> k1 -> *) (q :: k -> k1 -> *) (a :: k)
       (b :: k1).
p a b -> Sum p q a b
L2 (q b c
f forall (p :: * -> * -> *) a b c (q :: * -> * -> *).
(Profunctor p, Coercible c b) =>
q b c -> p a b -> p a c
#. p a b
x)
  q b c
f #. R2 q a b
y = forall {k} {k1} (p :: k -> k1 -> *) (q :: k -> k1 -> *) (a :: k)
       (b :: k1).
q a b -> Sum p q a b
R2 (q b c
f forall (p :: * -> * -> *) a b c (q :: * -> * -> *).
(Profunctor p, Coercible c b) =>
q b c -> p a b -> p a c
#. q a b
y)
  {-# INLINE (#.) #-}
  L2 p b c
x .# :: forall a b c (q :: * -> * -> *).
Coercible b a =>
Sum p q b c -> q a b -> Sum p q a c
.# q a b
f = forall {k} {k1} (p :: k -> k1 -> *) (q :: k -> k1 -> *) (a :: k)
       (b :: k1).
p a b -> Sum p q a b
L2 (p b c
x forall (p :: * -> * -> *) a b c (q :: * -> * -> *).
(Profunctor p, Coercible b a) =>
p b c -> q a b -> p a c
.# q a b
f)
  R2 q b c
y .# q a b
f = forall {k} {k1} (p :: k -> k1 -> *) (q :: k -> k1 -> *) (a :: k)
       (b :: k1).
q a b -> Sum p q a b
R2 (q b c
y forall (p :: * -> * -> *) a b c (q :: * -> * -> *).
(Profunctor p, Coercible b a) =>
p b c -> q a b -> p a c
.# q a b
f)
  {-# INLINE (.#) #-}

instance (Functor f, Profunctor p) => Profunctor (Tannen f p) where
  lmap :: forall a b c. (a -> b) -> Tannen f p b c -> Tannen f p a c
lmap a -> b
f (Tannen f (p b c)
h) = forall {k} {k1} {k2} (f :: k -> *) (p :: k1 -> k2 -> k) (a :: k1)
       (b :: k2).
f (p a b) -> Tannen f p a b
Tannen (forall (p :: * -> * -> *) a b c.
Profunctor p =>
(a -> b) -> p b c -> p a c
lmap a -> b
f forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f (p b c)
h)
  {-# INLINE lmap #-}
  rmap :: forall b c a. (b -> c) -> Tannen f p a b -> Tannen f p a c
rmap b -> c
g (Tannen f (p a b)
h) = forall {k} {k1} {k2} (f :: k -> *) (p :: k1 -> k2 -> k) (a :: k1)
       (b :: k2).
f (p a b) -> Tannen f p a b
Tannen (forall (p :: * -> * -> *) b c a.
Profunctor p =>
(b -> c) -> p a b -> p a c
rmap b -> c
g forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f (p a b)
h)
  {-# INLINE rmap #-}
  dimap :: forall a b c d.
(a -> b) -> (c -> d) -> Tannen f p b c -> Tannen f p a d
dimap a -> b
f c -> d
g (Tannen f (p b c)
h) = forall {k} {k1} {k2} (f :: k -> *) (p :: k1 -> k2 -> k) (a :: k1)
       (b :: k2).
f (p a b) -> Tannen f p a b
Tannen (forall (p :: * -> * -> *) a b c d.
Profunctor p =>
(a -> b) -> (c -> d) -> p b c -> p a d
dimap a -> b
f c -> d
g forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f (p b c)
h)
  {-# INLINE dimap #-}
  #. :: forall a b c (q :: * -> * -> *).
Coercible c b =>
q b c -> Tannen f p a b -> Tannen f p a c
(#.) q b c
f (Tannen f (p a b)
h) = forall {k} {k1} {k2} (f :: k -> *) (p :: k1 -> k2 -> k) (a :: k1)
       (b :: k2).
f (p a b) -> Tannen f p a b
Tannen ((q b c
f forall (p :: * -> * -> *) a b c (q :: * -> * -> *).
(Profunctor p, Coercible c b) =>
q b c -> p a b -> p a c
#.) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f (p a b)
h)
  {-# INLINE (#.) #-}
  .# :: forall a b c (q :: * -> * -> *).
Coercible b a =>
Tannen f p b c -> q a b -> Tannen f p a c
(.#) (Tannen f (p b c)
h) q a b
f = forall {k} {k1} {k2} (f :: k -> *) (p :: k1 -> k2 -> k) (a :: k1)
       (b :: k2).
f (p a b) -> Tannen f p a b
Tannen ((forall (p :: * -> * -> *) a b c (q :: * -> * -> *).
(Profunctor p, Coercible b a) =>
p b c -> q a b -> p a c
.# q a b
f) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f (p b c)
h)
  {-# INLINE (.#) #-}