monad-transformers

combining StateT with InputT

前提是你 提交于 2019-12-29 08:03:52
问题 It is a follow-up to this question. I'm trying to combine shell from @ErikR's answer in my InputT loop. main :: IO [String] main = do c <- makeCounter execStateT (repl c) [] repl :: Counter -> StateT [String] IO () repl c = lift $ runInputT defaultSettings loop where loop = do minput <- getLineIO $ in_ps1 $ c case minput of Nothing -> lift $ outputStrLn "Goodbye." Just input -> (liftIO $ process c input) >> loop getLineIO :: (MonadException m) => IO String -> InputT m (Maybe String) getLineIO

Is there a monad that doesn't have a corresponding monad transformer (except IO)?

喜你入骨 提交于 2019-12-28 03:31:09
问题 So far, every monad (that can be represented as a data type) that I have encountered had a corresponding monad transformer, or could have one. Is there such a monad that can't have one? Or do all monads have a corresponding transformer? By a transformer t corresponding to monad m I mean that t Identity is isomorphic to m . And of course that it satisfies the monad transformer laws and that t n is a monad for any monad n . I'd like to see either a proof (ideally a constructive one) that every

Haskell: Replace mapM in a monad transformer stack to achieve lazy evaluation (no space leaks)

余生长醉 提交于 2019-12-24 08:58:26
问题 It has already been discussed that mapM is inherently not lazy, e.g. here and here. Now I'm struggling with a variation of this problem where the mapM in question is deep inside a monad transformer stack. Here's a function taken from a concrete, working (but space-leaking) example using LevelDB that I put on gist.github.com: -- read keys [1..n] from db at DirName and check that the values are correct doRead :: FilePath -> Int -> IO () doRead dirName n = do success <- runResourceT $ do db <-

Avoiding Orphan Instances with Monad Transformers

僤鯓⒐⒋嵵緔 提交于 2019-12-24 07:19:26
问题 I have monad transformers corresponding to independent features of my app. In Weather module : class Monad m => WeatherT m where byCity :: String -> m WeatherData newtype MockWeather m a = MockWeather { ... } deriving (Functor, Applicative, Monad, MonadTrans) instance Monad m => WeatherT (MockWeather m) where ... In Counter module : class Monad m => CounterT m where increment :: m Int current :: m Int newtype MockCounter m a = MockCounter { ... } deriving (Functor, Applicative, Monad,

Nested applicative functors of different types in Haskell

纵然是瞬间 提交于 2019-12-24 03:41:11
问题 I'd like to make the nested applicative functors of different types. For example, nested simple functors of different types (in ghci) work fine: Prelude> ((+2) <$>) <$> (Just [1..4]) Just [3,4,5,6] But for applicative functors of different types: Prelude> ((*) <$>) <$> (Just [1,2,3]) <*> (Just [4,5,6,7]) <interactive>:56:1: error: * Couldn't match type `[Integer -> Integer]' with `[Integer] -> b' isn't working! I want to obtain something like this: Just [4,5,6,7,8,10,12,14,12,15,18,21] I know

Scala simplify nested monads

≡放荡痞女 提交于 2019-12-24 02:32:33
问题 I have some code written in Lift. Basically its nested Box (similar monad to Option). I'd like to simplify it a little bit if possible. Preferably add type parameter so this could be easily changed to string or double if needed. Here is the code tryo(r.param("boolean parameter").map(_.toBoolean)).map(_.openOr(false)).openOr(false) "tryo" is helper function to catch and wrap results in Box if exception occurs and r is Req object. "param" function returns Box[String] (that comes from request

How to flatten IO (IO ())?

寵の児 提交于 2019-12-23 13:22:46
问题 I'm just learning Haskell and monad transformers and I've found myself with an IO (IO ()) that I'd like to flatten into just IO (). I'm sure that I'm doing something wrong, but can't pinpoint exactly where I'm getting lost. Here is a simplified example of what I'm trying to do. This is a convoluted way of implementing echo , but it illustrates the problem. userInput :: Monad m => ReaderT (IO String) m (IO String) userInput = ask echo :: Monad m => ReaderT (IO String) m (IO ()) echo =

Constructing minimal Haskell example on error-handling in the State Monad

我是研究僧i 提交于 2019-12-23 13:16:14
问题 I'm twisting my brain into knots trying to understand how to combine the State monad with Maybe . Let's start with a concrete (and intentionally trivial/unnecessary) example in which we use a State monad to find the sum of a list of numbers: import Control.Monad.State list :: [Int] list = [1,4,5,6,7,0,3,2,1] adder :: Int adder = evalState addState list addState :: State [Int] Int addState = do ms <- get case ms of [] -> return 0 (x:xs) -> put xs >> fmap (+x) addState Cool. Now let's modify it

Flatten monad stack

£可爱£侵袭症+ 提交于 2019-12-23 12:33:50
问题 So I have this sort of code all over my first serious haskell project: f :: (MonadTrans t) => ExceptT () (t (StateT A B)) C f = do mapExceptT lift $ do lift $ do ... lift $ do ... r <- ... ... return r >>= \r -> ... There definitely may be something wrong about how I try to achieve my goals (there might be simpler ways how to do it) but currently I am interested in learning how to handle a stack of monad transformers in some nicer way, if there is one. This is the only way I figured out how

MonadTransControl instance for a custom monad

最后都变了- 提交于 2019-12-23 10:38:51
问题 The docs for monad-control provide an example on how to create an instance of MonadTransControl using defaultLiftWith and defaultRestoreT . The example is for the following newtype : newtype CounterT m a = CounterT {unCounterT :: StateT Int m a} This example can be adjusted to work for any newtype that is defined using only one "elementary" monad transformer (such as the ones from transformers or mtl ). But what about the case where the stack contains two "elementary" transformers? For