monads

Haskell Monad Join Function

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? ...

How practical is it to embed the core of a language with an effectful function space (like ML) into Haskell?

As Moggi proposed 20 years ago, the effectful function space -> of languages like ML can be decomposed into the standard total function space => plus a strong monad T to capture effects. A -> B decomposes to A => (T B) Now, Haskell supports monads, including an IO monad that appears sufficient for the effects in ML, and it has a ...

State monads: trading one pattern for another?

So I'm writing a game in Haskell, and I'm expressing a player's turn as a series of state-altering functions that correlate to various turn phases. Originally, this looks something like: let game' = phase1 game game'' = phase2 game' -- etc. Prime candidate for State monadosity, right? This leads to the more elegant: do phase1 ...

Wadler, "Monads for Functional Programming," Section 2.8

Edit II: Ah, okay: I wasn't understanding how a and b were being bound in the definition of eval! Now I do. If anyone's interested, this is a diagram tracking a and b. I'm a pretty big fan of diagrams. Drawing arrows really improved my Haskell, I swear. A Diagram of an eval call (PDF) Sometimes I feel really dense. In section 2.8 of...

Explanation of Monad laws

From a gentle introduction to Haskell, there are the following monad laws. Can anyone intuitively explain what they mean? return a >>= k = k a m >>= return = m xs >>= return . f = fmap f xs m >>= (\x -> k x >>= h) = (m >>= k) >>= h Here is my attempted explanation: We expect the return function ...

help with reader monad

I am new at haskell, I have to write a program context-aware,so I thought I can use the Reader Monad for keeping the context read from a file, I know how to read the file puting the content in a list of tuplessomething like [([Char],[Char])], but I do not know how to implement the Reader Monad for making the environment available to all...

Use for the identity monad in Clojure

I've been reading an excellent introduction to monads for Clojure programmers. The article illustrates that the Identity monad is functionally equivalent to Clojure's let and that the Sequence/List monad is equivalent to for. When the article gets to monad transformers, it shows an example combining the Maybe and Sequence monads. Ok, s...

Is every method returning `this` a monad?

Is every method on a class which returs this a monad? ...

Haskell: Scope of variable when using lambda expression with bind functions

The following line works as expected, but I am a little concerned why: getLine >>= \x-> getLine >>= \y-> return [x, y] Consider the addition of parenthesis to scope the lambda expressions: getLine >>= (\x-> getLine) >>= (\y-> return [x, y]) The second line is errorneous because x is not in scope when used in the return, and I am ha...

Functor / Applicative instances for State in Haskell

After reading (and skimming some sections of) Wadler's paper on monads, I decided to work through the paper more closely, defining functor and applicative instances for each of the monads he describes. Using the type synonym type M a = State -> (a, State) type State = Int Wadler uses to define the state monad, I have the following (us...

Monads and Actors

I've been trying to find anything that discusses when you should favor the use of monads over actors (in concurrency scenarios), but I've found nothing. In particular, I'm wondering about the use of the Reactive Extensions (LINQ to Events) vs. F#'s MailboxProcessor. Please give examples in addition to any philosophical reasoning you migh...

Using Scala's Delimited Continuations for implicit Monads

Hello, I'm playing with some kind of DSL defined by an monadic interface. Since applying the monad using a bunch of flatMap applications is kind of cumbersome and I find for-comprehension syntactically not that beautiful, I'm trying to implicitely mix monadic and non monadic code using delimited continuations. It's actually working fi...

Combine state with IO actions

Suppose I have a state monad such as: data Registers = Reg {...} data ST = ST {registers :: Registers, memory :: Array Int Int} newtype Op a = Op {runOp :: ST -> (ST, a)} instance Monad Op where return a = Op $ \st -> (st, a) (>>=) stf f = Op $ \st -> let (st1, a1) = runOp stf st (...

Why can Haskell exceptions only be caught inside the IO monad?

Can anybody explain why exceptions may be thrown outside the IO monad, but may only be caught inside it? ...

Monads vs. Arrows

I'm broadly familiar with the concepts of monads and arrows as used in functional programming. I also understand that they can be used to solve similar kinds of problems. However - I'm still a bit confused about how to select which one to use in any given situation. When should I use monads and when should I use arrows? ...

convert do notation to bind function

Hi I know that the following "do" notation's "bind" function is equivalent to getLine >>= \line -> putStrLn do line <- getLine putStrLn line But how is the following notation equivalent to bind function? do line1 <- getLine putStrLn "enter second line" line2 <- getLine return (line1,line2) ...

Context sensitive data processing in F#

I recently finished a project in which I was generating lists of strings, and I was wondering about the best way to do this. The string generation was context sensitive to determine if it was acceptable(it was a sequence of plays in a game, so you had to know what the last play was) The way I did it was with a function that was passed ...

Some languages and uses for monads

Hi guys, According to you, which language do you think would be the best for implementing monads (Python/Ruby/LISP)?,also can anyone tell me some possible uses of monads (please give examples),like exceptions? Thanks in advance ...

Haskell: Why do the Maybe and Either types behave differently when used as Monads?

I'm trying to get my head around error handling in Haskell. I've found the article "8 ways to report errors in Haskell" but I'm confused as to why Maybe and Either behave differently. For example: import Control.Monad.Error myDiv :: (Monad m) => Float -> Float -> m Float myDiv x 0 = fail "My divison by zero" myDiv x y = return (x / y)...

A monad is just a monoid in the category of endofunctors, what's the problem?

Who first said A monad is just a monoid in the category of endofunctors, what's the problem? and on a less important note is this true and if so could you give an explanation(hopefully one that can be understood by someone who doesn't have much haskell experience). ...