monad-transformers

Making a custom monad transformer an instance of MonadError

对着背影说爱祢 提交于 2019-12-07 05:29:50
问题 I want to make my monad transformer to be an instance of MonadError if the transformed monad is an instance. Basically I want my transformer to behave as the built-in transformers do, for example there is a MonadError instance for StateT : MonadError e m => MonadError e (StateT s m) I tried doing this: instance MonadError e m => MonadError e (MyMonadT m) But GHC started complaining about undecidable instances, apparently the MTL library just enables undecidable instances, but is there any way

Adjoint functors determine monad transformers, but where's lift?

只愿长相守 提交于 2019-12-07 02:46:54
问题 I'm intrigued by the construction described here for determining a monad transformer from adjoint functors. Here's some code that summarizes the basic idea: {-# LANGUAGE MultiParamTypeClasses #-} import Control.Monad newtype Three g f m a = Three { getThree :: g (m (f a)) } class (Functor f, Functor g) => Adjoint f g where counit :: f (g a) -> a unit :: a -> g (f a) instance (Adjoint f g, Monad m) => Monad (Three g f m) where return = Three . fmap return . unit m >>= f = Three $ fmap (>>=

Design of interface abstraction

亡梦爱人 提交于 2019-12-07 01:27:24
问题 Currently, I try to write a small game program (Skat) as a hobby project. Skat is a trick-taking game were two players play against a single player. As there are different kinds of players (lokal player, network player, computer, etc.), I wanted to abstract the interface to the player. My basic idea is to use a typeclass Player , that defines all kind of things, a player have to do and to know (playing a card, get notified about who won the trick, etc). Then, the whole game is just done by a

Non type-variable argument in the constraint: MonadError Failure m

百般思念 提交于 2019-12-06 18:50:38
问题 I have defined a custom error type: data Failure = NetworkError Message | UserIsTooStupid Message | InvalidOperation Message | UnexpectedError Message type Message = String I am trying to use MonadError with my error type: loadJSON :: (Aeson.FromJSON v, MonadIO m, MonadError Failure m) => URI -> m v loadJSON uri = do body <- liftIO $ getResponseBody =<< simpleHTTP (getRequest uri) case Aeson.decode body of Just json -> return json Nothing -> throwError $ SerialisationError "Couldn't

How to transform disjunction of Future to Future of disjunction

╄→尐↘猪︶ㄣ 提交于 2019-12-06 06:30:29
问题 I have the result of a method: val res: Future[Int] Xor Future[String] = getResult(x) and would like to transform it and use it as Future[Int Xor String] I could not extrapolate my use case from the herding cats blog and am not sure whether a monad transformer would be the right tool here, perhaps rather some form of traverse ? Xor from cats stands in for any disjunction. Scalaz \/ or stdlib Either would be fine as well (though I would prefer a biased disjunction). Thank you 回答1: Just as

How do you save a tree data structure to binary file in Haskell

给你一囗甜甜゛ 提交于 2019-12-06 05:31:37
问题 I'm trying to save a simple (but quite big) Tree structure into a binary file using Haskell. The structure looks something like this: -- For simplicity assume each Node has only 4 childs data Tree = Node [Tree] | Leaf [Int] And here is how I need the data look on disk: Each node starts with four 32-bit offsets to it's children, then follow the childs. I don't care much about the leafs, let's say it's just n consecutive 32-bit numbers. For practival purposes I would need some node labels or

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

痞子三分冷 提交于 2019-12-05 18:00:02
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 to define type LengthMonad = Except LengthError then calculateLength "abc" would be ExceptT (Identity

Scotty Using MongoDB

随声附和 提交于 2019-12-05 15:25:10
I'm relatively new to Haskell, and this is my first time working with monad transformers. I'd really appreciate some help. runQuery :: Pipe -> Query -> ActionM (Either Failure [Document]) runQuery pipe query = access pipe master "nutrition" (find query >>= rest) main = do pipe <- runIOE $ connect $ host "127.0.0.1" scotty 3000 $ do post "/" $ do b <- body let user :: Either String User = eitherDecode b case user of Left err -> text . pack $ "Could not decode the user:" ++ err ++ ":\n" ++ (show b) Right u -> do let query::Query = (select ["i_name" =: ["$in" =: map (unpack . name) (foods u)]]

Applicative instance for MaybeT m assumes Monad m

时间秒杀一切 提交于 2019-12-05 13:15:44
问题 I've been using the Haxl monad (described here: http://www.reddit.com/r/haskell/comments/1le4y5/the_haxl_project_at_facebook_slides_from_my_talk), which has the interesting feature that <*> for its Applicative instance isn't the same as ap from Control.Monad. This is a key feature that allows it to do concurrent computations without blocking. For example, if hf and ha are long computations, then let hf :: Haxl (a -> b) = ... ha :: Haxl a = ... in do f <- hf a <- ha return (f a) will do them

Implementing an MFunctor instance for RVarT

跟風遠走 提交于 2019-12-05 13:02:01
Is it possible to implement an MFunctor instance for RVarT ? So far I've come up with the following: {-# LANGUAGE RankNTypes #-} import Data.RVar -- from rvar import Control.Monad.Trans.Class (lift) -- from transformers hoistRVarT :: Monad m => (forall t. n t -> m t) -> RVarT n a -> RVarT m a hoistRVarT f rv = sampleRVarTWith (lift . f) rv However this can not be used as a definition of hoist for MFunctor , due to the Monad constraint on m incurred by lift . The problem is, that I couldn't find another way to lift the the resulting monad into RVarT without lift . But I think conceptually it