Skip to content

Instantly share code, notes, and snippets.

@aljce
Last active September 25, 2016 16:28
Show Gist options
  • Save aljce/107ad1e27d6f05f589a34a691aeff073 to your computer and use it in GitHub Desktop.
Save aljce/107ad1e27d6f05f589a34a691aeff073 to your computer and use it in GitHub Desktop.
{-# LANGUAGE MultiParamTypeClasses,
FunctionalDependencies,
FlexibleInstances #-} --Three extensions compared to just type families
{-# LANGUAGE TypeFamilies #-}
module Mtl where
newtype ReaderT r m a = ReaderT { unReaderT :: r -> m a }
instance (Functor m) => Functor (ReaderT r m) where
fmap f (ReaderT x) = ReaderT (\k -> fmap f (x k))
instance (Applicative m) => Applicative (ReaderT r m) where
pure x = ReaderT (pure (pure x))
(ReaderT f) <*> (ReaderT x) = ReaderT (\k -> f k <*> x k)
instance (Monad m) => Monad (ReaderT r m) where
(ReaderT x) >>= f = ReaderT (\k -> x k >>= flip unReaderT k . f)
class Monad m => MonadReader m where
type Env m
ask :: m (Env m)
instance (Monad m) => MonadReader (ReaderT r m) where
type Env (ReaderT r m) = r
ask = ReaderT return
-- Fundeps version
-- class Monad m => MonadReader r m | m -> r where
-- ask :: m r
-- instance (Monad m) => MonadReader r (ReaderT r m) where
-- ask = ReaderT return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment