monads

Must mplus always be associative? Haskell wiki vs. Oleg Kiselyov

若如初见. 提交于 2019-11-29 02:31:26
问题 The Haskell wikibook asserts that Instances of MonadPlus are required to fulfill several rules, just as instances of Monad are required to fulfill the three monad laws. ... The most essential are that mzero and mplus form a monoid. A consequence of which is that mplus must be associative. The Haskell wiki agrees. However, Oleg, in one of his many backtracking search implementations, writes that -- Generally speaking, mplus is not associative. It better not be, -- since associative and non

More fun with applicative functors

别等时光非礼了梦想. 提交于 2019-11-29 02:13:29
问题 Earlier I asked about translating monadic code to use only the applicative functor instance of Parsec. Unfortunately I got several replies which answered the question I literally asked, but didn't really give me much insight. So let me try this again... Summarising my knowledge so far, an applicative functor is something which is somewhat more restricted than a monad. In the tradition of "less is more", restricting what the code can do increases the possibilities for crazy code manipulation.

Difference between Haskell's Lazy and Strict monads (or transformers)

旧巷老猫 提交于 2019-11-29 02:01:40
问题 When browsing Hackage, most of the monads have a Lazy and a Strict version. What is the difference exactly? Can you highlight it with some examples for the common monads (State, Reader, Writer)? 回答1: I don't know of a separation into lazy and strict for the reader monad, the reason for the State(T) and Writer(T) separation doesn't apply there. The difference between the lazy and strict Writer and State monads resp. their monad transformers is the implementation of the monadic bind (>>=) ,

How to apply higher order function to an effectful function in Haskell?

夙愿已清 提交于 2019-11-29 01:49:26
I have too functions: higherOrderPure :: (a -> b) -> c effectful :: Monad m => (a -> m b) I'd like to apply the first function to the second: higherOrderPure `someOp` effectful :: Monad m => m c where someOp :: Monad m => ((a -> b) -> c) -> (a -> m b) -> m c Example: curve :: (Double -> Double) -> Dia Any curve f = fromVertices $ map p2 [(x, f x) | x <- [1..100]] func :: Double -> Either String Double func _ = Left "Parse error" -- in other cases this func can be a useful arithmetic computation as a Right value someOp :: ((Double -> Double) -> Dia Any) -> (Double -> Either String Double) ->

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

房东的猫 提交于 2019-11-29 01:47:40
问题 Can anybody explain why exceptions may be thrown outside the IO monad, but may only be caught inside it? 回答1: One of the reasons is the denotational semantics of Haskell. One of the neat properties of (pure) Haskell functions is their monotonicity -- more defined argument yields more defined value. This property is very important e.g. to reason about recursive functions (read the article to understand why). Denotation of exception by definition is the bottom, _|_ , the least element in poset

What's the point of using monads in an interpreter?

孤人 提交于 2019-11-29 01:36:26
问题 I recently discovered this little scala example called Simple interpreter using monads : object simpleInterpreter { case class M[A](value: A) { def bind[B](k: A => M[B]): M[B] = k(value) def map[B](f: A => B): M[B] = bind(x => unitM(f(x))) def flatMap[B](f: A => M[B]): M[B] = bind(f) } def unitM[A](a: A): M[A] = M(a) def showM(m: M[Value]): String = m.value.toString(); type Name = String trait Term; case class Var(x: Name) extends Term case class Con(n: int) extends Term case class Add(l:

Are there contravariant monads?

◇◆丶佛笑我妖孽 提交于 2019-11-29 01:13:58
Functors can be covariant and contravariant. Can this covariant/contravariant duality also be applied to monads? Something like: class Monad m where return :: a -> m a (>>=) :: m a -> (a -> m b) -> m b class ContraMonad m where return :: a -> m a contrabind :: m a -> (b -> m a) -> m b Does ContraMonad class make sense? Any examples? Well, of course, it's possible to define it, but I doubt it would be of any use. There is a popular saying that "monad is just a monoid in a category of endofunctors". What it means is, first of all, that we have a category of endofunctors (meaning, (covariant)

what is proper monad or sequence comprehension to both map and carry state across?

别说谁变了你拦得住时间么 提交于 2019-11-28 23:37:23
问题 I'm writing a programming language interpreter. I have need of the right code idiom to both evaluate a sequence of expressions to get a sequence of their values, and propagate state from one evaluator to the next to the next as the evaluations take place. I'd like a functional programming idiom for this. It's not a fold because the results come out like a map. It's not a map because of the state prop across. What I have is this code which I'm using to try to figure this out. Bear with a few

Implementing monads in JavaScript

眉间皱痕 提交于 2019-11-28 23:26:10
Now that node.js supports ECMAScript Harmony generators we can write monadic code succinctly ala do blocks in Haskell: function monad(unit, bind) { return function (f) { return function () { var g = f.apply(this, arguments); return typeOf(g) === "Generator" ? send() : unit(g); function send(value) { var result = g.next(value); if (result.done) return unit(result.value); else return bind(result.value, send); } }; }; } function typeOf(value) { return Object.prototype.toString.call(value).slice(8, -1); } In the code above monad is a function which can be used to create deterministic monads like:

Why can applicative functors have side effects, but functors can't?

一笑奈何 提交于 2019-11-28 23:21:46
I'm feeling rather silly asking this question, but it's been on my mind for a while and I can't find any answers. So the question is: why can applicative functors have side effects, but functors can't? Maybe they can and I've just never noticed...? This answer is a bit of an over-simplification, but if we define side effects as computations being affected by previous computations, it's easy to see that the Functor typeclass is insufficient for side effects simply because there is no way to chain multiple computations. class Functor f where fmap :: (a -> b) -> f a -> f b The only thing a