Skip to content

Instantly share code, notes, and snippets.

Created December 23, 2012 18:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/4365046 to your computer and use it in GitHub Desktop.
Save anonymous/4365046 to your computer and use it in GitHub Desktop.
{-# LANGUAGE GeneralizedNewtypeDeriving, FlexibleInstances, MultiParamTypeClasses #-}
import Control.Monad.State
import Control.Monad.Trans
-- Stuff from haskeline. MonadException is something that haskeline requires for whatever reason.
class MonadIO m => MonadException m
newtype InputT m a = InputT (m a) deriving (Monad, MonadIO)
-- So I add a new class MonadInput
class MonadException m => MonadInput t m where
liftInput :: InputT m a -> t m a
instance MonadException m => MonadInput InputT m where
liftInput = id
-- Now the MonadException constraint is implied
myInputFunc :: MonadInput t m => t m (Maybe String)
myInputFunc = liftInput $ undefined
-- Stuff from my own transformer. This requires that m be MonadIO because it needs to store state to disk
data FitState = FitState
newtype FitStateT m a = FitStateT (StateT FitState m a) deriving (Monad, MonadTrans, MonadIO)
class MonadIO m => MonadFitState t m where
liftFitState :: FitStateT m a -> t m a
instance MonadIO m => MonadFitState FitStateT m where
liftFitState = id
-- Now the MonadIO constraint is implied as well
myFitStateFunc :: MonadFitState t m => t m ()
myFitStateFunc = liftFitState $ undefined
-- So I added a newtype wrapper around my main routine's transformers
newtype Routine m a = Routine (FitStateT (InputT m) a)
deriving (Monad, MonadIO)
-- And then made it instances of each new class
instance MonadException m => MonadInput Routine m where
liftInput = Routine . lift
instance MonadIO m => MonadFitState Routine m where
-- liftFitState = undefined
liftFitState = Routine -- This fails with an error.
--This would have been the ideal end result. But I can't quite make it work.
{-myMainRoutineFunc :: (MonadInput t m, MonadFitState t m) => t m ()
myMainRoutineFunc = do
myFitStateFunc
myInputFunc
return ()
-}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment