Created
December 23, 2012 18:22
-
-
Save anonymous/4365046 to your computer and use it in GitHub Desktop.
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
{-# 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