module Pandora.Pattern.Functor.Bindable (Bindable (..)) where
import Pandora.Core.Functor (type (:.), type (:=))
import Pandora.Core.Morphism ((%))
import Pandora.Pattern.Category (identity)
import Pandora.Pattern.Functor.Covariant (Covariant ((<$>)))
infixl 1 >>=
infixr 1 =<<, <=<, >=>
class Covariant t => Bindable t where
{-# MINIMAL (>>=) #-}
(>>=) :: t a -> (a -> t b) -> t b
(=<<) :: (a -> t b) -> t a -> t b
(=<<) = (%) (>>=)
bind :: (a -> t b) -> t a -> t b
bind f t = t >>= f
join :: t :. t := a -> t a
join t = t >>= identity
(>=>) :: (a -> t b) -> (b -> t c) -> (a -> t c)
f >=> g = \x -> f x >>= g
(<=<) :: (b -> t c) -> (a -> t b) -> (a -> t c)
(<=<) = (%) (>=>)
($>>=) :: Covariant u => (a -> t b) -> u :. t := a -> u :. t := b
f $>>= x = (>>= f) <$> x
(>>=$) :: (t b -> c) -> (a -> t b) -> t a -> c
f >>=$ g = f <$> (>>= g)
instance Bindable ((->) e) where
f >>= g = \x -> g (f x) x