module Pandora.Pattern.Functor.Extendable where

import Pandora.Core.Functor (type (:.), type (:=))
import Pandora.Pattern.Functor.Covariant (Covariant ((<$>)))

infixl 1 =>>
infixr 1 <<=, =<=, =>=

{- |
> When providing a new instance, you should ensure it satisfies:
> * Duplication interchange: comap (comap f) . duplicate ≡ duplicate . comap f
> * Extension interchange: extend f ≡ comap f . duplicate
-}

class Covariant t => Extendable t where
	{-# MINIMAL (=>>) #-}
	-- | Infix and flipped version of 'extend', the dual of '>>='
	(=>>) :: t a -> (t a -> b) -> t b

	-- | Flipped version of '>>=', the dual of '=<<'
	(<<=) :: (t a -> b) -> t a -> t b
	t a -> b
f <<= t a
x = t a
x t a -> (t a -> b) -> t b
forall (t :: * -> *) a b. Extendable t => t a -> (t a -> b) -> t b
=>> t a -> b
f
	-- | Prefix and flipped version of '=>>', the dual of 'bind'
	extend :: (t a -> b) -> t a -> t b
	extend t a -> b
f t a
t = t a
t t a -> (t a -> b) -> t b
forall (t :: * -> *) a b. Extendable t => t a -> (t a -> b) -> t b
=>> t a -> b
f
	-- | Clone existing structure, the dual of 'join'
	duplicate :: t a -> t :. t := a
	duplicate t a
t = t a
t t a -> (t a -> t a) -> (t :. t) := a
forall (t :: * -> *) a b. Extendable t => t a -> (t a -> b) -> t b
=>> (\t a
x -> t a
x)
	-- | Right-to-left Cokleisli composition
	(=<=) :: (t b -> c) -> (t a -> b) -> t a -> c
	t b -> c
f =<= t a -> b
g = \t a
x -> t b -> c
f ((t a -> b) -> t a -> t b
forall (t :: * -> *) a b. Extendable t => (t a -> b) -> t a -> t b
extend t a -> b
g t a
x)

	-- | Left-to-right Cokleisli composition
	(=>=) :: (t a -> b) -> (t b -> c) -> t a -> c
	t a -> b
f =>= t b -> c
g = \t a
x -> t b -> c
g ((t a -> b) -> t a -> t b
forall (t :: * -> *) a b. Extendable t => (t a -> b) -> t a -> t b
extend t a -> b
f t a
x)

	-- | Experimental methods
	($=>>) :: Covariant u => u :. t := a -> (t a -> b) -> u :. t := b
	(u :. t) := a
x $=>> t a -> b
f = (t a -> (t a -> b) -> t b
forall (t :: * -> *) a b. Extendable t => t a -> (t a -> b) -> t b
=>> t a -> b
f) (t a -> t b) -> ((u :. t) := a) -> (u :. t) := b
forall (t :: * -> *) a b. Covariant t => (a -> b) -> t a -> t b
<$> (u :. t) := a
x
	(<<=$) :: Covariant u => u :. t := a -> (t a -> b) -> u :. t := b
	(u :. t) := a
x <<=$ t a -> b
f = (t a -> (t a -> b) -> t b
forall (t :: * -> *) a b. Extendable t => t a -> (t a -> b) -> t b
=>> t a -> b
f) (t a -> t b) -> ((u :. t) := a) -> (u :. t) := b
forall (t :: * -> *) a b. Covariant t => (a -> b) -> t a -> t b
<$> (u :. t) := a
x