monads

Is the composition of an arbitrary monad with a traversable always a monad?

荒凉一梦 提交于 2019-11-27 02:10:15
问题 If I have two monads m and n , and n is traversable, do I necessarily have a composite m -over- n monad? More formally, here's what I have in mind: import Control.Monad import Data.Functor.Compose prebind :: (Monad m, Monad n) => m (n a) -> (a -> m (n b)) -> m (n (m (n b))) mnx `prebind` f = do nx <- mnx return $ do x <- nx return $ f x instance (Monad m, Monad n, Traversable n) => Monad (Compose m n) where return = Compose . return . return Compose mnmnx >>= f = Compose $ do nmnx <- mnmnx

Combining Free types

被刻印的时光 ゝ 提交于 2019-11-27 01:20:54
问题 I've been recently teaching myself about the Free monad from the free package, but I've come across a problem with it. I would like to have different free monads for different libraries, essentially I would like to build DSLs for different contexts, but I would also like to be able to combine them together. As an example: {-# LANGUAGE DeriveFunctor #-} module TestingFree where import Control.Monad.Free data BellsF x = Ring x | Chime x deriving (Functor, Show) type Bells = Free BellsF data

What happens to you if you break the monad laws?

谁说我不能喝 提交于 2019-11-27 01:13:56
问题 Do the compiler or the more "native" parts of the libraries (IO or functions that have access to black magic and the implementation) make assumptions about these laws? Will breaking them cause the impossible to happen? Or do they just express a programming pattern -- ie, the only person you'll annoy by breaking them are people who use your code and didn't expect you to be so careless? 回答1: The compiler doesn't make any assumptions about the laws, however, if your instance does not obey the

Constructing efficient monad instances on `Set` (and other containers with constraints) using the continuation monad

主宰稳场 提交于 2019-11-27 01:09:30
问题 Set , similarly to [] has a perfectly defined monadic operations. The problem is that they require that the values satisfy Ord constraint, and so it's impossible to define return and >>= without any constraints. The same problem applies to many other data structures that require some kind of constraints on possible values. The standard trick (suggested to me in a haskell-cafe post) is to wrap Set into the continuation monad. ContT doesn't care if the underlying type functor has any

Desugaring do-notation for Monads

て烟熏妆下的殇ゞ 提交于 2019-11-27 00:59:29
问题 As I'm learning Haskell I'm realizing that do notation is just syntatic sugar: a = do x <- [3..4] [1..2] return (x, 42) Translates into a = [3..4] >>= (\x -> [1..2] >>= (\_ -> return (x, 42))) I realize that I'll probably use do-notation but I'd like to understand whats going on in translation. So purely for pedagogical reasons, is there a way for ghc/ghci to give me the corresponding bind statements for a fairly complex monad written in do-notation? Edit. It turns out lambdabot on #haskell

Monad join function

可紊 提交于 2019-11-27 00:29:19
问题 While monads are represented in Haskell using the bind and return functions, they can also have another representation using the join function, such as discussed here. I know the type of this function is M(M(X))->M(X), but what does this actually do? 回答1: Actually, in a way, join is where all the magic really happens-- (>>=) is used mostly for convenience. All Functor -based type classes describe additional structure using some type. With Functor this extra structure is often thought of as a

Creative uses of monads

守給你的承諾、 提交于 2019-11-26 23:46:56
问题 I'm looking for creative uses of monads to learn from. I've read somewhere that monads have been used for example in AI, but being a monad newbie, I fail to see how. Please include a link to the source code and sample usages. No standard monads please. 回答1: Phil Wadler has written many papers on monads, but the one to read first is a lot of fun and will be accessible to any programmer; it's called The essence of functional programming. The paper includes source code and sample usages. A

Do statement under a where clause

与世无争的帅哥 提交于 2019-11-26 23:44:07
问题 I'm trying to convert IO [String] to [String] with <- binding; however, I need to use a do block to do that under a where statement, but Haskell complains about the indentation all the time. Here is the code: decompEventBlocks :: IO [String] -> IO [[String]] decompEventBlocks words | words' /= [] = block : (decompEventBlocks . drop $ (length block) words') | otherwise = [] where do words' <- words let block = (takeWhile (/="END") words') What is the reason for that ? And how can we use do

why isn't Validation a Monad? (scalaz7)

蹲街弑〆低调 提交于 2019-11-26 22:45:35
问题 an example use case: def div2(i: Int): Validation[String, Int] = if (i%2 == 0) Validation.success(i/2) else Validation.failure("odd") def div4(i: Int) = for { a <- div2(i) b <- div2(a) } yield b error : Unable to unapply type scalaz.Validation[String,Int] into a type constructor of kind M[_] that is classified by the type class scalaz.Bind I guess the error is caused by the compiler can't find a Monad instance for Validation[String, Int] I can make one for myself, like: object Instances {

Haskell pre-monadic I/O

陌路散爱 提交于 2019-11-26 22:41:20
问题 I wonder how I/O were done in Haskell in the days when IO monad was still not invented. Anyone knows an example. Edit: Can I/O be done without the IO Monad in modern Haskell? I'd prefer an example that works with modern GHC. 回答1: Before the IO monad was introduced, main was a function of type [Response] -> [Request] . A Request would represent an I/O action like writing to a channel or a file, or reading input, or reading environment variables etc.. A Response would be the result of such an