monad-transformers

Monad and MonadIO for custom type

♀尐吖头ヾ 提交于 2020-01-06 14:16:05
问题 I have a Logger type of kind * -> * which can take any type and log the value in a file. I am trying to implement this in a monadic way so that I log and keep working the same. My code looks like import Control.Applicative import Control.Monad import System.IO import Control.Monad.IO.Class instance Functor Logger where fmap = liftM instance Applicative Logger where pure = return (<*>) = ap newtype Logger a = Logger a deriving (Show) instance Monad (Logger) where return = Logger Logger logStr

How to combine two different monads

拟墨画扇 提交于 2020-01-04 05:21:20
问题 I'm testing a REST server. I hit it in the IO monad and simulate it in State Db where Db tracks the supposed state of the server. The following function is supposed to run both versions and compare the results... check :: (Eq a, MonadState d s) => s a -> IO a -> s (IO Bool) -- or: check :: (Eq a, MonadState d s, MonadIO i) => s a -> i a -> s (i Bool) check _ _ = (return.return) False -- for now but when I try it with these simplest possible functions ... simReset :: State Db () realReset ::

What purpose does the complexity of `Except` serve in Haskell?

巧了我就是萌 提交于 2020-01-02 04:56:31
问题 I understand (I think) that there is a close relationship between Either and Except in Haskell, and that it is easy to convert from one to the other. But I'm a bit confused about best practices for handling errors in Haskell and under what circumstances and scenarios I would choose one over the other. For example, in the example provided in Control.Monad.Except , Either is used in the definition type LengthMonad = Either LengthError so that calculateLength "abc" is Right 3 If instead one were

Is there a library or typeclass for getting the transformer version of a monad?

≯℡__Kan透↙ 提交于 2020-01-02 01:18:08
问题 In my current project I've run into the need to turn various monads into their transformer counterparts e.g. stateT :: Monad m => State s a -> StateT s m a stateT stf = StateT $ return . runState stf It's trivial to write these utility functions for the monads I need, but I was wondering if there already exists a library that contains this functionality for the standard monads and maybe a typeclass that abstracts this sort of transformation. Something like class (Monad f, MonadTrans t) =>

Examples of Haskell Applicative Transformers

北慕城南 提交于 2020-01-01 02:32:27
问题 The wiki on www.haskell.org tells us the following about Applicative Transformers: So where are applicative transformers? The answer is, that we do not need special transformers for applicative functors since they can be combined in a generic way. http://www.haskell.org/haskellwiki/Applicative_functor#Applicative_transfomers I tried the following in order to try to combine a bunch of applicative functors. But all I got was bunch of errors. Here is the code: import Control.Applicative import

Has anyone ever encountered a Monad Transformer in the wild?

北城余情 提交于 2019-12-31 08:07:31
问题 In my area of business - back office IT for a financial institution - it is very common for a software component to carry a global configuration around, to log its progress, to have some kind of error handling / computation short circuit... Things that can be modelled nicely by Reader-, Writer-, Maybe-monads and the like in Haskell and composed together with monad transformers. But there seem to some drawbacks: The concept behind monad transformers is quite tricky and hard to understand,

Can I use different workflows simultaneously in F#?

独自空忆成欢 提交于 2019-12-31 04:37:08
问题 I need my state to be passed along while being able to chain functions with the maybe workflow. Is there a way for 2 workflows to share the same context? If no, what is the way of doing it? UPDATE: Well, I have a state that represents a segment of available ID's for the entities that I am going to create in the database. So once an ID is acquired the state has to be transformed to a newer state with the next available ID and thrown away so that nobody can use it again. I don't want to mutate

Modular Program Design - Combining Monad Transformers in Monad Agnostic functions

落爺英雄遲暮 提交于 2019-12-29 14:54:26
问题 I am trying to come up with a modular program design and I, once again, kindly request your help. As a follow-up to these following posts Monad Transformers vs passing Parameters and Large Scale Design in Haskell, I am trying to build two independent modules that use Monad Transformers but expose Monad-agnostic functions, then combine a Monad-agnostic function from each of these modules into a new Monad-agnostic function. I have been unable to run the combining function e.g. how do I call

Modular Program Design - Combining Monad Transformers in Monad Agnostic functions

落花浮王杯 提交于 2019-12-29 14:52:09
问题 I am trying to come up with a modular program design and I, once again, kindly request your help. As a follow-up to these following posts Monad Transformers vs passing Parameters and Large Scale Design in Haskell, I am trying to build two independent modules that use Monad Transformers but expose Monad-agnostic functions, then combine a Monad-agnostic function from each of these modules into a new Monad-agnostic function. I have been unable to run the combining function e.g. how do I call

How to get ReaderT to work with another monad transformer?

匆匆过客 提交于 2019-12-29 08:06:53
问题 I would like to embed ReaderT into another monad transformer. How do I do this? The example below uses Scotty but I think it would be the same with any other monad. {-# LANGUAGE OverloadedStrings #-} import qualified Web.Scotty import Web.Scotty.Trans import Data.Text.Lazy import Control.Monad.IO.Class (liftIO) import Control.Monad.Trans.Reader import Control.Monad.Trans data Config = Config Text main :: IO () main = do let config = Config "Hello World" -- how to I make this line work?