monads

Why will a IO nested in other monads not execute? Is there a way to force them to?

帅比萌擦擦* 提交于 2019-12-04 03:48:20
问题 This is a follow up of my last question. IO action nested in other monads not executing The solution to that question was to remove some of the monads, and that allowed the IO action to execute. Why did I need to unnest the monads? Is there a way to execute the IO without unnesting? Note : This is a what-if more than it is a question about good or bad practice. 回答1: Perhaps it would help to think of IO as type IO a = World -> (a, World) ; that is, a function that takes as its only parameter

Can a “Cont r a” post-process the result of its continuation

谁说我不能喝 提交于 2019-12-04 03:37:18
问题 the Cont r a type stands for a function which takes a continuation a->r and produces a result of type r . So both the continuation and the entire Cont r a produce a result of the same type r . My question is: are the two results necessarily the same value , or can a Cont r a post-process the result from the continuation and produce a different value, albeit of the same type r ? I tried using (+1) for post-processing (note the + 1 --<-- ): c1 :: Int -> Cont r Int c1 x = let y = 2*x in cont $

Haskell do clause with multiple monad types

一个人想着一个人 提交于 2019-12-04 03:20:37
I'm using a graphic library in Haskell called Threepenny-GUI . In this library the main function returns a UI monad object. This causes me much headache as when I attempt to unpack IO values into local variables I receive errors complaining of different monad types. Here's an example of my problem. This is a slightly modified version of the standard main function, as given by Threepenny-GUI's code example: main :: IO () main = startGUI defaultConfig setup setup :: Window -> UI () setup w = do labelsAndValues <- shuffle [1..10] shuffle :: [Int] -> IO [Int] shuffle [] = return [] shuffle xs = do

Scala: how to understand the flatMap method of Try?

為{幸葍}努か 提交于 2019-12-04 02:55:39
The flatMap method of the Success is implemented like this: def flatMap[U](f: T => Try[U]): Try[U] = try f(value) catch { case NonFatal(e) => Failure(e) } I kinda understand what this method is doing, it helps us to avoid writing a lot of catching code. But in what sense is it similar to the regular flatMap? A regular flatMap takes a sequence of sequences, and put all the elements into one big "flat" sequence. But the flatMap method of Try is not really flattening anything. So, how to understand the flatMap method of Try? Without entering into monads, instead of thinking about it in terms of

Applicative instance for MaybeT m assumes Monad m

余生颓废 提交于 2019-12-04 01:56:52
I've been using the Haxl monad (described here: http://www.reddit.com/r/haskell/comments/1le4y5/the_haxl_project_at_facebook_slides_from_my_talk ), which has the interesting feature that <*> for its Applicative instance isn't the same as ap from Control.Monad. This is a key feature that allows it to do concurrent computations without blocking. For example, if hf and ha are long computations, then let hf :: Haxl (a -> b) = ... ha :: Haxl a = ... in do f <- hf a <- ha return (f a) will do them sequentially, while hf <*> ha will do them in parallel and then combine the results. I would like to be

Applicative is to monad what X is to comonad

杀马特。学长 韩版系。学妹 提交于 2019-12-04 00:42:05
Can we solve this equation for X ? Applicative is to monad what X is to comonad After giving it some thought, I think this is actually a backward question. One might think that ComonadApply is to Comonad what Applicative is to Monad , but that is not the case. But to see this, let us use PureScript's typeclass hierarchy: class Functor f where fmap :: (a -> b) -> f a -> f b class Functor f => Apply f where apply :: f (a -> b) -> f a -> f b -- (<*>) class Apply f => Applicative f where pure :: a -> f a class Applicative m => Monad m where bind :: m a -> (a -> m b) -> m b -- (>>=) -- join :: m (m

Is this a monad?

会有一股神秘感。 提交于 2019-12-04 00:25:38
I'm trying to understand the concept of monads and I want to know if this code is an implementation of this concept (in JavaScript). I have function M which return new object that have set method which create wrapper method var foo = M().set('getX', function() { return this.x; }).set('setX', function(x) { this.x = x; }).set('addX', function(x) { this.x += x; }); And then I can chain method of foo foo.setX(10).addX(20).addX(30).getX() will return 60 and the same if I have object with methods and call M with this object. var foo = { x: 10, add: function(x) { this.x += x; } }; M(foo).add(10).add

The “reader” monad

自闭症网瘾萝莉.ら 提交于 2019-12-04 00:20:30
OK, so the writer monad allows you to write stuff to [usually] some kind of container, and get that container back at the end. In most implementations, the "container" can actually be any monoid. Now, there is also a "reader" monad. This, you might think , would offer the dual operation - incrementally reading from some kind of container, one item at a time. In fact, this is not the functionality that the usual reader monad provides. (Instead, it merely offers easy access to a semi-global constant.) To actually write a monad which is dual to the usual writer monad, we would need some kind of

What are all of the monad naming conventions?

倖福魔咒の 提交于 2019-12-03 23:40:45
It seems that Haskell has established several naming conventions around monads. Examples: appending T to the end to obtain the name of the monad transformer (e.g. Reader -> ReaderT ) using runXXX to perform a monad computation (e.g. runST , runReader ) liftXXX for various values of XXX Are there other naming conventions? runX m where m :: X a will run the X monad and return the "side effect" along with the monad result, a . evalX m will run the computation and return the result, a . execX m will run the computation and return the "side effect" but not the result. The lifts come in various

How to show that a monad is a functor and an applicative functor?

别等时光非礼了梦想. 提交于 2019-12-03 23:30:35
Monads are known to be theoretically a subset of functors and specifically applicative functors, even though it's not indicated in Haskell's type system. Knowing that, given a monad and basing on return and bind , how to: derive fmap , derive <*> ? ehird Well, fmap is just (a -> b) -> f a -> f b , i.e. we want to transform the monadic action's result with a pure function. That's easy to write with do notation: fmap f m = do a <- m return (f a) or, written "raw": fmap f m = m >>= \a -> return (f a) This is available as Control.Monad.liftM . pure :: a -> f a is of course return . (<*>) :: f (a -