monads

mtl, transformers, monads-fd, monadLib, and the paradox of choice

南楼画角 提交于 2019-11-28 15:11:17
Hackage has several packages for monad transformers: mtl : Monad transformer library transformers : Concrete functor and monad transformers monads-fd : Monad classes, using functional dependencies monads-tf : Monad classes, using type families monadLib : A collection of monad transformers. mtl-tf : Monad transformer library using type families. mmtl : Modular Monad transformer library mtlx : Monad transformer library with type indexes, providing 'free' copies. compose-trans : Composable monad transformers (and maybe I missed some) Which one shall we use? mtl is the one in the Haskell Platform,

Monads with Java 8

╄→尐↘猪︶ㄣ 提交于 2019-11-28 15:08:20
In the interests of helping to understand what a monad is, can someone provide an example using java ? Are they possible ? Lambda expressions are possible using java if you download the pre-release lambda compatible JDK8 from here http://jdk8.java.net/lambda/ An example of a lambda using this JDK is shown below, can someone provide a comparably simple monad ? public interface TransformService { int[] transform(List<Integer> inputs); } public static void main(String ars[]) { TransformService transformService = (inputs) -> { int[] ints = new int[inputs.size()]; int i = 0; for (Integer element :

Monads vs. Arrows

痴心易碎 提交于 2019-11-28 15:07:34
I'm broadly familiar with the concepts of monads and arrows as used in functional programming. I also understand that they can be used to solve similar kinds of problems. However, I'm still a bit confused about how to select which one to use in any given situation. When should I use monads and when should I use arrows? sclv There are two excellent papers by Lindley, Wadler & Yallop (discussed at LTU here ). The most important thing to understand is that there are more things which are arrows than there are things which are monads. Conversely, monads are strictly more powerful than arrows (the

Why do we need monads?

天大地大妈咪最大 提交于 2019-11-28 14:54:03
In my humble opinion the answers to the famous question "What is a monad?" , especially the most voted ones, try to explain what is a monad without clearly explaining why monads are really necessary . Can they be explained as the solution to a problem? cibercitizen1 Why do we need monads? We want to program only using functions . ("functional programming (FP)" after all). Then, we have a first big problem. This is a program: f(x) = 2 * x g(x,y) = x / y How can we say what is to be executed first ? How can we form an ordered sequence of functions (i.e. a program ) using no more than functions ?

Why monads? How does it resolve side-effects?

故事扮演 提交于 2019-11-28 14:45:27
问题 I am learning Haskell and trying to understand Monads. I have two questions: From what I understand, Monad is just another typeclass that declares ways to interact with data inside "containers", including Maybe , List , and IO . It seems clever and clean to implement these 3 things with one concept, but really, the point is so there can be clean error handling in a chain of functions, containers, and side effects. Is this a correct interpretation? How exactly is the problem of side-effects

Use of Haskell state monad a code smell?

一个人想着一个人 提交于 2019-11-28 14:25:53
问题 God I hate the term "code smell", but I can't think of anything more accurate. I'm designing a high-level language & compiler to Whitespace in my spare time to learn about compiler construction, language design, and functional programming (compiler is being written in Haskell). During the code generation phase of the compiler, I have to maintain "state"-ish data as I traverse the syntax tree. For example, when compiling flow-control statements I need to generate unique names for the labels to

Haskell Monad - How does Monad on list work?

为君一笑 提交于 2019-11-28 14:13:10
In order to understand Monad, I came up with the following definitions: class Applicative' f where purea :: a -> f a app :: f (a->b) -> f a -> f b class Applicative' m => Monadd m where (>>|) :: m a -> (a -> m b) -> m b instance Applicative' [] where purea x = [x] app gs xs = [g x | g <- gs, x <- xs] instance Monadd [] where (>>|) xs f = [ y | x <-xs, y <- f x] It works as expected: (>>|) [1,2,3,4] (\x->[(x+1)]) [2,3,4,5] I am not sure how it is working though. For example: [ y | y <- [[1],[2]]] [[1],[2]] How does application (\x->([x+1]) to each list element of [1,2,3] result in [2,3,4] and

How to implement a stack-safe chainRec operator for the continuation monad?

梦想与她 提交于 2019-11-28 14:09:37
I am currently experimenting with the continuation monad. Cont is actually useful in Javascript, because it abstracts from the callback pattern. When we deal with monadic recursion, there is always the risk of a stack overflow, because the recursive call isn't in tail position: const chain = g => f => k => g(x => f(x) (k)); const of = x => k => k(x); const id = x => x; const inc = x => x + 1; const repeat = n => f => x => n === 0 ? of(x) : chain(of(f(x))) (repeat(n - 1) (f)); console.log( repeat(1e6) (inc) (0) (id) // stack overflow ); However, even if we are able to transform some cases into

Generate parser that runs a received parser on the output of another parser and monadically joins the results

安稳与你 提交于 2019-11-28 13:59:14
given the following type and function, meant to parse a field of a CSV field into a string: type Parser resultType = ParsecT String () Identity resultType cell :: Parser String I have implemented the following function: customCell :: String -> Parser res -> Parser res customCell typeName subparser = cell >>= either (const $ unexpected typeName) return . parse (subparser <* eof) "" Though I cannot stop thinking that I am not using the Monad concept as much as desired and that eventually there is a better way to merge the result of the inner with the outer parser, specially on what regards its

The Pause monad

僤鯓⒐⒋嵵緔 提交于 2019-11-28 13:27:35
问题 Monads can do many amazing, crazy things. They can create variables which hold a superposition of values. They can allow you to access data from the future before you compute it. They can allow you to write destructive updates, but not really. And then the continuation monad allows you to break people's minds! Ususally your own. ;-) But here's a challenge: Can you make a monad which can be paused ? data Pause s x instance Monad (Pause s) mutate :: (s -> s) -> Pause s () yield :: Pause s ()