Last active
November 15, 2020 16:18
-
-
Save neongreen/11a35918bf64774d2d4ae68dabe357c7 to your computer and use it in GitHub Desktop.
MonadBase and MonadBaseControl instances
This file contains hidden or 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
{-# LANGUAGE DeriveFunctor #-} | |
{-# LANGUAGE GeneralizedNewtypeDeriving #-} | |
{-# LANGUAGE MultiParamTypeClasses #-} | |
{-# LANGUAGE TypeFamilies #-} | |
import Control.Concurrent.STM.TVar | |
import Control.Monad.Reader | |
import Control.Monad.Base | |
import Control.Monad.Trans.Control | |
data Env = Env {envBalance :: !(TVar Int)} | |
newtype AppM a = AppM { unAppM :: ReaderT Env IO a } | |
deriving (Functor, Applicative, Monad, MonadIO, MonadReader Env) | |
instance MonadBase IO AppM where | |
liftBase = AppM . liftBase | |
instance MonadBaseControl IO AppM where | |
type StM AppM a = a | |
liftBaseWith f = AppM (liftBaseWith (\g -> f (\(AppM x) -> g x))) | |
restoreM = AppM . restoreM |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment