-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Reduce the use of unsafeCoerce Remove most uses of `unsafeCoerce`. Most of the rest are taken care of in my `mergeG` pull request.
- Loading branch information
1 parent
c477539
commit 5e037c0
Showing
7 changed files
with
163 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
{-# language RoleAnnotations #-} | ||
{-# language MultiParamTypeClasses #-} | ||
{-# language FlexibleInstances #-} | ||
{-# language CPP #-} | ||
module Control.Monad.ReaderIO | ||
( | ||
ReaderIO (..) | ||
) | ||
where | ||
|
||
import Control.Monad.Fix | ||
#if MIN_VERSION_base(4,10,0) | ||
import Control.Applicative | ||
#endif | ||
import Control.Monad | ||
import Control.Monad.Reader.Class | ||
import Control.Monad.IO.Class | ||
|
||
-- | An approximate clone of @RIO@ from the @rio@ package, but not based on | ||
-- @ReaderT@. The trouble with @ReaderT@ is that its third type argument has a | ||
-- @nominal@ role, so we can't coerce through it when it's wrapped in some | ||
-- other @data@ type. Ugh. | ||
newtype ReaderIO e a = ReaderIO { runReaderIO :: e -> IO a } | ||
type role ReaderIO representational representational | ||
|
||
instance Functor (ReaderIO e) where | ||
fmap = liftM | ||
{-# INLINE fmap #-} | ||
a <$ m = m >> pure a | ||
{-# INLINE (<$) #-} | ||
|
||
instance Applicative (ReaderIO e) where | ||
pure a = ReaderIO $ \_ -> pure a | ||
{-# INLINE pure #-} | ||
(<*>) = ap | ||
{-# INLINE (<*>) #-} | ||
#if MIN_VERSION_base(4,10,0) | ||
liftA2 = liftM2 | ||
{-# INLINE liftA2 #-} | ||
#endif | ||
(*>) = (>>) | ||
{-# INLINE (*>) #-} | ||
|
||
instance Monad (ReaderIO e) where | ||
ReaderIO q >>= f = ReaderIO $ \e -> q e >>= \a -> runReaderIO (f a) e | ||
{-# INLINE (>>=) #-} | ||
|
||
instance MonadFix (ReaderIO e) where | ||
mfix f = ReaderIO $ \e -> mfix $ \r -> runReaderIO (f r) e | ||
{-# INLINE mfix #-} | ||
|
||
instance MonadIO (ReaderIO e) where | ||
liftIO m = ReaderIO $ \_ -> m | ||
{-# INLINE liftIO #-} | ||
|
||
instance MonadReader e (ReaderIO e) where | ||
ask = ReaderIO pure | ||
{-# INLINE ask #-} | ||
local f (ReaderIO m) = ReaderIO (m . f) | ||
{-# INLINE local #-} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
This needs version bounds