monads

Haskell: Replace mapM in a monad transformer stack to achieve lazy evaluation (no space leaks)

余生长醉 提交于 2019-12-24 08:58:26
问题 It has already been discussed that mapM is inherently not lazy, e.g. here and here. Now I'm struggling with a variation of this problem where the mapM in question is deep inside a monad transformer stack. Here's a function taken from a concrete, working (but space-leaking) example using LevelDB that I put on gist.github.com: -- read keys [1..n] from db at DirName and check that the values are correct doRead :: FilePath -> Int -> IO () doRead dirName n = do success <- runResourceT $ do db <-

Stream transformer Monad for Vector.Stream

此生再无相见时 提交于 2019-12-24 07:26:50
问题 Data.Vector.Stream provides an nice Stream implementation that is very efficient thanks to the focus on fusability (see this paper for more). Since vector-0.1 this Stream implementation changed slightly by moving the Step type into a monad. (Now, the implementation is located in Data.Vector.Fusion.Stream.Monadic.) In a nutshell, here's the definition of Stream : data Step s a where Yield :: a -> s -> Step s a Skip :: s -> Step s a Done :: Step s a data Stream a = forall s. Stream (s -> Step s

fmap into a do block fails with a print error

大城市里の小女人 提交于 2019-12-24 07:19:24
问题 I'm trying to understand why a function I have written with a do-block can't be rewritten to fmap a similar lambda expression over a list. I have the following: -- This works test1 x = do let m = T.pack $ show x T.putStrLn m test1 1 Produces 1 But -- This fails fmap (\x -> do let m = T.pack $ show x T.putStrLn m ) [1..10] -- And this also fails fmap (\x -> do T.putStrLn $ T.pack $ show x ) [1..10] With error: <interactive>:1:1: error: • No instance for (Show (IO ())) arising from a use of

Pithy summary for codata (Where a comonad is a 'type for input impurity')

自作多情 提交于 2019-12-24 05:31:36
问题 In terms of pithy summaries - this description of Comonads seems to win - describing them as a 'type for input impurity'. What is an equivalent pithy (one-sentence) description for codata? 回答1: "Codata are types inhabited by values that may be infinite" This contrasts with "data" which is inhabited only by finite values. For example, if we take the "data" definition of lists, it is inhabited by lists of finite length (as in ML), but if we take the "codata" definition it is inhabited also by

Composing functions that return an option

六眼飞鱼酱① 提交于 2019-12-24 04:26:11
问题 Suppose I have a few functions of type Int => Option[Int] : def foo(n: Int): Int => Option[Int] = {x => if (x == n) none else x.some} val f0 = foo(0) val f1 = foo(1) I can compose them with >=> as follows: val composed: Int => Option[Int] = Kleisli(f0) >=> Kleisli(f1) Suppose now I need to compose all functions from a list: val fs: List[Int => Option[Int]] = List(0, 1, 2).map(n => foo(n)) I can do it with map and reduce : val composed: Int => Option[Int] = fs.map(f => Kleisli(f)).reduce(_ >=>

Scala simplify nested monads

≡放荡痞女 提交于 2019-12-24 02:32:33
问题 I have some code written in Lift. Basically its nested Box (similar monad to Option). I'd like to simplify it a little bit if possible. Preferably add type parameter so this could be easily changed to string or double if needed. Here is the code tryo(r.param("boolean parameter").map(_.toBoolean)).map(_.openOr(false)).openOr(false) "tryo" is helper function to catch and wrap results in Box if exception occurs and r is Req object. "param" function returns Box[String] (that comes from request

Type error in Monad definition

不想你离开。 提交于 2019-12-24 00:42:18
问题 I'm trying to learn about Monads in Haskell. Given the data type: data XY a = X a | Y a I would like 'X a >>= f' to return 'f a' and 'Y a >>= f' to just ignore 'f' and return 'Y a' . This is the code I wrote: 4 instance Monad XY where 5 return x = X x 6 (X a) >>= f = f a 7 (Y a) >>= f = Y a and this is the compiler error I got: prog.hs:7:25: Couldn't match expected type `b' with actual type `a' `b' is a rigid type variable bound by the type signature for >>= :: XY a -> (a -> XY b) -> XY b at

F# computation expression for nested Boolean tests?

为君一笑 提交于 2019-12-23 19:12:59
问题 I think I've got enough understanding of F# monads (workflows) that I see a few places in my code where implementing them makes sense. For example, I've got a function with multiple nested if/thens, i.e. the function should continue only so long as the data pass certain "tests" along the way. I'm familiar with the "maybe" monad, but in all the examples that I've seen, it's coded to operate on let! bindings, which I'm not doing. I'm hoping that someone can provide me with an example of the

Why isn't the list monad method for `>>` defined as `flip const`?

寵の児 提交于 2019-12-23 17:12:35
问题 Is there some reason why the Prelude doesn't define the list monad like this? (Note the non-standard implementation of >> .) instance Monad [] where m >>= k = concat (map k m) m >> k = k -- a.k.a. flip const return x = [x] fail s = [] I tried checking this against the monad laws, but they don't mention >> . The Monad class definition is this: m >> k = m >>= \_ -> k which in the [] instance would translate to this: concat (map (\_ -> k) m) which is of course not equivalent to flip const —they

Pithy summary for comonad. (Where a monad is a 'type for impure computation')

时光怂恿深爱的人放手 提交于 2019-12-23 16:43:34
问题 In terms of pithy summaries - this description of Monads seems to win - describing them as a 'type for impure computation'. What is an equivalent pithy (one-sentence) description of a comonad? 回答1: "A type for context-dependent computation" Alternatively, a better "pithy description" for monads might be a 'type for output impurity', in which case then the pithy description for comonads is a 'type for input impurity'. (If you are interested in comonads, some more introduction is given in some