{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE Safe #-}
{-# LANGUAGE TypeOperators #-}

module Data.Bifunctor.Functor
  ( (:->)
  , BifunctorFunctor(..)
  , BifunctorMonad(..)
  , biliftM
  , BifunctorComonad(..)
  , biliftW
  ) where

-- | Using parametricity as an approximation of a natural transformation in two arguments.
type (:->) p q = forall a b. p a b -> q a b
infixr 0 :->

class BifunctorFunctor t where
  bifmap :: (p :-> q) -> t p :-> t q

class BifunctorFunctor t => BifunctorMonad t where
  bireturn :: p :-> t p
  bibind   :: (p :-> t q) -> t p :-> t q
  bibind p :-> t q
f = forall {k} {k} (t :: (k -> k -> *) -> k -> k -> *)
       (p :: k -> k -> *).
BifunctorMonad t =>
t (t p) :-> t p
bijoin forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k} {k} {k} {k} (t :: (k -> k -> *) -> k -> k -> *)
       (p :: k -> k -> *) (q :: k -> k -> *).
BifunctorFunctor t =>
(p :-> q) -> t p :-> t q
bifmap p :-> t q
f
  bijoin   :: t (t p) :-> t p
  bijoin = forall {k} {k} (t :: (k -> k -> *) -> k -> k -> *)
       (p :: k -> k -> *) (q :: k -> k -> *).
BifunctorMonad t =>
(p :-> t q) -> t p :-> t q
bibind forall a. a -> a
id
  {-# MINIMAL bireturn, (bibind | bijoin) #-}

biliftM :: BifunctorMonad t => (p :-> q) -> t p :-> t q
biliftM :: forall {k} {k} (t :: (k -> k -> *) -> k -> k -> *)
       (p :: k -> k -> *) (q :: k -> k -> *).
BifunctorMonad t =>
(p :-> q) -> t p :-> t q
biliftM p :-> q
f = forall {k} {k} (t :: (k -> k -> *) -> k -> k -> *)
       (p :: k -> k -> *) (q :: k -> k -> *).
BifunctorMonad t =>
(p :-> t q) -> t p :-> t q
bibind (forall {k} {k} (t :: (k -> k -> *) -> k -> k -> *)
       (p :: k -> k -> *).
BifunctorMonad t =>
p :-> t p
bireturn forall b c a. (b -> c) -> (a -> b) -> a -> c
. p :-> q
f)
{-# INLINE biliftM #-}

class BifunctorFunctor t => BifunctorComonad t where
  biextract :: t p :-> p
  biextend :: (t p :-> q) -> t p :-> t q
  biextend t p :-> q
f = forall {k} {k} {k} {k} (t :: (k -> k -> *) -> k -> k -> *)
       (p :: k -> k -> *) (q :: k -> k -> *).
BifunctorFunctor t =>
(p :-> q) -> t p :-> t q
bifmap t p :-> q
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k} {k} (t :: (k -> k -> *) -> k -> k -> *)
       (p :: k -> k -> *).
BifunctorComonad t =>
t p :-> t (t p)
biduplicate
  biduplicate :: t p :-> t (t p)
  biduplicate =  forall {k} {k} (t :: (k -> k -> *) -> k -> k -> *)
       (p :: k -> k -> *) (q :: k -> k -> *).
BifunctorComonad t =>
(t p :-> q) -> t p :-> t q
biextend forall a. a -> a
id
  {-# MINIMAL biextract, (biextend | biduplicate) #-}

biliftW :: BifunctorComonad t => (p :-> q) -> t p :-> t q
biliftW :: forall {k} {k} (t :: (k -> k -> *) -> k -> k -> *)
       (p :: k -> k -> *) (q :: k -> k -> *).
BifunctorComonad t =>
(p :-> q) -> t p :-> t q
biliftW p :-> q
f = forall {k} {k} (t :: (k -> k -> *) -> k -> k -> *)
       (p :: k -> k -> *) (q :: k -> k -> *).
BifunctorComonad t =>
(t p :-> q) -> t p :-> t q
biextend (p :-> q
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k} {k} (t :: (k -> k -> *) -> k -> k -> *)
       (p :: k -> k -> *).
BifunctorComonad t =>
t p :-> p
biextract)
{-# INLINE biliftW #-}