{-# LANGUAGE CPP #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE Safe #-}
-----------------------------------------------------------------------------
-- |
-- Copyright   :  (C) 2015-2018 Edward Kmett
-- License     :  BSD-style (see the file LICENSE)
--
-- Maintainer  :  Edward Kmett <ekmett@gmail.com>
-- Stability   :  experimental
-- Portability :  portable
--
----------------------------------------------------------------------------
module Data.Profunctor.Mapping
  ( Mapping(..)
  , CofreeMapping(..)
  , FreeMapping(..)
  -- * Traversing in terms of Mapping
  , wanderMapping
  -- * Closed in terms of Mapping
  , traverseMapping
  , closedMapping
  ) where

import Control.Arrow (Kleisli(..))
import Data.Bifunctor.Tannen
import Data.Distributive
import Data.Functor.Compose
import Data.Functor.Identity
import Data.Profunctor.Choice
import Data.Profunctor.Closed
import Data.Profunctor.Monad
import Data.Profunctor.Strong
import Data.Profunctor.Traversing
import Data.Profunctor.Types
import Data.Profunctor.Unsafe
#if __GLASGOW_HASKELL__ < 710
import Control.Applicative
#endif

class (Traversing p, Closed p) => Mapping p where
  -- | Laws:
  --
  -- @
  -- 'map'' '.' 'rmap' f ≡ 'rmap' ('fmap' f) '.' 'map''
  -- 'map'' '.' 'map'' ≡ 'dimap' 'Data.Functor.Compose.Compose' 'Data.Functor.Compose.getCompose' '.' 'map''
  -- 'dimap' 'Data.Functor.Identity.Identity' 'Data.Functor.Identity.runIdentity' '.' 'map'' ≡ 'id'
  -- @
  map' :: Functor f => p a b -> p (f a) (f b)
  map' = forall (p :: * -> * -> *) a b s t.
Mapping p =>
((a -> b) -> s -> t) -> p a b -> p s t
roam forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap

  roam :: ((a -> b) -> s -> t)
       -> p a b -> p s t
  roam (a -> b) -> s -> t
f = forall (p :: * -> * -> *) a b c d.
Profunctor p =>
(a -> b) -> (c -> d) -> p b c -> p a d
dimap (\s
s -> forall t b a. ((a -> b) -> t) -> Bar t b a
Bar forall a b. (a -> b) -> a -> b
$ \a -> b
ab -> (a -> b) -> s -> t
f a -> b
ab s
s) forall t a. Bar t a a -> t
lent forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (p :: * -> * -> *) (f :: * -> *) a b.
(Mapping p, Functor f) =>
p a b -> p (f a) (f b)
map'

newtype Bar t b a = Bar
  { forall t b a. Bar t b a -> (a -> b) -> t
runBar :: (a -> b) -> t }
  deriving forall a b. a -> Bar t b b -> Bar t b a
forall a b. (a -> b) -> Bar t b a -> Bar t b b
forall t b a b. a -> Bar t b b -> Bar t b a
forall t b a b. (a -> b) -> Bar t b a -> Bar t b b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: forall a b. a -> Bar t b b -> Bar t b a
$c<$ :: forall t b a b. a -> Bar t b b -> Bar t b a
fmap :: forall a b. (a -> b) -> Bar t b a -> Bar t b b
$cfmap :: forall t b a b. (a -> b) -> Bar t b a -> Bar t b b
Functor

lent :: Bar t a a -> t
lent :: forall t a. Bar t a a -> t
lent Bar t a a
m = forall t b a. Bar t b a -> (a -> b) -> t
runBar Bar t a a
m forall a. a -> a
id

instance Mapping (->) where
  map' :: forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
map' = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap
  roam :: forall a b s t. ((a -> b) -> s -> t) -> (a -> b) -> s -> t
roam (a -> b) -> s -> t
f = (a -> b) -> s -> t
f

instance (Monad m, Distributive m) => Mapping (Kleisli m) where
  map' :: forall (f :: * -> *) a b.
Functor f =>
Kleisli m a b -> Kleisli m (f a) (f b)
map' (Kleisli a -> m b
f) = forall (m :: * -> *) a b. (a -> m b) -> Kleisli m a b
Kleisli (forall (g :: * -> *) (f :: * -> *) a b.
(Distributive g, Functor f) =>
(a -> g b) -> f a -> g (f b)
collect a -> m b
f)
#if __GLASGOW_HASKELL__ >= 710
  roam :: forall a b s t.
((a -> b) -> s -> t) -> Kleisli m a b -> Kleisli m s t
roam (a -> b) -> s -> t
f = forall (m :: * -> *) a b. (a -> m b) -> Kleisli m a b
Kleisli forall (p :: * -> * -> *) a b c (q :: * -> * -> *).
(Profunctor p, Coercible c b) =>
q b c -> p a b -> p a c
#. forall (f :: * -> *) a b s t.
Distributive f =>
((a -> b) -> s -> t) -> (a -> f b) -> s -> f t
genMap (a -> b) -> s -> t
f forall (p :: * -> * -> *) a b c (q :: * -> * -> *).
(Profunctor p, Coercible b a) =>
p b c -> q a b -> p a c
.# forall (m :: * -> *) a b. Kleisli m a b -> a -> m b
runKleisli
#else
  -- We could actually use this implementation everywhere, but it's kind of a
  -- warty mess, and there have been rumblings of WrappedMonad deprecation.
  -- If/when GHC 7.8 moves out of the support window, this will vanish in a
  -- puff of cleanup.
  roam f = (Kleisli . (unwrapMonad .)) #. genMapW f .# ((WrapMonad .) . runKleisli)
    where
      genMapW
        :: (Monad m, Distributive m)
        => ((a -> b) -> s -> t)
        -> (a -> WrappedMonad m b) -> s -> WrappedMonad m t
      genMapW abst amb s = WrapMonad $ (\ab -> abst ab s) <$> distribute (unwrapMonad #. amb)
#endif

genMap :: Distributive f => ((a -> b) -> s -> t) -> (a -> f b) -> s -> f t
genMap :: forall (f :: * -> *) a b s t.
Distributive f =>
((a -> b) -> s -> t) -> (a -> f b) -> s -> f t
genMap (a -> b) -> s -> t
abst a -> f b
afb s
s = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\a -> b
ab -> (a -> b) -> s -> t
abst a -> b
ab s
s) (forall (g :: * -> *) (f :: * -> *) a.
(Distributive g, Functor f) =>
f (g a) -> g (f a)
distribute a -> f b
afb)

-- see <https://github.com/ekmett/distributive/issues/12>
instance (Applicative m, Distributive m) => Mapping (Star m) where
  map' :: forall (f :: * -> *) a b.
Functor f =>
Star m a b -> Star m (f a) (f b)
map' (Star a -> m b
f) = forall {k} (f :: k -> *) d (c :: k). (d -> f c) -> Star f d c
Star (forall (g :: * -> *) (f :: * -> *) a b.
(Distributive g, Functor f) =>
(a -> g b) -> f a -> g (f b)
collect a -> m b
f)
  roam :: forall a b s t. ((a -> b) -> s -> t) -> Star m a b -> Star m s t
roam (a -> b) -> s -> t
f = forall {k} (f :: k -> *) d (c :: k). (d -> f c) -> Star f d c
Star forall (p :: * -> * -> *) a b c (q :: * -> * -> *).
(Profunctor p, Coercible c b) =>
q b c -> p a b -> p a c
#. forall (f :: * -> *) a b s t.
Distributive f =>
((a -> b) -> s -> t) -> (a -> f b) -> s -> f t
genMap (a -> b) -> s -> t
f forall (p :: * -> * -> *) a b c (q :: * -> * -> *).
(Profunctor p, Coercible b a) =>
p b c -> q a b -> p a c
.# forall {k} (f :: k -> *) d (c :: k). Star f d c -> d -> f c
runStar

instance (Functor f, Mapping p) => Mapping (Tannen f p) where
  map' :: forall (f :: * -> *) a b.
Functor f =>
Tannen f p a b -> Tannen f p (f a) (f b)
map' = 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 b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall (p :: * -> * -> *) (f :: * -> *) a b.
(Mapping p, Functor f) =>
p a b -> p (f a) (f b)
map' forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k1} {k2} {k3} (f :: k1 -> *) (p :: k2 -> k3 -> k1)
       (a :: k2) (b :: k3).
Tannen f p a b -> f (p a b)
runTannen

wanderMapping :: Mapping p => (forall f. Applicative f => (a -> f b) -> s -> f t) -> p a b -> p s t
wanderMapping :: forall (p :: * -> * -> *) a b s t.
Mapping p =>
(forall (f :: * -> *). Applicative f => (a -> f b) -> s -> f t)
-> p a b -> p s t
wanderMapping forall (f :: * -> *). Applicative f => (a -> f b) -> s -> f t
f = forall (p :: * -> * -> *) a b s t.
Mapping p =>
((a -> b) -> s -> t) -> p a b -> p s t
roam ((forall a. Identity a -> a
runIdentity forall b c a. (b -> c) -> (a -> b) -> a -> c
.) forall (p :: * -> * -> *) a b c (q :: * -> * -> *).
(Profunctor p, Coercible c b) =>
q b c -> p a b -> p a c
#. forall (f :: * -> *). Applicative f => (a -> f b) -> s -> f t
f forall (p :: * -> * -> *) a b c (q :: * -> * -> *).
(Profunctor p, Coercible b a) =>
p b c -> q a b -> p a c
.# (forall a. a -> Identity a
Identity forall b c a. (b -> c) -> (a -> b) -> a -> c
.))

traverseMapping :: (Mapping p, Functor f) => p a b -> p (f a) (f b)
traverseMapping :: forall (p :: * -> * -> *) (f :: * -> *) a b.
(Mapping p, Functor f) =>
p a b -> p (f a) (f b)
traverseMapping = forall (p :: * -> * -> *) (f :: * -> *) a b.
(Mapping p, Functor f) =>
p a b -> p (f a) (f b)
map'

closedMapping :: Mapping p => p a b -> p (x -> a) (x -> b)
closedMapping :: forall (p :: * -> * -> *) a b x.
Mapping p =>
p a b -> p (x -> a) (x -> b)
closedMapping = forall (p :: * -> * -> *) (f :: * -> *) a b.
(Mapping p, Functor f) =>
p a b -> p (f a) (f b)
map'

newtype CofreeMapping p a b = CofreeMapping { forall (p :: * -> * -> *) a b.
CofreeMapping p a b
-> forall (f :: * -> *). Functor f => p (f a) (f b)
runCofreeMapping :: forall f. Functor f => p (f a) (f b) }

instance Profunctor p => Profunctor (CofreeMapping p) where
  lmap :: forall a b c.
(a -> b) -> CofreeMapping p b c -> CofreeMapping p a c
lmap a -> b
f (CofreeMapping forall (f :: * -> *). Functor f => p (f b) (f c)
p) = forall (p :: * -> * -> *) a b.
(forall (f :: * -> *). Functor f => p (f a) (f b))
-> CofreeMapping p a b
CofreeMapping (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) forall (f :: * -> *). Functor f => p (f b) (f c)
p)
  rmap :: forall b c a.
(b -> c) -> CofreeMapping p a b -> CofreeMapping p a c
rmap b -> c
g (CofreeMapping forall (f :: * -> *). Functor f => p (f a) (f b)
p) = forall (p :: * -> * -> *) a b.
(forall (f :: * -> *). Functor f => p (f a) (f b))
-> CofreeMapping p a b
CofreeMapping (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) forall (f :: * -> *). Functor f => p (f a) (f b)
p)
  dimap :: forall a b c d.
(a -> b) -> (c -> d) -> CofreeMapping p b c -> CofreeMapping p a d
dimap a -> b
f c -> d
g (CofreeMapping forall (f :: * -> *). Functor f => p (f b) (f c)
p) = forall (p :: * -> * -> *) a b.
(forall (f :: * -> *). Functor f => p (f a) (f b))
-> CofreeMapping p a b
CofreeMapping (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) forall (f :: * -> *). Functor f => p (f b) (f c)
p)

instance Profunctor p => Strong (CofreeMapping p) where
  second' :: forall a b c. CofreeMapping p a b -> CofreeMapping p (c, a) (c, b)
second' = forall (p :: * -> * -> *) (f :: * -> *) a b.
(Mapping p, Functor f) =>
p a b -> p (f a) (f b)
map'

instance Profunctor p => Choice (CofreeMapping p) where
  right' :: forall a b c.
CofreeMapping p a b -> CofreeMapping p (Either c a) (Either c b)
right' = forall (p :: * -> * -> *) (f :: * -> *) a b.
(Mapping p, Functor f) =>
p a b -> p (f a) (f b)
map'

instance Profunctor p => Closed (CofreeMapping p) where
  closed :: forall a b x.
CofreeMapping p a b -> CofreeMapping p (x -> a) (x -> b)
closed = forall (p :: * -> * -> *) (f :: * -> *) a b.
(Mapping p, Functor f) =>
p a b -> p (f a) (f b)
map'

instance Profunctor p => Traversing (CofreeMapping p) where
  traverse' :: forall (f :: * -> *) a b.
Traversable f =>
CofreeMapping p a b -> CofreeMapping p (f a) (f b)
traverse' = forall (p :: * -> * -> *) (f :: * -> *) a b.
(Mapping p, Functor f) =>
p a b -> p (f a) (f b)
map'
  wander :: forall a b s t.
(forall (f :: * -> *). Applicative f => (a -> f b) -> s -> f t)
-> CofreeMapping p a b -> CofreeMapping p s t
wander forall (f :: * -> *). Applicative f => (a -> f b) -> s -> f t
f = forall (p :: * -> * -> *) a b s t.
Mapping p =>
((a -> b) -> s -> t) -> p a b -> p s t
roam forall a b. (a -> b) -> a -> b
$ (forall a. Identity a -> a
runIdentity forall b c a. (b -> c) -> (a -> b) -> a -> c
.) forall (p :: * -> * -> *) a b c (q :: * -> * -> *).
(Profunctor p, Coercible c b) =>
q b c -> p a b -> p a c
#. forall (f :: * -> *). Applicative f => (a -> f b) -> s -> f t
f forall (p :: * -> * -> *) a b c (q :: * -> * -> *).
(Profunctor p, Coercible b a) =>
p b c -> q a b -> p a c
.# (forall a. a -> Identity a
Identity forall b c a. (b -> c) -> (a -> b) -> a -> c
.)

instance Profunctor p => Mapping (CofreeMapping p) where
  -- !@(#*&() Compose isn't representational in its second arg or we could use #. and .#
  map' :: forall (f :: * -> *) a b.
Functor f =>
CofreeMapping p a b -> CofreeMapping p (f a) (f b)
map' (CofreeMapping forall (f :: * -> *). Functor f => p (f a) (f b)
p) = forall (p :: * -> * -> *) a b.
(forall (f :: * -> *). Functor f => p (f a) (f b))
-> CofreeMapping p a b
CofreeMapping (forall (p :: * -> * -> *) a b c d.
Profunctor p =>
(a -> b) -> (c -> d) -> p b c -> p a d
dimap forall {k} {k1} (f :: k -> *) (g :: k1 -> k) (a :: k1).
f (g a) -> Compose f g a
Compose forall {k1} {k2} (f :: k1 -> *) (g :: k2 -> k1) (a :: k2).
Compose f g a -> f (g a)
getCompose forall (f :: * -> *). Functor f => p (f a) (f b)
p)
  roam :: forall a b s t.
((a -> b) -> s -> t) -> CofreeMapping p a b -> CofreeMapping p s t
roam (a -> b) -> s -> t
f (CofreeMapping forall (f :: * -> *). Functor f => p (f a) (f b)
p) =
     forall (p :: * -> * -> *) a b.
(forall (f :: * -> *). Functor f => p (f a) (f b))
-> CofreeMapping p a b
CofreeMapping forall a b. (a -> b) -> a -> b
$
       forall (p :: * -> * -> *) a b c d.
Profunctor p =>
(a -> b) -> (c -> d) -> p b c -> p a d
dimap (forall {k} {k1} (f :: k -> *) (g :: k1 -> k) (a :: k1).
f (g a) -> Compose f g a
Compose 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
fmap (\s
s -> forall t b a. ((a -> b) -> t) -> Bar t b a
Bar forall a b. (a -> b) -> a -> b
$ \a -> b
ab -> (a -> b) -> s -> t
f a -> b
ab s
s)) (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall t a. Bar t a a -> t
lent forall (p :: * -> * -> *) a b c (q :: * -> * -> *).
(Profunctor p, Coercible b a) =>
p b c -> q a b -> p a c
.# forall {k1} {k2} (f :: k1 -> *) (g :: k2 -> k1) (a :: k2).
Compose f g a -> f (g a)
getCompose) forall (f :: * -> *). Functor f => p (f a) (f b)
p

instance ProfunctorFunctor CofreeMapping where
  promap :: forall (p :: * -> * -> *) (q :: * -> * -> *).
Profunctor p =>
(p :-> q) -> CofreeMapping p :-> CofreeMapping q
promap p :-> q
f (CofreeMapping forall (f :: * -> *). Functor f => p (f a) (f b)
p) = forall (p :: * -> * -> *) a b.
(forall (f :: * -> *). Functor f => p (f a) (f b))
-> CofreeMapping p a b
CofreeMapping (p :-> q
f forall (f :: * -> *). Functor f => p (f a) (f b)
p)

instance ProfunctorComonad CofreeMapping where
  proextract :: forall (p :: * -> * -> *). Profunctor p => CofreeMapping p :-> p
proextract (CofreeMapping forall (f :: * -> *). Functor f => p (f a) (f b)
p) = forall a. Identity a -> a
runIdentity forall (p :: * -> * -> *) a b c (q :: * -> * -> *).
(Profunctor p, Coercible c b) =>
q b c -> p a b -> p a c
#. forall (f :: * -> *). Functor f => p (f a) (f b)
p forall (p :: * -> * -> *) a b c (q :: * -> * -> *).
(Profunctor p, Coercible b a) =>
p b c -> q a b -> p a c
.# forall a. a -> Identity a
Identity
  produplicate :: forall (p :: * -> * -> *).
Profunctor p =>
CofreeMapping p :-> CofreeMapping (CofreeMapping p)
produplicate (CofreeMapping forall (f :: * -> *). Functor f => p (f a) (f b)
p) = forall (p :: * -> * -> *) a b.
(forall (f :: * -> *). Functor f => p (f a) (f b))
-> CofreeMapping p a b
CofreeMapping (forall (p :: * -> * -> *) a b.
(forall (f :: * -> *). Functor f => p (f a) (f b))
-> CofreeMapping p a b
CofreeMapping (forall (p :: * -> * -> *) a b c d.
Profunctor p =>
(a -> b) -> (c -> d) -> p b c -> p a d
dimap forall {k} {k1} (f :: k -> *) (g :: k1 -> k) (a :: k1).
f (g a) -> Compose f g a
Compose forall {k1} {k2} (f :: k1 -> *) (g :: k2 -> k1) (a :: k2).
Compose f g a -> f (g a)
getCompose forall (f :: * -> *). Functor f => p (f a) (f b)
p))

-- | @FreeMapping -| CofreeMapping@
data FreeMapping p a b where
  FreeMapping :: Functor f => (f y -> b) -> p x y -> (a -> f x) -> FreeMapping p a b

instance Functor (FreeMapping p a) where
  fmap :: forall a b. (a -> b) -> FreeMapping p a a -> FreeMapping p a b
fmap a -> b
f (FreeMapping f y -> a
l p x y
m a -> f x
r) = forall (f :: * -> *) y b (p :: * -> * -> *) x a.
Functor f =>
(f y -> b) -> p x y -> (a -> f x) -> FreeMapping p a b
FreeMapping (a -> b
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. f y -> a
l) p x y
m a -> f x
r

instance Profunctor (FreeMapping p) where
  lmap :: forall a b c. (a -> b) -> FreeMapping p b c -> FreeMapping p a c
lmap a -> b
f (FreeMapping f y -> c
l p x y
m b -> f x
r) = forall (f :: * -> *) y b (p :: * -> * -> *) x a.
Functor f =>
(f y -> b) -> p x y -> (a -> f x) -> FreeMapping p a b
FreeMapping f y -> c
l p x y
m (b -> f x
r forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
f)
  rmap :: forall b c a. (b -> c) -> FreeMapping p a b -> FreeMapping p a c
rmap b -> c
g (FreeMapping f y -> b
l p x y
m a -> f x
r) = forall (f :: * -> *) y b (p :: * -> * -> *) x a.
Functor f =>
(f y -> b) -> p x y -> (a -> f x) -> FreeMapping p a b
FreeMapping (b -> c
g forall b c a. (b -> c) -> (a -> b) -> a -> c
. f y -> b
l) p x y
m a -> f x
r
  dimap :: forall a b c d.
(a -> b) -> (c -> d) -> FreeMapping p b c -> FreeMapping p a d
dimap a -> b
f c -> d
g (FreeMapping f y -> c
l p x y
m b -> f x
r) = forall (f :: * -> *) y b (p :: * -> * -> *) x a.
Functor f =>
(f y -> b) -> p x y -> (a -> f x) -> FreeMapping p a b
FreeMapping (c -> d
g forall b c a. (b -> c) -> (a -> b) -> a -> c
. f y -> c
l) p x y
m (b -> f x
r forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
f)
  q b c
g #. :: forall a b c (q :: * -> * -> *).
Coercible c b =>
q b c -> FreeMapping p a b -> FreeMapping p a c
#. FreeMapping f y -> b
l p x y
m a -> f x
r = forall (f :: * -> *) y b (p :: * -> * -> *) x a.
Functor f =>
(f y -> b) -> p x y -> (a -> f x) -> FreeMapping p a b
FreeMapping (q b c
g forall (p :: * -> * -> *) a b c (q :: * -> * -> *).
(Profunctor p, Coercible c b) =>
q b c -> p a b -> p a c
#. f y -> b
l) p x y
m a -> f x
r
  FreeMapping f y -> c
l p x y
m b -> f x
r .# :: forall a b c (q :: * -> * -> *).
Coercible b a =>
FreeMapping p b c -> q a b -> FreeMapping p a c
.# q a b
f = forall (f :: * -> *) y b (p :: * -> * -> *) x a.
Functor f =>
(f y -> b) -> p x y -> (a -> f x) -> FreeMapping p a b
FreeMapping f y -> c
l p x y
m (b -> f x
r forall (p :: * -> * -> *) a b c (q :: * -> * -> *).
(Profunctor p, Coercible b a) =>
p b c -> q a b -> p a c
.# q a b
f)

instance Strong (FreeMapping p) where
  second' :: forall a b c. FreeMapping p a b -> FreeMapping p (c, a) (c, b)
second' = forall (p :: * -> * -> *) (f :: * -> *) a b.
(Mapping p, Functor f) =>
p a b -> p (f a) (f b)
map'

instance Choice (FreeMapping p) where
  right' :: forall a b c.
FreeMapping p a b -> FreeMapping p (Either c a) (Either c b)
right' = forall (p :: * -> * -> *) (f :: * -> *) a b.
(Mapping p, Functor f) =>
p a b -> p (f a) (f b)
map'

instance Closed (FreeMapping p) where
  closed :: forall a b x. FreeMapping p a b -> FreeMapping p (x -> a) (x -> b)
closed = forall (p :: * -> * -> *) (f :: * -> *) a b.
(Mapping p, Functor f) =>
p a b -> p (f a) (f b)
map'

instance Traversing (FreeMapping p) where
  traverse' :: forall (f :: * -> *) a b.
Traversable f =>
FreeMapping p a b -> FreeMapping p (f a) (f b)
traverse' = forall (p :: * -> * -> *) (f :: * -> *) a b.
(Mapping p, Functor f) =>
p a b -> p (f a) (f b)
map'
  wander :: forall a b s t.
(forall (f :: * -> *). Applicative f => (a -> f b) -> s -> f t)
-> FreeMapping p a b -> FreeMapping p s t
wander forall (f :: * -> *). Applicative f => (a -> f b) -> s -> f t
f = forall (p :: * -> * -> *) a b s t.
Mapping p =>
((a -> b) -> s -> t) -> p a b -> p s t
roam ((forall a. Identity a -> a
runIdentity forall b c a. (b -> c) -> (a -> b) -> a -> c
.) forall (p :: * -> * -> *) a b c (q :: * -> * -> *).
(Profunctor p, Coercible c b) =>
q b c -> p a b -> p a c
#. forall (f :: * -> *). Applicative f => (a -> f b) -> s -> f t
f forall (p :: * -> * -> *) a b c (q :: * -> * -> *).
(Profunctor p, Coercible b a) =>
p b c -> q a b -> p a c
.# (forall a. a -> Identity a
Identity forall b c a. (b -> c) -> (a -> b) -> a -> c
.))

instance Mapping (FreeMapping p) where
  map' :: forall (f :: * -> *) a b.
Functor f =>
FreeMapping p a b -> FreeMapping p (f a) (f b)
map' (FreeMapping f y -> b
l p x y
m a -> f x
r) = forall (f :: * -> *) y b (p :: * -> * -> *) x a.
Functor f =>
(f y -> b) -> p x y -> (a -> f x) -> FreeMapping p a b
FreeMapping (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap f y -> b
l forall (p :: * -> * -> *) a b c (q :: * -> * -> *).
(Profunctor p, Coercible b a) =>
p b c -> q a b -> p a c
.# forall {k1} {k2} (f :: k1 -> *) (g :: k2 -> k1) (a :: k2).
Compose f g a -> f (g a)
getCompose) p x y
m (forall {k} {k1} (f :: k -> *) (g :: k1 -> k) (a :: k1).
f (g a) -> Compose f g a
Compose 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
fmap a -> f x
r)

instance ProfunctorFunctor FreeMapping where
  promap :: forall (p :: * -> * -> *) (q :: * -> * -> *).
Profunctor p =>
(p :-> q) -> FreeMapping p :-> FreeMapping q
promap p :-> q
f (FreeMapping f y -> b
l p x y
m a -> f x
r) = forall (f :: * -> *) y b (p :: * -> * -> *) x a.
Functor f =>
(f y -> b) -> p x y -> (a -> f x) -> FreeMapping p a b
FreeMapping f y -> b
l (p :-> q
f p x y
m) a -> f x
r

instance ProfunctorMonad FreeMapping where
  proreturn :: forall (p :: * -> * -> *). Profunctor p => p :-> FreeMapping p
proreturn p a b
p = forall (f :: * -> *) y b (p :: * -> * -> *) x a.
Functor f =>
(f y -> b) -> p x y -> (a -> f x) -> FreeMapping p a b
FreeMapping forall a. Identity a -> a
runIdentity p a b
p forall a. a -> Identity a
Identity
  projoin :: forall (p :: * -> * -> *).
Profunctor p =>
FreeMapping (FreeMapping p) :-> FreeMapping p
projoin (FreeMapping f y -> b
l (FreeMapping f y -> y
l' p x y
m x -> f x
r') a -> f x
r) = forall (f :: * -> *) y b (p :: * -> * -> *) x a.
Functor f =>
(f y -> b) -> p x y -> (a -> f x) -> FreeMapping p a b
FreeMapping ((f y -> b
l forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap f y -> y
l') forall (p :: * -> * -> *) a b c (q :: * -> * -> *).
(Profunctor p, Coercible b a) =>
p b c -> q a b -> p a c
.# forall {k1} {k2} (f :: k1 -> *) (g :: k2 -> k1) (a :: k2).
Compose f g a -> f (g a)
getCompose) p x y
m (forall {k} {k1} (f :: k -> *) (g :: k1 -> k) (a :: k1).
f (g a) -> Compose f g a
Compose 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
fmap x -> f x
r' forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> f x
r))