packages feed

pandora-0.4.5: Pandora/Paradigm/Schemes/UT.hs

module Pandora.Paradigm.Schemes.UT where

import Pandora.Core.Functor (type (:.), type (:=), type (~>))
import Pandora.Pattern.Semigroupoid ((.))
import Pandora.Pattern.Category (($), identity)
import Pandora.Pattern.Functor.Covariant (Covariant, Covariant ((-<$>-)), (-<$$>-))
import Pandora.Pattern.Functor.Contravariant (Contravariant)
import Pandora.Pattern.Functor.Semimonoidal (Semimonoidal (multiply_))
import Pandora.Pattern.Functor.Pointable (Pointable (point))
import Pandora.Pattern.Functor.Bindable (Bindable ((=<<)))
import Pandora.Pattern.Functor.Extractable (Extractable (extract))
import Pandora.Pattern.Functor.Traversable (Traversable ((<<-)))
import Pandora.Pattern.Transformer.Liftable (Liftable (lift))
import Pandora.Pattern.Transformer.Lowerable (Lowerable (lower))
import Pandora.Paradigm.Controlflow.Effect.Interpreted (Interpreted (Primary, run, unite))
import Pandora.Paradigm.Primary.Algebraic ((:*:) ((:*:)))

newtype UT ct cu t u a = UT (u :. t := a)

infixr 3 <.:>, >.:>, <.:<, >.:<

type (<.:>) = UT Covariant Covariant
type (>.:>) = UT Contravariant Covariant
type (<.:<) = UT Covariant Contravariant
type (>.:<) = UT Contravariant Contravariant

instance Interpreted (UT ct cu t u) where
	type Primary (UT ct cu t u) a = u :. t := a
	run ~(UT x) = x
	unite = UT

instance (Covariant t (->) (->), Covariant u (->) (->)) => Covariant (t <.:> u) (->) (->) where
	f -<$>- x = UT $ f -<$$>- run x

instance (Covariant u (->) (->), Semimonoidal t (->) (:*:) (:*:), Semimonoidal u (->) (:*:) (:*:)) => Semimonoidal (t <.:> u) (->) (:*:) (:*:) where
	multiply_ (UT x :*: UT y) = UT $ multiply_ @_ @(->) @(:*:) -<$>- multiply_ (x :*: y)

instance (Pointable t (->), Pointable u (->)) => Pointable (t <.:> u) (->) where
	point = UT . point . point

instance (Traversable t (->) (->), Bindable t (->), Semimonoidal u (->) (:*:) (:*:), Pointable u (->), Bindable u (->)) => Bindable (t <.:> u) (->) where
	f =<< UT x = UT $ ((identity =<<) -<$>-) . (run . f <<-) =<< x

instance (Extractable t (->), Extractable u (->)) => Extractable (t <.:> u) (->) where
	extract = extract . extract . run

instance Pointable t (->) => Liftable (UT Covariant Covariant t) where
	lift :: Covariant u (->) (->) => u ~> t <.:> u
	lift x = UT $ point @_ @(->) -<$>- x

instance Extractable t (->) => Lowerable (UT Covariant Covariant t) where
	lower :: Covariant u (->) (->) => t <.:> u ~> u
	lower (UT x) = extract @_ @(->) -<$>- x