monad-transformers

Haskell Best Practise: Early termination in Haskeline

孤街醉人 提交于 2019-12-23 09:47:29
问题 I am using the Haskeline package and I want to get three strings in a row from the command line before I do anything and I have come up with what seems to be a neat solution to me. But I am sure that there might be a better way to do it. I am looking for best practices while using the Haskeline package. Please evaluate the merits of the following example code: import System.Console.Haskeline import Control.Monad.Trans import Control.Monad.Maybe import Data.Maybe import Control.Monad main ::

State and IO Monads

六眼飞鱼酱① 提交于 2019-12-23 07:47:58
问题 I've been trying to wrap my head around the concept of monads and I've been experimenting with the following example: I have an Editor data-type that represents the state of a text document and some functions that work on it. data Editor = Editor { lines :: [Line], -- editor contents are kept line by line lineCount :: Int, -- holds length lines at all times caret :: Caret -- the current caret position -- ... some more definitions } deriving (Show) -- get the line at the given position (first

How to define MonadUnliftIO instance for a newtype with a phantom type-variable?

十年热恋 提交于 2019-12-22 07:11:21
问题 Related question - Is it safe to derive MonadThrow, MonadCatch, MonadBaseControl, MonadUnliftIO, etc? - where I had enabled, both - DeriveAnyClass and GeneralizedNewtypeDeriving to get the code to compile, but didn't bother looking at the ominous warnings. Now, that I am running my refactored code, it's throwing a runtime error: No instance nor default method for class operation >>= So, I removed DeriveAnyClass and kept ONLY GeneralizedNewtypeDeriving and have the following compile error: {-#

How do I add the Reader monad to Scotty's monad?

人盡茶涼 提交于 2019-12-22 05:40:10
问题 I'm trying to use Scotty to build a very simple API. I'd like to extend the Scotty monads such that my route handler actions are able to access an unchanging environment. I believe the way to do this would be to add a Reader monad to the stack. For now I just want to pass some Text data around. I've extended the Scotty monads as follows: type BrandyScottyM = ScottyT TL.Text (ReaderT T.Text IO) type BrandyActionM = ActionT TL.Text (ReaderT T.Text IO) https://github.com/stu-smith/Brandy/blob

Can GHC derive Functor and Applicative instances for a monad transformer?

淺唱寂寞╮ 提交于 2019-12-22 04:42:31
问题 I'm trying to implement MaybeT in the spirit of the mtl library. With this non-compiling solution: {-# LANGUAGE FlexibleInstances, MultiParamTypeClasses, UndecidableInstances #-} import Control.Monad import Control.Monad.Trans import Control.Monad.State newtype MaybeT m a = MaybeT { runMaybeT :: m (Maybe a) } instance (Monad m) => Monad (MaybeT m) where x >>= f = MaybeT $ runMaybeT x >>= maybe (return Nothing) (runMaybeT . f) return a = MaybeT $ return (Just a) fail _ = MaybeT $ return

bind a monadic value (m2 a) inside some other monad m1

狂风中的少年 提交于 2019-12-21 22:18:23
问题 Working in a Coding Dojo today I tried the following example :: IO () example = do input <- getLine parsed <- parseOnly parser input ... where parseOnly :: Parser a -> Either String a (from attoparsec ) of course the compiler complained that Either .. is not IO .. essentially telling me I am mixing monads. Of course this can be solved by case parseOnly parser input of .. -> .. which is kind of unelegant, I think. Also my guess is that somebody else had this problem earlier and the solution I

Why is there no MonadMask instance for ExceptT?

ぐ巨炮叔叔 提交于 2019-12-21 07:11:13
问题 Edward Kmett's exceptions library does not provide a MonadMask instance for ExceptT. Ben Gamari once asked about this and then concluded that it was explained by the documentation. This is the closest relevant-looking passage I can find: Note that this package does provide a MonadMask instance for CatchT . This instance is only valid if the base monad provides no ability to provide multiple exit. For example, IO or Either would be invalid base monads, but Reader or State would be acceptable.

Use StateT within Web.Scotty

ε祈祈猫儿з 提交于 2019-12-21 04:27:17
问题 I'm trying to make a silly webserver that stores data as State . I'm using Web.Scotty. I've used ReaderT before with scotty to access config, but following the same approach doesn't work here. It resets the state on every request. I want to set the initial state when the program starts, then have that same state stick around for the whole life of the program. How can I make this work? (The following creates a new state every request) {-# LANGUAGE OverloadedStrings #-} import Web.Scotty.Trans

Combining StateT and State monads

我只是一个虾纸丫 提交于 2019-12-21 03:26:15
问题 Lets say I have a function f :: State [Int] Int and a function: g :: StateT [Int] IO Int I want to use f in g and pass the state between them. Is there a library function for StateT (return . runState f) ? Or in general, given a monad transformer with a corresponding monad, is there a library function for it? 回答1: In even more general, what you're trying to do is apply a transformation to an inner layer of a transformer stack. For two arbitrary monads, the type signature might look something

Using servant with ReaderT IO a

人走茶凉 提交于 2019-12-20 21:02:25
问题 I'm using the servant library for my JSON API. I need some help to get a ServerT MyAPI (ReaderT a IO) monad stack working. Here's an example using ReaderT , but without integrating it with servant: -- this code works type TestAPI = "a" :> Get '[JSON] String :<|> "b" :> Get '[JSON] String test2 :: EitherT ServantErr IO String test2 = return "asdf" testServer :: Int -> Server TestAPI testServer code = test :<|> test2 where test :: EitherT ServantErr IO String test = liftIO $ runReaderT