monads

Seeking constructive criticism on monad implementation

断了今生、忘了曾经 提交于 2019-11-28 06:16:08
I'm learning monads, this is my first working one (aside from the trivial monad). Feel free to criticize everything in it ruthlessly. I'm especially interested in "more idiomatic" and "more elegant" kind of responses. This monad counts the number of binds performed. data C a = C {value :: a, count :: Int} deriving (Show) instance Monad C where (>>=) (C x c) f = C (value $ f x) (c + 1) return x = C x 0 add :: (Num a) => a -> a -> C a add x y = return $ x + y -- Simpler way to do this? foldM is obviously something different. mysum [x] = return x mysum (x:xs) = mysum xs >>= add x Stylistically

What happens to you if you break the monad laws?

こ雲淡風輕ζ 提交于 2019-11-28 06:13:09
Do the compiler or the more "native" parts of the libraries (IO or functions that have access to black magic and the implementation) make assumptions about these laws? Will breaking them cause the impossible to happen? Or do they just express a programming pattern -- ie, the only person you'll annoy by breaking them are people who use your code and didn't expect you to be so careless? The compiler doesn't make any assumptions about the laws, however, if your instance does not obey the laws, it will not behave like a monad -- it will do strange things and otherwise appear to your users to not

Constructing efficient monad instances on `Set` (and other containers with constraints) using the continuation monad

主宰稳场 提交于 2019-11-28 05:48:14
Set , similarly to [] has a perfectly defined monadic operations. The problem is that they require that the values satisfy Ord constraint, and so it's impossible to define return and >>= without any constraints. The same problem applies to many other data structures that require some kind of constraints on possible values. The standard trick (suggested to me in a haskell-cafe post ) is to wrap Set into the continuation monad. ContT doesn't care if the underlying type functor has any constraints. The constraints become only needed when wrapping/unwrapping Set s into/from continuations: import

Desugaring do-notation for Monads

不打扰是莪最后的温柔 提交于 2019-11-28 05:22:39
As I'm learning Haskell I'm realizing that do notation is just syntatic sugar: a = do x <- [3..4] [1..2] return (x, 42) Translates into a = [3..4] >>= (\x -> [1..2] >>= (\_ -> return (x, 42))) I realize that I'll probably use do-notation but I'd like to understand whats going on in translation. So purely for pedagogical reasons, is there a way for ghc/ghci to give me the corresponding bind statements for a fairly complex monad written in do-notation? Edit. It turns out lambdabot on #haskell can do this: <Guest61347> @undo do x <- [3..4] ; [1..2] ; return (x, 42) <lambdabot> [3 .. 4] >>= \ x ->

To what extent are Applicative/Monad instances uniquely determined?

浪尽此生 提交于 2019-11-28 05:21:54
As described this question/answers , Functor instances are uniquely determined, if they exists. For lists, there are two well know Applicative instances: [] and ZipList . So Applicative isn't unique (see also Can GHC derive Functor and Applicative instances for a monad transformer? and Why is there no -XDeriveApplicative extension? ). However, ZipList needs infinite lists, as its pure repeats a given element indefinitely. Are there other, perhaps better examples of data structures that have at least two Applicative instances? Are there any such examples that only involve finite data structures

What monads can be expressed as Free over some functor?

半世苍凉 提交于 2019-11-28 04:42:56
The documentation for Free says: A number of common monads arise as free monads, Given data Empty a , Free Empty is isomorphic to the Identity monad. Free Maybe can be used to model a partiality monad where each layer represents running the computation for a while longer. What other monads are expressible using Free ? I could think only of one more: I believe Free (Const e) is isomorphic to Either e . Edit: What monads are not expressible using Free and why? Philip JF Almost all of them (up to issues involving looping and mfix ) but not Cont . Consider the State monad newtype State s a = State

Monad join function

回眸只為那壹抹淺笑 提交于 2019-11-28 04:32:28
While monads are represented in Haskell using the bind and return functions, they can also have another representation using the join function, such as discussed here . I know the type of this function is M(M(X))->M(X), but what does this actually do? Actually, in a way, join is where all the magic really happens-- (>>=) is used mostly for convenience. All Functor -based type classes describe additional structure using some type. With Functor this extra structure is often thought of as a "container", while with Monad it tends to be thought of as "side effects", but those are just (occasionally

Evil use of Maybe monad and extension methods in C#?

穿精又带淫゛_ 提交于 2019-11-28 03:40:56
edit 2015 This question and its answers are no longer relevant. It was asked before the advent of C# 6, which has the null propagating opertor (?.), which obviates the hacky-workarounds discussed in this question and subsequent answers. As of 2015, in C# you should now use Form.ActiveForm?.ActiveControl?.Name. I've been thinking about the null propagation problem in .NET, which often leads to ugly, repeated code like this: Attempt #1 usual code: string activeControlName = null; var activeForm = Form.ActiveForm; if (activeForm != null) { var activeControl = activeForm.ActiveControl; if

How and why does the Haskell Cont monad work?

廉价感情. 提交于 2019-11-28 02:50:51
This is how the Cont monad is defined: newtype Cont r a = Cont { runCont :: (a -> r) -> r } instance Monad (Cont r) where return a = Cont ($ a) m >>= k = Cont $ \c -> runCont m $ \a -> runCont (k a) c Could you explain how and why this works? What is it doing? The first thing to realize about the continuation monad is that, fundamentally, it's not really doing anything at all. It's true! The basic idea of a continuation in general is that it represents the rest of a computation . Say we have an expression like this: foo (bar x y) z . Now, extract just the parenthesized portion, bar x y --this

Creative uses of monads

狂风中的少年 提交于 2019-11-28 02:39:47
I'm looking for creative uses of monads to learn from. I've read somewhere that monads have been used for example in AI, but being a monad newbie, I fail to see how. Please include a link to the source code and sample usages. No standard monads please. Phil Wadler has written many papers on monads , but the one to read first is a lot of fun and will be accessible to any programmer; it's called The essence of functional programming . The paper includes source code and sample usages. A personal favorite of mine is the probability monad ; if you can find Sungwoo Park 's PhD thesis, it has a