monad-transformers

What is the difference between different orderings of the same monad transformers?

瘦欲@ 提交于 2019-12-20 08:58:02
问题 I am attempting to define an API to express a particular type of procedure in my program. newtype Procedure a = { runProcedure :: ? } There is state, consisting of a mapping of IDs to records: type ID = Int data Record = { ... } type ProcedureState = Map ID Record There are three basic operations: -- Declare the current procedure invalid and bail (similar to some definitions of fail for class Monad) abort :: Procedure () -- Get a record from the shared state; abort if the record does not

Is it safe to derive MonadThrow, MonadCatch, MonadBaseControl, MonadUnliftIO, etc?

和自甴很熟 提交于 2019-12-20 04:31:07
问题 I'm refactoring some old code, which is in a polymorphic, but type-class constrained, monad: class ( MonadIO m , MonadLogger m , MonadLoggerIO m , MonadThrow m , MonadCatch m , MonadMask m , MonadBaseControl IO m , MonadUnliftIO) => HasLogging m where In the older code the application's main monad was... type AppM = ReaderT Env IO ...which will now change to... newtype AppM (features :: [FeatureFlag]) a = AppM (ReaderT Env IO a) deriving (Functor, Applicative, Monad, MonadReader Env, MonadIO)

stacking StateT in scalaz

。_饼干妹妹 提交于 2019-12-19 13:39:05
问题 I'm trying to understand Monad Transformers in Scala by porting some examples from this tutorial by Dan Piponi: http://blog.sigfpe.com/2006/05/grok-haskell-monad-transformers.html I did a couple of easy ones: import Control.Monad.State import Control.Monad.Identity test1 = do a <- get modify (+1) b <- get return (a,b) test2 = do a <- get modify (++"1") b <- get return (a,b) go1 = evalState test1 0 go2 = evalState test2 "0" becomes: import scalaz._, Scalaz._ val test1 = for { a <- get[Int] _ <

Customising composition of Future, Either and Writer in Scalaz

五迷三道 提交于 2019-12-19 04:14:09
问题 This is a follow up to my previous question: Sequencing both Scalaz WriterT and Either with for-yield The following code block is an example of sequencing Future , Either and Writer using the EitherT and WriterT monad transformers; the following question is about how to subtly change the behaviour of that stack of transformers. import scalaz._, Scalaz._ class Example[F[_], L] (val logFn: (String) => L)(implicit val f: Monad[F], l: Monoid[L]) { type T = Throwable type EF[α] = EitherT[F, T, α]

How to design a monadic stack?

天大地大妈咪最大 提交于 2019-12-18 11:45:23
问题 How do you design and build your monadic stacks? For the first time I need to build a monadic stack (using transformers) to solve a real world problem, but I'm not thoroughly sure in which order to stack the transformers. As you already know, as long as a computation has kind * -> * , basically anything can play the role of the inner monad in a transformer, thus a couple of questions: Should some particular transformer be at the top of the stack (e.g. ReaderT? WriterT?) What should drive the

Why is there no IO transformer in Haskell?

蓝咒 提交于 2019-12-18 10:26:37
问题 Every other monad comes with a transformer version, and from what I know the idea of a transformer is a generic extension of monads. Following how the other transformers are build, IOT would be something like newtype IOT m a = IOT { runIOT :: m (IO a) } for which I could make up useful applications on the spot: IOT Maybe can either do an IO action or nothing, IOT [] can build a list that can later be sequence d. So why is there no IO transformer in Haskell? (Notes: I've seen this post on

exceptions and monad transformers

不羁的心 提交于 2019-12-17 23:49:13
问题 I'm using the EitherT monad transformer. Combining it with the IO monad, I'm afraid I would get an exception and it would not be caught. Indeed the exception just passes through: import Control.Monad.Trans import Control.Error import System.Directory main = runEitherT testEx >>= print testEx :: EitherT String IO () testEx = lift $ removeFile "non existing filename" But the EitherT otherwise fits the bill perfectly to convey to callers the error. So I want to use that, not throw exceptions...

Stateful computation with different types of short-circuit (Maybe, Either)

天涯浪子 提交于 2019-12-13 14:22:56
问题 I am trying to find the most elegant way of converting the following stateful imperative piece of code to pure functional representation (preferably in Haskell to use abstraction that its Monad implementation offers). However I am not yet good at combining different monads using transformers and the like. It seems to me, that analyzing other's takes on such tasks helps the best when learning how to do it myself. The imperative code: while (true) { while (x = get()) { // Think of this as

How to preserve information when failing?

a 夏天 提交于 2019-12-13 14:19:00
问题 I'm writing some code that uses the StateT monad transformer to keep track of some stateful information (logging and more). The monad I'm passing to StateT is very simple: data CheckerError a = Bad {errorMessage :: Log} | Good a deriving (Eq, Show) instance Monad CheckerError where return x = Good x fail msg = Bad msg (Bad msg) >>= f = Bad msg (Good x) >>= f = f x type CheckerMonad a = StateT CheckerState CheckerError a It's just a Left and Right variant. What troubles me is the definition of

Kleisli[Future, Context, \/] to Kleisli[EitherT, Context, …]

☆樱花仙子☆ 提交于 2019-12-13 01:22:10
问题 As I want to combine Kleisli that works on long methods Future that can fail Either , I need to stack the effect. Here is the resulting code to stack the effect in the Kleisli. Is there an existing combinator in scalaz ? type FutureEitherT[A] = EitherT[Future, String, A] def toKleisliEitherTFromDisjunction[A](f: Kleisli[Future, Context,String \/ A]) = Kleisli[FutureEitherT, Context, A] { ctx => EitherT(f(ctx)) } I've tried without success f.liftMK[FutureEitherT] , but unfortunately, the third