Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {-# LANGUAGE FlexibleInstances, GeneralizedNewtypeDeriving, DeriveFunctor #-}
- import Data.Functor.Kan.Ran
- import Control.Compose
- import Control.Monad
- import Control.Monad.Trans
- import Data.Pointed
- instance (Functor f, Monad g) => Monad(Ran f((:.) f g)) where
- return x = Ran(O. (return<$>). ($x))
- Ran f >>= f2 = Ran(\f3 -> O(join<$>unO(f(unO.($f3).runRan.f2))))
- instance (Functor f, Monad g) => Applicative(Ran f((:.) f g)) where
- pure = return
- (<*>) = ap
- newtype Lift g f t = Lift(Ran f((:.) f g) t) deriving (Functor, Applicative, Monad)
- instance (Monad g) => MonadTrans(Lift g) where
- lift f = Lift$ Ran(\f2-> O$ f >>= f2 >>= return.return)
- -------------------------------------
- -- Ex. for various base monads.
- tell' :: (Monoid x, Monad f) => x -> Lift((,) x) f ()
- tell' x = Lift(Ran(\f->O$(,) x <$> f()))
- zero' :: (Monad f, MonadPlus g) => Lift g f x
- zero' = Lift(Ran(const$ O$ return mzero))
- -- winitzki
- class (Monad f) => Rigid f where
- fx ::(t->f x)->f(t->x)
- ask' :: (Rigid f) => Lift((->) x) f x
- ask' = Lift(Ran(O. fx))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement