haskell,monads,monad-transformers,state-monad

Your CheckerError monad is very similar to the Either monad. I will use the Either monad (and its monad transformer counterpart ErrorT) in my answer. There is a subtlety with monad trasformers: order matters. Effects in the "inner" monad have primacy over effects caused by the "outer" layers. Consider these...

haskell,monad-transformers,state-monad,threepenny-gui

StateT won't work in this case. The problem is that you need the state of your counter to persist between invocations of the button callback. Since the callback (and startGUI as well) produce UI actions, any StateT computation to be ran using them has to be self-contained, so that you...

haskell,types,monads,monad-transformers

You need to make PwdError an instance of the Error typeclass. The following should be sufficient, though I haven't tried to compile it: instance Error PwdError where strMsg = PwdError ...

haskell,state,monads,monad-transformers,statet

You just have two little mistakes. The first is here: n <- get <* modify (-m+) This gets the value of n before we subtract m. You almost certainly want n <- modify (-m+) >> get instead, or modify (-m+) n <- get if you prefer that spelling. The other...

haskell,monad-transformers,scotty

If you want to run each action in a separate reader, you don't need the more complex Scotty.Trans interface at all. You can just build you monad stack the other way around, with ReaderT on top. import qualified Data.Text.Lazy as T import Control.Monad.Reader import Web.Scotty type ActionD = ReaderT String...

haskell,monad-transformers,lens

I solved it by fixing package inconsistencies, thanks to a hint by Rufflewind (see comment on my question above). I first checked my existing package configuration with ghc-pkg check, and then used ghc-pkg unregister --force to remove all broken packages. I iteratively repeated it until no more broken packages were...

The trick here is to use lift to convert a list monad (ie an [a]) into a ReaderT env [] by using lift: lift :: (Monad m, MonadTrans t) => m a -> t m a or specialized to your monad stack: lift :: [a] -> ReaderT [(Int,Int)] [] a...

haskell,monad-transformers,scotty

Your approach looks okay. The type error is because you should use runReaderT rather than runReader (runReader is only for when you use Reader, which is ReaderT with just the dummy Identity monad below it).

Foo is neither a "base" monad, nor a monad transformer. defaultLiftBaseWith won't be helpful here, since you want the instance for Foo to be identical to the one for ReaderT Int IO. First, use GND to get the boring instances: {-# LANGUAGE GeneralizedNewtypeDeriving #-} import Control.Monad.Trans.Control import Control.Monad.Base import Control.Monad.Reader...

(>>=) doesn't need to deal with the inner monad (besides that it's not possible to implement it in a way you proposed, as chi showed), because we can just lift monadic values and get the desired semantics. lift :: Monad m => m a -> ContT r m a lift...

zoom has its own class for overloading, so no wonder just MonadState doesn't cut it. The Zoom class covers roughly the same ground as mtl, although it has somewhat more complicated machinery. In any case, you're not obligated to program in concrete monads. You can try enabling NoMonomorphismRestriction and inferring...

haskell,monads,monad-transformers

The MonadReader class is defined using the FunctionalDependencies extension, which allows declarations like class Monad m => MonadReader r m | m -> r where ... This means that for any monad m, the r is uniquely determined by it. Therefore, you can't have a single monad m that determines...

So, the basic problem with doing this within the requirements of the question is that there is no MTL library function for lifting from a list monad that may be arbitrary levels down. However, you can "cheat" a bit: The MonadPlus instance for the combined Monad is inherited from the...

haskell,monads,monad-transformers,maybe

In short: Replace lift by the MaybeT constructor. Note that newtype MaybeT m a = MaybeT { runMaybeT :: m (Maybe a) } and lift :: (MonadTrans t, Monad m) => m a -> t m a Your use of lift in x <- lift lol is at the type...

haskell,monad-transformers,state-monad,free-monad

Look at the type signature for runTest: it returns a value in a monad m as long as m is a MonadState FakeDB. So when you call runTest you need to bind it into such a monad, which IO is not (that's what the error message is telling you). Try...

Perhaps this is what you meant to do? I pushed the try into the specific call that could throw, and made use of bimapEitherT to convert the exception into Text. saveAuthKey :: ObjRef ProjectViewState -> Text -> ObjRef (Entity Server) -> IO (Either Text Text) saveAuthKey _ path (entityVal ....

haskell,monads,monad-transformers

I don't think you can lift this in general for any MonadBaseControl IO m. There are some ms for which we can. In General UnWebT m is isomorphic to WebT m which has a MonadTransControl instance. You can convert to and from WebT with mkWebT :: UnWebT m a ->...

scala,haskell,scalaz,monad-transformers,state-monad

The problem is that the modify you get with the usual imports is from State, and isn't going to help you with StateT. It's a good idea to start with the Haskell type signature: test3 :: (MonadState [Char] m, MonadState s (t m), MonadTrans t, Num s) => t m...

haskell,monad-transformers,haskeline

Since InputT IO is a MonadIO, you can use liftIO with this type: liftIO :: IO a -> InputT IO a So, do ... x <- liftIO $ runExceptT $ foo input case x of Left err -> ... Right res -> ... Alternatively, use Control.Monad.Trans.lift instead....

haskell,random,monad-transformers,maybe

Problem 1 Can't reproduce. There are already these instances for RandT. Problem 2 lookup returns Maybe, but you have a stack based on MaybeT. The reason why there is no MonadMaybe is that the corresponding type class is MonadPlus (or more general Alternative) - pure/return correspond to Just and empty/mzero...

haskell,monads,monad-transformers,state-monad

Measure is already defined in a following way: newtype Measure a = Measure { unMeasure :: [Cond] -> Sampler (a, [Cond]) } You can see that there is no place to store your Int, so you cannot make it a proper instance of MonadState. If you want to extend Measure...

haskell,monads,monad-transformers

Your offspring function returns a Rand computation. Rand, however, is just a specific MonadRandom instance (is is RandT over Identity), and not your FacilityApp monad. You should either change Rand to FacilityApp or, given that you are only using MonadRandom functions, generalise the type to any MonadRandom: offspring :: (MonadRandom...

haskell,yesod,typeclass,persistent,monad-transformers

Turns out I didn't realize you needed to repeat constraints inside of functions as well. This worked: _ <- rawSql "SELECT ?? FROM my_entities WHERE id = ? FOR UPDATE" [toPersistValue somethingId] :: MonadIO m => SqlPersistT m [Entity MyEntity] ...

haskell,monads,monad-transformers,scotty

The behaviour you are seeing is definitely the expected one: note the remark on the third argument in the documentation for scottyT: -> (m Response -> IO Response) -- Run monad m into IO, called at each action. What you could do is store the state external to the StateT...

haskell,monads,monad-transformers

I managed to implement the suggestion mentioned in the comments of the question. I give vty callbacks in IO that sends events down a Chan. Then i have another thread listening for those events and executing the appropriate actions in my own monad....

haskell,monad-transformers,state-monad

I think that if you can write an instance of MonadState for your transformer you can use modify without the lift: instance Monad m => MonadState (ZipperT s m a) where ... I must confess I am not sure about what part of the state modify should affect, though. I've...

haskell,monad-transformers,applicative

An implementation (taken from Tony Morris' functional programming course) could be (<*>) :: (Functor f, Monad f) => StateT s f (a -> b) -> StateT s f a -> StateT s f b StateT f <*> StateT a = StateT (\s -> (\(g, t) -> (\(z, u) -> (g...

haskell,monads,monad-transformers

Just look at the definition: unET (ET m) = m you know that the only field of ET has type: m (Maybe a). The unET function takes an ET m a and returns its only field, so the return type of unET must be the type of the field, i.e....

In your implementation, check is going to return an IO Bool regardless of what the state monad s is. So, when you pass simReset to check, which is a monadic action in the State Db monad, the return value is going to be State Db (IO Bool). How to fix...

haskell,monad-transformers,state-monad

commands is polymorphic, it has a type variable, m, but commandNames :: [String] doesn't have any type variables. This means that (barring some built-in defaults) type inference won't be able to infer the type variable for commands. There are two things you can do. You can provide a type for...

haskell,monad-transformers,type-families,ghc-mod

Scratch this, my eyes are fooling me. My mistake, since monad-control-1.x, MonadControl has had an associated type instead of an associated data type, as explained here.

mongodb,scala,monads,scalaz,monad-transformers

I'd "try" something like this: def tryOption[T](option: Option[T], message:String ="" ):Try[T] = option match { case Some(v) => Success(v) case None => Failure(new Exception(message)) } val docContentOpt = for { doc <- tryOption(mongoCollection.findOne(MongoDBObject("_id" -> id)),s"$id not found") content <- tryOption(doc.getAs[String]("content"), "content not found") } yield content Basically an Option to...

haskell,monad-transformers,state-monad

Update: I wasn't able to reproduce the issue, different GHC versions might optimize code differently. Nevertheless some ideas below: The forced evaluation you added doesn't do anything - it's never executed, as problem runs indefinitely. The reason why adding WriterT to the stack causes the memory leak could be precisely...

haskell,monads,monad-transformers,state-monad,maybe

The type I would recommend using is: StateT [Int] Maybe Int A really simple way to use Maybe/MaybeT is to just call mzero whenever you want to fail and mplus whenever you want to recover from a failed computation. This works even if they are layered within other monad transformers....

haskell,monad-transformers,lifting

You have two options: Find a monad morphism. This is often a matter of finding the right library; in this case hoist and generalize together should get you where you need to go. Make your State action more polymorphic. This is the commonly used one, and the recommended one; it...

scala,scalaz,monad-transformers

I found the answer myself, after some experimenting: val res = (1 to 3).map(anAsyncThingy).toList.sequenceU res.map(println) // List(1, 2, 3) Yay for scalaz! By the way, sequenceU is needed instead of sequence because scala isn't smart enough to figure out when you have OptionT[M, A] if you fix a type parameter...

haskell,monads,monad-transformers

The part p' <- return $ errorf p looks wrong. Here, return builds a monadic value in EitherT String IO. Assuming that errorf p = Left something the value which is being built is p' <- EitherT (return (Right (Left something))) where the above return builds an IO action. This...

haskell,monads,monad-transformers

The simplest thing is to use module-level encapsulation. Something like this: module Game (Game, loadResource) where data GameState -- = ... newtype Game = Game { runGame :: StateT GameState IO a } io :: IO a -> Game a io = Game . liftIO loadResource :: IO a ->...

haskell,monads,monad-transformers

You were almost there, test should be: test :: ReaderT Int IO String test = giveMeAMessage As for your other questions, I don't have time to answer just now but us servant developers should probably make it easier or better documented. Could you please read through the source for whichever...