Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bypass Coercible Type for Deriving Monad* Classes of New Transformers Composed of Transformers #149

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions Control/Monad/Reader.hs
Expand Up @@ -40,6 +40,8 @@ module Control.Monad.Reader (
-- * MonadReader class
MonadReader.MonadReader(..),
MonadReader.asks,
-- * Lifting helper type
MonadReader.LiftingReader,
-- * The Reader monad
Reader,
runReader,
Expand Down
25 changes: 23 additions & 2 deletions Control/Monad/Reader/Class.hs
@@ -1,9 +1,11 @@
{-# LANGUAGE Safe #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE MultiParamTypeClasses #-}
-- Search for UndecidableInstances to see why this is needed
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE StandaloneKindSignatures #-}

Check failure on line 6 in Control/Monad/Reader/Class.hs

View workflow job for this annotation

GitHub Actions / Native: GHC 8.8 on ubuntu-latest

Unsupported extension: StandaloneKindSignatures
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE Trustworthy #-}
-- Needed because the CPSed versions of Writer and State are secretly State
-- wrappers, which don't force such constraints, even though they should legally
-- be there.
Expand Down Expand Up @@ -48,6 +50,7 @@
module Control.Monad.Reader.Class (
MonadReader(..),
asks,
LiftingReader(..),
) where

import qualified Control.Monad.Trans.Cont as Cont
Expand All @@ -68,7 +71,8 @@
import Control.Monad.Trans.Select (SelectT (SelectT), runSelectT)
import qualified Control.Monad.Trans.RWS.CPS as CPSRWS
import qualified Control.Monad.Trans.Writer.CPS as CPS
import Control.Monad.Trans.Class (lift)
import Control.Monad.Trans.Class (MonadTrans(lift))
import Data.Kind (Type)

-- ----------------------------------------------------------------------------
-- class MonadReader
Expand Down Expand Up @@ -202,3 +206,20 @@
r <- ask
local f (runSelectT m (local (const r) . c))
reader = lift . reader

type LiftingReader :: ((Type -> Type) -> Type -> Type) -> (Type -> Type) -> Type -> Type
newtype LiftingReader t m a = LiftingReader (t m a)
deriving (Functor, Applicative, Monad, MonadTrans)

instance MonadReader r m => MonadReader r (LiftingReader (ReaderT r') m) where
ask = lift ask
local f = mapLiftingReader (ReaderT.mapReaderT (local f))
reader = lift . reader

instance (MonadReader r m, Monoid w) => MonadReader r (LiftingReader (LazyRWS.RWST r' w s) m) where
ask = lift ask
local f = mapLiftingReader (LazyRWS.mapRWST (local f))
reader = lift . reader

mapLiftingReader :: (t m a -> t n b) -> LiftingReader t m a -> LiftingReader t n b
mapLiftingReader = coerce

Check failure on line 225 in Control/Monad/Reader/Class.hs

View workflow job for this annotation

GitHub Actions / Native: GHC 9.0 on ubuntu-latest

Variable not in scope: