monad-transformers

ReaderT static environment

纵然是瞬间 提交于 2019-12-12 21:24:07
问题 Declaration of the ReaderT monad transformer, which adds a static environment to a given monad. What does it mean to add a static environment to a given monad? Someone suggested that this is a duplicate to another question. I believe that this question is unique because I'm asking what it means to have a static environment and also my question pertains to ReaderT. Even if it is similar to Reader, they are still different. 回答1: It means that the environment cannot be updated: you can only read

Trouble with ST's type in a transformer stack

泪湿孤枕 提交于 2019-12-12 20:40:26
问题 I am having some trouble dealing with the forall quantifier in ST 's type. The following (simplified) code sample works fine, producing the expected result. import Control.Monad import Control.Monad.ST return' :: a -> ST s a return' = return function :: a -> a function x = runST $ do -- Some complicated expression eventually producing a result return' x This is fine, if all I want to do is have ST . But in my computation, I want a state transformer computation that might also fail, so I

Catch SomeException with ExceptT

泪湿孤枕 提交于 2019-12-11 09:48:51
问题 I'm trying to use the ExceptT monad transformer to catch any exception thrown by a function, like so: import Control.Exception import Control.Monad.Trans.Except badFunction :: ExceptT SomeException IO () badFunction = throw DivideByZero main :: IO () main = do r <- runExceptT badFunction case r of Left _ -> putStrLn "caught error" Right _ -> putStrLn "nope, didn't catch no error" ... but the exception happily flies by. What am I doing wrong? Edit : to clarify, the aim is to catch any

How do I define a function for a Monad Stack of State, Disjunction, and List?

半世苍凉 提交于 2019-12-11 08:53:48
问题 I've got a list monad List[Int] . I want to generate effects (Disjunction, and state) based on the values of the list. Here is my monad stack and the code to run the monad stack. My question is what should be proper way define checkNum so that I can generate the correct effects? My expected output should List(("", \/-(1), ("Error: 2", -\/(Throwable())), ("",\/-(3)), ("Error: 4", -\/(Throwable()))) import scalaz._ import Scalaz._ type EitherTH[F[_], A] = EitherT[F, Throwable,A] type StateTH[F[

Using the ExceptT monad with different error types

倖福魔咒の 提交于 2019-12-11 07:53:17
问题 I decided to refactor this code https://github.com/kototama/screepy/blob/21b5b12bc16f9c4e5ee699ca319fd6c6466d1421/src/Screepy/Auth.hs which deals with IO, Either and Maybe values with an ExceptT monad. Here is my refactored code: https://github.com/kototama/screepy/blob/dc2dcf5abfd72e29dd018c4a7377a02a1fa097a9/src/Screepy/Auth.hs This does not seem much of an improvement to me. What can I do to improve this code? And in general how to deal with code calling libraries that return different

Using ReaderT transformer in ScottyT (vs ActionT)

孤街浪徒 提交于 2019-12-11 07:35:42
问题 I'm trying to thread configuration through my Scotty based application using ReaderT monad transformer approach, and having trouble doing so. I have to use configuration both when defining routes (as some of them depend on the config) and when handling actual requests. The latter works just fine in the ActionT, but no matter what I try I just can't get the types right in ScottyT. Here's the minimal example I compiled from the ReaderT sample from Scotty GitHub repository: {-# LANGUAGE

Monad transformers: Implementation of a stack machine with MaybeT (State Stack)

。_饼干妹妹 提交于 2019-12-11 06:01:43
问题 I'm trying to implement a Maybe-State monad transformer and use it to implement a simple stack machine. The definitions of state monad and maybe should be correct. Now I'm trying to implement pop: pop :: MaybeT (State Stack) Int So that if the stack is empty it returns nothing, otherwise it returns Just <popped stack> . This is what I have so far: pop :: MaybeT (State Stack) Int pop = guard True (do (r:rs) <- get put rs return r) (Obviously True is just a dummy placeholder - I'll implement

How to make it a monad?

和自甴很熟 提交于 2019-12-11 05:39:29
问题 I am trying to validate a list of strings sequentially and define the validation result type like that: import cats._, cats.data._, cats.implicits._ case class ValidationError(msg: String) type ValidationResult[A] = Either[NonEmptyList[ValidationError], A] type ListValidationResult[A] = ValidationResult[List[A]] // not a monad :( I would like to make ListValidationResult a monad. Should I implement flatMap and pure manually or there is an easier way ? 回答1: I suggest you to take a totally

unwrap getInputLine result from Input

旧巷老猫 提交于 2019-12-11 04:44:56
问题 I'm getting a result from getInputline , whose type is: (MonadException m) => IO String -> InputT m (Maybe String) I'd like to get just the Maybe String part. I'm well aware that in general there is no way to strip a monad, as explained in this answer (and other answers in the same question). However, since I'm doing it inside an InputT , I guess it's possible, as proposed here. However, I can't just use liftIO , as the answer suggests, since the IO is inside a StateT . loop :: Counter ->

MonadTransControl instance for ProxyFast/ProxyCorrect

有些话、适合烂在心里 提交于 2019-12-11 02:57:20
问题 Using pipes, I'm trying to write an instance of MonadTransControl for the ProxyFast or ProxyCorrect type. This is what I've got: instance MonadTransControl (ProxyFast a' a b' b) where data StT (ProxyFast a' a b' b) a = StProxy { unStProxy :: ProxyFast a' a b' b Identity a} liftWith = undefined restoreT = undefined I have no idea how to write liftWith or restoreT. The instances for the other monad transformers all use a function that "swaps" the monads, for example EitherT e m a -> m (EitherT