monads

What is MonadBaseControl for?

无人久伴 提交于 2019-12-03 04:33:59
I'm digging deeper into Yesod's monads, and have encountered MonadBaseControl . I took a look at the hackage doc, and got lost. Could someone tell me the problem it is trying to solve? It comes from the package monad-control , and is one of a pair of type classes (the other one being MonadTransControl ) that enhance MonadBase (resp. MonadTrans ) by supporting an alternative liftBase (resp. lift ) operation for monads that implement it. This enhanced version no longer takes a simple action in the absolute base monad (resp. immediate base monad), but instead takes a function that gets the base

Under what circumstances are monadic computations tail-recursive?

若如初见. 提交于 2019-12-03 04:27:13
In Haskell Wiki's Recursion in a monad there is an example that is claimed to be tail-recursive : f 0 acc = return (reverse acc) f n acc = do v <- getLine f (n-1) (v : acc) While the imperative notation leads us to believe that it is tail-recursive, it's not so obvious at all (at least to me). If we de-sugar do we get f 0 acc = return (reverse acc) f n acc = getLine >>= \v -> f (n-1) (v : acc) and rewriting the second line leads to f n acc = (>>=) getLine (\v -> f (n-1) (v : acc)) So we see that f occurs inside the second argument of >>= , not in a tail-recursive position. We'd need to examine

Examples of a monad whose Applicative part can be better optimized than the Monad part

天大地大妈咪最大 提交于 2019-12-03 04:20:47
In one discussion I heard that Applicative interface of some parsers is implemented differently, more efficiently than their Monad interface. The reason is that with Applicative we know all "effects" in advance, before the whole effectful computation is run. With monads, effects can depend on values during the computation so this optimization is not possible. I'd like to see some good examples of this. It can be some very simple parser or some different monad, that's not important. The important thing is that the Applicative interface of such a monad complies with its return and ap , but using

What are the alternative of monads to use IO in pure functional programming?

删除回忆录丶 提交于 2019-12-03 04:14:01
问题 monads are described as the haskell solution to deal with IO. I was wondering if there were other ways to deal with IO in pure functional language. 回答1: What alternatives are there to monads for I/O in a pure functional language? I'm aware of two alternatives in the literature: One is a so-called linear type system . The idea is that a value of linear type must be used exactly one time: you can't ignore it, and you can't use it twice. With this idea in mind, you give the state of the world an

How do you make a generic memoize function in Haskell?

*爱你&永不变心* 提交于 2019-12-03 03:46:58
问题 I've seen the other post about this, but is there a clean way of doing this in Haskell? As a 2nd part, can it also be done without making the function monadic? 回答1: This largely follows http://www.haskell.org/haskellwiki/Memoization. You want a function of type (a -> b). If it doesn't call itself, then you can just write a simple wrapper that caches the return values. The best way to store this mapping depends on what properties of a you can exploit. Ordering is pretty much a minimum. With

Why must we use state monad instead of passing state directly?

回眸只為那壹抹淺笑 提交于 2019-12-03 03:38:13
Can someone show a simple example where state monad can be better than passing state directly? bar1 (Foo x) = Foo (x + 1) vs bar2 :: State Foo Foo bar2 = do modify (\(Foo x) -> Foo (x + 1)) get State passing is often tedious, error-prone, and hinders refactoring. For example, try labeling a binary tree or rose tree in postorder: data RoseTree a = Node a [RoseTree a] deriving (Show) postLabel :: RoseTree a -> RoseTree Int postLabel = fst . go 0 where go i (Node _ ts) = (Node i' ts', i' + 1) where (ts', i') = gots i ts gots i [] = ([], i) gots i (t:ts) = (t':ts', i'') where (t', i') = go i t (ts

How to put mutable Vector into State Monad

无人久伴 提交于 2019-12-03 03:14:16
I wrote small program in haskell to count all ocurences of Int values in Tree using State Monad with Vector: import Data.Vector import Control.Monad.State import Control.Monad.Identity data Tree a = Null | Node (Tree a) a (Tree a) deriving Show main :: IO () main = do print $ runTraverse (Node Null 5 Null) type MyMon a = StateT (Vector Int) Identity a runTraverse :: Tree Int -> ((),Vector Int) runTraverse t = runIdentity (runStateT (traverse t) (Data.Vector.replicate 7 0)) traverse :: Tree Int -> MyMon () traverse Null = return () traverse (Node l v r) = do s <- get put (s // [(v, (s ! v) + 1)

When to use Haskell monads

落花浮王杯 提交于 2019-12-03 03:14:09
问题 I'm implementing a combinatorial optimization algorithm in Haskell: Given an initial candidate solution, repeat until stopping criteria are met: 1. Determine possible moves 2. Evaluate possible moves 3. Choose a move 4. Make move, record new candidate solution, update search state I could write functions for steps 1-4 and chain them together inside a recursive function to handle looping and passing state from one iteration to the next, but I have a vague idea that monads apply. What's the

Is it possible to do the IO monad from Haskell in Clojure?

孤人 提交于 2019-12-03 03:01:17
I've had a look at the algo.monads and fluokitten documentation. I've also read through monad blog entries by Jim Duey , Konrad Hinsen and Leonardo Borges . The closest I can find is Konrad Hinsen's library Monadic IO streams - but this doesn't appear to 'implement the monad interface' (for want of a better phrasing) This is example using ST in Haskell oneST :: ST s Int -- note that this works correctly for any s oneST = do var <- newSTRef 0 modifySTRef var (+1) readSTRef var one :: Int one = runST oneST My question is: Is it possible to do the IO Monad from Haskell in Clojure? Could you

Combine state with IO actions

早过忘川 提交于 2019-12-03 02:42:55
问题 Suppose I have a state monad such as: data Registers = Reg {...} data ST = ST {registers :: Registers, memory :: Array Int Int} newtype Op a = Op {runOp :: ST -> (ST, a)} instance Monad Op where return a = Op $ \st -> (st, a) (>>=) stf f = Op $ \st -> let (st1, a1) = runOp stf st (st2, a2) = runOp (f a1) st1 in (st2, a2) with functions like getState :: (ST -> a) -> Op a getState g = Op (\st -> (st, g st) updState :: (ST -> ST) -> Op () updState g = Op (\st -> (g st, ())) and so forth. I want