monads

Is `data PoE a = Empty | Pair a a` a monad?

江枫思渺然 提交于 2019-12-04 16:48:27
问题 This question comes from this answer in example of a functor that is Applicative but not a Monad: It is claimed that the data PoE a = Empty | Pair a a deriving (Functor,Eq) cannot have a monad instance, but I fail to see that with: instance Applicative PoE where pure x = Pair x x Pair f g <*> Pair x y = Pair (f x) (g y) _ <*> _ = Empty instance Monad PoE where Empty >>= _ = Empty Pair x y >>= f = case (f x, f y) of (Pair x' _,Pair _ y') -> Pair x' y' _ -> Empty The actual reason why I believe

How to detect a Monad?

[亡魂溺海] 提交于 2019-12-04 16:29:29
问题 Many of us don't have a background on functional programming, and much less on category theory algebra. So let's suppose that we need and therefore create a generic type like data MySomething t = ....... Then we continue programming, and using MySomething . What evidences should alert us that MySomething is a monad, and that we have to make it one by writing instance Monad MySomething ... and defining return and >>= for it? Thanks. Edit: See also this question: is chaining operations the only

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

匆匆过客 提交于 2019-12-04 15:48:44
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 think is related to monad transformers, but the last bits I cannot piece together. It also reminded me

Using the Maybe Monad in “reverse”

末鹿安然 提交于 2019-12-04 14:59:44
问题 Let's say I have a number of functions: f :: a -> Maybe a g :: a -> Maybe a h :: a -> Maybe a And I want to compose them in the following way: If f returns Nothing, compute g. If g returns Nothing, compute h. If any of them compute Just a, stop the chain. And the whole composition (h . g . f) should of course return Maybe a. This is the reverse of the typical use of the Maybe monad, where typically you stop computing if Nothing is returned. What's the Haskell idiom for chaining computations

python: mutating `globals` to dynamically put things in scope

僤鯓⒐⒋嵵緔 提交于 2019-12-04 14:58:23
how terrible an idea is this? class monad implements the with interface to put things in and out of scope, so i can write a library of generic functions like m_chain who refer to functions unit and bind who can have an implementation put in at runtime. (It doesn't matter what all this code does or if it's a good idea.) other ideas i tried all revolved around passing around a structure containing unit/bind as an argument or a kwarg, or putting m_chain in a class, implement it in terms of self.unit and self.bind and having derived classes provide them. but it added complexity to the code and

Monad's left unit law does not seem to hold for Lists in scala. Are scala Lists not monads then?

偶尔善良 提交于 2019-12-04 14:12:14
问题 Monads' "left unit law": unit(x) flatMap f == f(x) But: (List(1) flatMap ((x: Int) => Some[Int](x))) == List(1) // true ((x: Int) => Some[Int](x))(1) == Some(1) // also true So left unit law does not hold for lists in scala. Are lists not monads then? 回答1: First, the monad law assumes f: A => M[A] (here f: A => List[A] ). This is not true of (x: Int) => Some[Int](x) . Second, List 's flatMap is not monadic bind. It is more general than bind, because it takes an implicit CanBuildFrom that

the equivalence between applicative functor and monad

爷,独闯天下 提交于 2019-12-04 14:07:10
问题 People say monads are an extension of applicative functors, but I don't see that. Let's take an example of applicative functor: (<*>) :: f(a->b) -> f a -> f b [(+3)] <*> [2,3,4] Now, I also expect I can do the same thing as monad, it means I can apply 2 parameters: a context contains a function, and another context to get a context. But for monad, I can't. All I need is to write an ugly function like this: [2,3,4] >>= (\x->[x+3]) Yes, of course, you can say that [(+3)] is equivalent to [\x->

Using monads for trivial tasks like list manipulation?

一个人想着一个人 提交于 2019-12-04 13:11:19
问题 Whenever I read about Monad example, they always present IO as a case study. Are there any examples of monads doing list manipulation which somebody could present? I aprpeciate this could be overkill, but I am interested if monads can present advantages over regular list manipulation techniques. 回答1: The big secret to the list monad in Haskell is that list comprehensions are syntactic sugar for do blocks . Any time you write a list comprehension, you could have written it using a do block

Implementing the Haskell-MaybeMonad in F# - how can we get this lazy?

╄→гoц情女王★ 提交于 2019-12-04 12:33:27
问题 we are trying to build the Haskell-MaybeMonad sample from http://www.haskell.org/all_about_monads/html/maybemonad.html in F#. The idea is to search for a mailaddress in two dictionaries. If one of the both lookups returns a result we look into the third one. let bindM x k = match x with | Some value -> k value | None -> None let returnM x = Some x type MaybeBuilder() = member this.Bind(x, k) = bindM x k member this.Return(x) = returnM x member this.ReturnFrom(x) = x member this.Delay(f) = f()

Relation of free monad and AST

房东的猫 提交于 2019-12-04 12:17:37
问题 I'm referring to the Ken Scambler's source code listed below, also see GitHub source . package kenbot.free import scalaz._ import Scalaz._ import Free._ import scala.collection.mutable // This example is based off the one in Runar Bjarnason's "Dead Simple Dependency Injection" talk. // http://www.youtube.com/watch?v=ZasXwtTRkio // 0. Fantasy API // def put(key: String, value: String): Unit // def get(key: String): String // def delete(key: String): Unit // 1. ADT sealed trait KVS[+Next] case