monads

How to modify using a monadic function with lenses?

喜你入骨 提交于 2019-12-30 05:57:06
问题 I needed a lens function that works like over , but with monadic operations: overM :: (Monad m) => Lens s t a b -> (a -> m b) -> (s -> m t) While this function is easy to define (it's actually just an identity modulo WrappedMonad ), I wonder are such functions defined somewhere in lens ? {-# LANGUAGE RankNTypes #-} import Control.Applicative import Control.Lens overF :: (Functor f) => Lens s t a b -> (a -> f b) -> (s -> f t) overF l = l overM :: (Monad m) => Lens s t a b -> (a -> m b) -> (s -

Simulating interacting stateful objects in Haskell

混江龙づ霸主 提交于 2019-12-29 08:27:18
问题 I'm currently writing a Haskell program that involves simulating an abstract machine, which has internal state, takes input and gives output. I know how to implement this using the state monad, which results in much cleaner and more manageable code. My problem is that I don't know how to pull the same trick when I have two (or more) stateful objects interacting with one another. Below I give a highly simplified version of the problem and sketch out what I have so far. For the sake of this

How to get ReaderT to work with another monad transformer?

匆匆过客 提交于 2019-12-29 08:06:53
问题 I would like to embed ReaderT into another monad transformer. How do I do this? The example below uses Scotty but I think it would be the same with any other monad. {-# LANGUAGE OverloadedStrings #-} import qualified Web.Scotty import Web.Scotty.Trans import Data.Text.Lazy import Control.Monad.IO.Class (liftIO) import Control.Monad.Trans.Reader import Control.Monad.Trans data Config = Config Text main :: IO () main = do let config = Config "Hello World" -- how to I make this line work?

combining StateT with InputT

前提是你 提交于 2019-12-29 08:03:52
问题 It is a follow-up to this question. I'm trying to combine shell from @ErikR's answer in my InputT loop. main :: IO [String] main = do c <- makeCounter execStateT (repl c) [] repl :: Counter -> StateT [String] IO () repl c = lift $ runInputT defaultSettings loop where loop = do minput <- getLineIO $ in_ps1 $ c case minput of Nothing -> lift $ outputStrLn "Goodbye." Just input -> (liftIO $ process c input) >> loop getLineIO :: (MonadException m) => IO String -> InputT m (Maybe String) getLineIO

Why the Haskell sequence function can't be lazy or why recursive monadic functions can't be lazy

两盒软妹~` 提交于 2019-12-29 07:28:08
问题 With the question Listing all the contents of a directory by breadth-first order results in low efficiencyI learned that the low efficiency is due to a strange behavior of the recursive monad functions. Try sequence $ map return [1..]::[[Int]] sequence $ map return [1..]::Maybe [Int] and ghci will fall into an endless calculation. If we rewrite the sequence function in a more readable form like follows: sequence' [] = return [] sequence' (m:ms) = do {x<-m; xs<-sequence' ms; return (x:xs)} and

Can someone explain to me why the app function of ArrowApply makes them as powerful as monads?

送分小仙女□ 提交于 2019-12-28 11:11:24
问题 So I'll break my question into 4 parts, but first some background: I feel relatively comfortable with Monads, but not very comfortable with Arrows. I suppose the main problem I have with them is, I don't see what they are useful for. Whether formally correct or not, I understand Monads to be a tool that allows us to introduce side effects from computation. As they generalize program fragments from pure values to values boxed with other actions. From my shotgun "read all the papers" approach

Is there a monad that doesn't have a corresponding monad transformer (except IO)?

喜你入骨 提交于 2019-12-28 03:31:09
问题 So far, every monad (that can be represented as a data type) that I have encountered had a corresponding monad transformer, or could have one. Is there such a monad that can't have one? Or do all monads have a corresponding transformer? By a transformer t corresponding to monad m I mean that t Identity is isomorphic to m . And of course that it satisfies the monad transformer laws and that t n is a monad for any monad n . I'd like to see either a proof (ideally a constructive one) that every

Haskell, IO, monads, quickcheck

拟墨画扇 提交于 2019-12-25 07:59:17
问题 Beginner at Haskell here. I have a function, and a bunch of properties to test it with which I've written using quickcheck. The properties work when I run them individually in the interpreter, and the function checks out fine. However, manually quickchecking with each property in the interpreter by hand (quickCheck prop_this, quickCheck prop_that) is boring, monotonous, and time-consuming. I would like to stick all of this in a program, and have that program run all of the quickchecks. This

How to create monadic behaviour in reactive-banana

风格不统一 提交于 2019-12-24 12:46:24
问题 Suppose I catch key presses and manipulate a code buffer accordingly: let bCode = accumB emptyCode eModifications eCodeChanges <- changes bCode I would like to create another behaviour bEval bEval = accumB freshEnv (magic eCodeChanges) which maps any state of code to its evaluation (triggered only when something really changes). However, evaluation happens in a monad Interpreter (think hint from hackage). Can I actually define such a behaviour bEval ? I guess I could just drag Interpreter

Trampolining scalaz' Monad.whileM_ to prevent stack overflow

假装没事ソ 提交于 2019-12-24 11:25:58
问题 I'm using scalaz' Monad.whileM_ to implement a while loop in a functional way as follows: object Main { import scalaz._ import Scalaz._ import scala.language.higherKinds case class IState(s: Int) type IStateT[A] = StateT[Id, IState, A] type MTransT[S[_], A] = EitherT[S, String, A] type MTrans[A] = MTransT[IStateT, A] def eval(k: Int): MTrans[Int] = { for { state <- get[IState].liftM[MTransT] _ <- put(state.copy(s = (state.s + 1) % k)).liftM[MTransT] } yield (k + 1) } def evalCond(): MTrans