Skip to content

Instantly share code, notes, and snippets.

@neongreen
Last active November 15, 2020 16:18
Show Gist options
  • Save neongreen/11a35918bf64774d2d4ae68dabe357c7 to your computer and use it in GitHub Desktop.
Save neongreen/11a35918bf64774d2d4ae68dabe357c7 to your computer and use it in GitHub Desktop.
MonadBase and MonadBaseControl instances
{-# 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