monads

Haskell maps returning a monad

谁说胖子不能爱 提交于 2019-12-05 01:22:40
The lookup function in Data.Map and Data.IntMap currently return values wrapped in Maybe with the type signature lookup :: Ord k => k -> Map k a -> Maybe a It used to have the more general type of lookup :: (Monad m, Ord k) => k -> Map k a -> m a I realize the former likely reduces the need of extra type specification, but the latter would make it much more general and allow lookup to be used in list comprehensions. Is there any way to mimic this behavior with the newer version, or would I have to use an older version of the library? Don's lift converts Maybe 's elements to their general Monad

Concurrent data access as in Haxl and Stitch

别说谁变了你拦得住时间么 提交于 2019-12-05 00:08:33
问题 This is a follow-up to my previous question. As I understand from Haxl and Stitch they use a monad for data access. The monad is actually a tree of data access commands. The children are the commands the node depends on. The siblings are executed concurrently. The business logic creates the monad and then a separate function fetch interprets it. Now, the question: Suppose I am performing a few data access operations concurrently. I can use an applicative functor (not a monad), which is just a

Can I eliminate the use of UndecidableInstances in this Show instance for a Free Monad?

无人久伴 提交于 2019-12-04 23:47:15
问题 I've just been trying to wrap my head around free monads; as a learning aid, I've managed to write a Show instance for the following Free type: {-# LANGUAGE FlexibleContexts, UndecidableInstances #-} -- Free monad datatype data Free f a = Return a | Roll (f (Free f a)) instance Functor f => Monad (Free f) where return = Return Return a >>= f = f a Roll ffa >>= f = Roll $ fmap (>>= f) ffa -- Show instance for Free; requires FlexibleContexts and -- UndecidableInstances instance (Show (f (Free f

Is there an instance of Monad but not of MonadFix?

こ雲淡風輕ζ 提交于 2019-12-04 23:42:54
The question is mostly in the title. It seems like mfix can be defined for any monadic computation, even though it might diverge: mfix :: (a -> m a) -> m a mfix f = fix (join . liftM f) What is wrong with this construction? Also, why are the Monad and MonadFix typeclasses separate (i.e. what type has an instance of Monad but not of MonadFix )? The left shrinking (or tightening) law says that mfix (\x -> a >>= \y -> f x y) = a >>= \y -> mfix (\x -> f x y) In particular this means that mfix (\x -> a' >> f x) = a' >> mfix f which means that the monadic action inside mfix must be evaluated exactly

Haskell — dual personality IO / ST monad?

丶灬走出姿态 提交于 2019-12-04 22:42:31
I have some code that currently uses a ST monad for evaluation. I like not putting IO everywhere because the runST method produces a pure result, and indicates that such result is safe to call (versus unsafePerformIO ). However, as some of my code has gotten longer, I do want to put debugging print statements in. Is there any class that provides a dual-personality monad [or typeclass machinery], one which can be a ST or an IO (depending on its type or a "isDebug" flag)? I recall SPJ introduced a "Mutation" class in his "Fun with Type Functions" paper, which used associative types to relate IO

Scala Cats Effects - IO Async Shift - How Does it Work?

二次信任 提交于 2019-12-04 21:30:26
问题 Here is some Scala cats code using the IO Monad: import java.util.concurrent.{ExecutorService, Executors} import cats.effect.IO import scala.concurrent.{ExecutionContext, ExecutionContextExecutor} import scala.util.control.NonFatal object Program extends App { type CallbackType = (Either[Throwable, Unit]) => Unit // IO.async[Unit] is like a Future that returns Unit on completion. // Unlike a regular Future, it doesn't start to run until unsafeRunSync is called. def forkAsync(toRun: () => Unit

Is it possible to do the Free Monad in Clojure?

拈花ヽ惹草 提交于 2019-12-04 21:07:14
问题 There has been some outstanding work with Monads in Clojure by Konrad Hinsen, Jim Duey and Leonardo Borges. My question is - is it possible to do the Free Monad in Clojure? This is an example in Haskell from an article on Scala: data Free f r = Free (f (Free f r)) | Pure r This is the corresponding Scala example sealed abstract class Free[S[+_], +A](implicit S: Functor[S]) { final def map[B](f: A => B): Free[S, B] = flatMap(a => Return(f(a))) final def flatMap[B](f: A => Free[S, B]): Free[S,

How to write a monad that prints “step i of N” when executing each statement in the monad?

你说的曾经没有我的故事 提交于 2019-12-04 20:33:37
问题 I'm not even sure this is possible in any kind of monad; does it violate monad laws? But it seems like something that should be possible in some kind of construct or other. Specifically is there any way to have something that I can write something like do someOp () someOtherOp () thirdOp () and it would print step 1 of 3 step 2 of 3 step 3 of 3 Would this require Template Haskell or would a monad work? (And if Template Haskell is required, how to do it that way?) 回答1: I assume that you want

On the signature of >>= Monad operator

孤街醉人 提交于 2019-12-04 19:31:36
问题 This is the signature of the well know >>= operator in Haskell >>= :: Monad m => m a -> (a -> m b) -> m b The question is why type of the function is (a -> m b) instead of (a -> b) I would say the latter one is more practical because it allows straightforward integration of existing "pure" functions in the monad being defined. On the contrary, it seems not difficult to write a general "adapter" adapt :: (Monad m) => (a -> b) -> (a -> m b) but anyway I regard more probable that you already

How to reduce iterations when chaining map reduce filter?

天涯浪子 提交于 2019-12-04 19:22:36
I have been reading about map , reduce and filter a lot because of how much they are used in react and FP in general. If we write something like: let myArr = [1,2,3,4,5,6,7,8,9] let sumOfDoubleOfOddNumbers = myArr.filter(num => num % 2) .map(num => num * 2) .reduce((acc, currVal) => acc + currVal, 0); 3 different loops are run. I've read about Java 8 streams as well and know that they use what is called a monad, ie, the computations are stored first. They are performed once only in one iteration. For example, Stream.of("d2", "a2", "b1", "b3", "c") .map(s -> { System.out.println("map: " + s);