monads

How can non-determinism be modeled with a List monad?

醉酒当歌 提交于 2019-11-28 22:34:30
Can anyone explain (better with an example in plain English) what a list monad can do to model non-deterministic calculations? Namely what the problem is and what solution a list monad can offer. Here's an example based on coin tossing. The problem is as follows: You have two coins, labeled Biased and Fair . The Biased coin has two heads, and the Fair coin has one head and one tail. Pick one of these coins at random, toss it and observe the result. If the result is a head, what is the probability that you picked the Biased coin? We can model this in Haskell as follows. First, you need the

What's so special about 'return' keyword

久未见 提交于 2019-11-28 20:49:01
When I seemed to understand what return is for in Haskell, I tried to play with different alternatives and it seems that return not only can be used anywhere in the monad chain, but also can be excluded completely *Main> Just 9 >>= \y -> (Just y) >>= \x -> return x Just 9 *Main> Just 9 >>= \y -> (return y) >>= \x -> (Just y) Just 9 *Main> Just 9 >>= \y -> (Just y) >>= \x -> (Just x) Just 9 Even if I omit return in my own instancing, I only get warning... data MaybeG a = NothingG | JustG a deriving Show instance Monad MaybeG where -- return x = JustG x NothingG >>= f = NothingG JustG x >>= f =

How to handle `Reader` monad and `Try`?

我与影子孤独终老i 提交于 2019-11-28 20:37:15
问题 I'm reading this great article about dependency injection in scala with Reader monad. The original example is working well, but I did a little bit change on the return types of the UserRepository.get/find . It was User , but I changed it to Try[User] . Then the code won't be compiled, I had tries many times, but still without lucky. import scala.util.Try import scalaz.Reader case class User(email: String, supervisorId: Int, firstName: String, lastName: String) trait UserRepository { def get

Haskell: actual IO monad implementation, in different language?

拥有回忆 提交于 2019-11-28 20:35:08
问题 How is IO monad actually implemented?in sense of, what would be the actual implementation of the main function? How would I call haskell function (IO) from another language and do I in that case need to maintain IO my self? Does main pulls IO actions (Lazily) as references and then call them? Or it is interpreter job, when it found actions in its way it can call them? Or maybe something else? Is there good IO monad implementation in different language which can help to deeply understand what

Monad instance for binary tree

自古美人都是妖i 提交于 2019-11-28 20:01:44
问题 I built binary tree with: data Tree a = Empty | Node a (Tree a) (Tree a) deriving (Eq, Ord, Read, Show) How can i make Monad type class instance for this tree? And can i make it on not? i try: instance Monad Tree where return x = Node x Empty Empty Empty >>= f = Empty (Node x Empty Empty) >>= f = f x But i can't make (>>=) for Node x left right. Thank you. 回答1: There is no (good) monad for the type you just described, exactly. It would require rebalancing the tree and merging together the

Monoid vs MonadPlus [duplicate]

江枫思渺然 提交于 2019-11-28 19:57:45
问题 This question already has an answer here: Why MonadPlus and not Monad + Monoid? 3 answers I am very new to both Monads and Monoids and recently also learned about MonadPlus . From what I see, Monoid and MonadPlus both provide a type with a associative binary operation and an identity. (I'd call this a semigroup in mathematical parlance.) So what is the difference between Monoid and MonadPlus ? 回答1: A semigroup is a structure equipped with an associative binary operation. A monoid is a

Why MonadPlus and not Monad + Monoid?

不问归期 提交于 2019-11-28 19:08:58
I'm trying to understand the motivation behind the MonadPlus . Why is it necessary if there are already the typeclasses Monad and Monoid ? Granted, instances of Monoid are concrete types, whereas instances of Monad require a single type parameter. (See Monoid vs MonadPlus for a helpful explanation.) But couldn't you rewrite any type constraint of (MonadPlus m) => ... as a combination of Monad and Monoid ? (Monad m, Monoid (m a)) => ... Take the guard function from Control.Monad , for example. Its implementation is: guard :: (MonadPlus m) => Bool -> m () guard True = return () guard False =

Monads in C# — why Bind implementations require passed function to return a monad?

倖福魔咒の 提交于 2019-11-28 19:06:28
问题 Most examples of monads I saw in C# are written somewhat like that: public static Identity<B> Bind<A, B>(this Identity<A> a, Func<A, Identity<B>> func) { return func(a.Value); } For example, see http://mikehadlow.blogspot.com/2011/01/monads-in-c-3-creating-our-first-monad.html. The question is, what is the point of requiring func to return an Identity<B> ? If I use the following definition: public interface IValue<A> { public IValue<B> Bind<B>(Func<A, B> func) } then I can actually use same

Why there is no something like IMonad<T> in upcoming .NET 4.0

廉价感情. 提交于 2019-11-28 19:00:31
问题 ... with all those new (and not so new if we count IEnumerable) monad-related stuff? interface IMonad<T> { SelectMany/Bind(); Return/Unit(); } That would allow to write functions that operate on any monadic type. Or it's not so critical? 回答1: Think about what the signature for IMonad<T> 's methods would have to be. In Haskell the Monad typeclass is defined as class Monad m where (>>=) :: m a -> (a -> m b) -> m b return :: a -> m a It's tricky to translate this directly to a C# interface

How does ArrowLoop work? Also, mfix?

左心房为你撑大大i 提交于 2019-11-28 18:55:53
问题 I'm fairly comfortable now with the rest of the arrow machinery, but I don't get how loop works. It seems magical to me, and that's bad for my understanding. I also have trouble understanding mfix. When I look at a piece of code that uses rec in a proc or do block, I get confused. With regular monadic or arrow code, I can step through the computation and keep an operational picture of what's going on in my head. When I get to rec , I don't know what picture to keep! I get stuck, and I can't