Advertisement
Guest User

Monad transformers

a guest
Jun 3rd, 2018
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. {-# LANGUAGE FlexibleInstances, GeneralizedNewtypeDeriving, DeriveFunctor #-}
  2.  
  3. import Data.Functor.Kan.Ran
  4. import Control.Compose
  5. import Control.Monad
  6. import Control.Monad.Trans
  7. import Data.Pointed
  8.  
  9. instance (Functor f, Monad g) => Monad(Ran f((:.) f g)) where
  10. return x = Ran(O. (return<$>). ($x))
  11. Ran f >>= f2 = Ran(\f3 -> O(join<$>unO(f(unO.($f3).runRan.f2))))
  12.  
  13. instance (Functor f, Monad g) => Applicative(Ran f((:.) f g)) where
  14. pure = return
  15. (<*>) = ap
  16.  
  17. newtype Lift g f t = Lift(Ran f((:.) f g) t) deriving (Functor, Applicative, Monad)
  18.  
  19. instance (Monad g) => MonadTrans(Lift g) where
  20. lift f = Lift$ Ran(\f2-> O$ f >>= f2 >>= return.return)
  21.  
  22. -------------------------------------
  23. -- Ex. for various base monads.
  24.  
  25. tell' :: (Monoid x, Monad f) => x -> Lift((,) x) f ()
  26. tell' x = Lift(Ran(\f->O$(,) x <$> f()))
  27.  
  28. zero' :: (Monad f, MonadPlus g) => Lift g f x
  29. zero' = Lift(Ran(const$ O$ return mzero))
  30.  
  31. -- winitzki
  32. class (Monad f) => Rigid f where
  33. fx ::(t->f x)->f(t->x)
  34.  
  35. ask' :: (Rigid f) => Lift((->) x) f x
  36. ask' = Lift(Ran(O. fx))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement