monads

iterate + forever = iterateM? Repeating an action with feedback

时间秒杀一切 提交于 2019-12-10 13:02:49
问题 I'm trying to repeat an IO action forever, but feeding the result of one execution into the next. Something like this: -- poorly named iterateM :: Monad m => (a -> m a) -> a -> m b iterateM f a = f a >>= iterateM f Hoogle didn't seem to help me, but I see plenty of functions which look enticingly close to what I want, but none seem to come together to be exactly it. 回答1: You're right, I don't know of a place this particular kind of loop is implemented. Your implementation looks fine; why not

How can I combine the CheckingFuelMonad with a State monad in Hoopl?

故事扮演 提交于 2019-12-10 13:00:00
问题 I am using the Hoopl library and would like to carry some state around while rewriting. The rewrite functions are polymorphic regarding the monad used, but I cannot figure out how to combine a State monad with one of the library's Fuel monads. Below is a minimal example. MyMonad is a synonym combining Hoopl's CheckingFuelMonad and a State monad carrying a flag. Stmt is just a placeholder for my intermediate language and isn't really important. {-# LANGUAGE GADTs, RankNTypes #-} import

Linking/Combining Type Classes in Haskell

女生的网名这么多〃 提交于 2019-12-10 12:49:43
问题 Say I have two type classes defined as follows that are identical in function but different in names: class Monad m where (>>=) :: m a -> (a -> m b) -> m b return :: a -> m a class PhantomMonad p where pbind :: p a -> (a -> p b) -> p b preturn :: a -> p a Is there a way to tie these two classes together so something that is an instance of PhantomMonad will automatically be an instance of Monad, or will instances for each class have to be explicitly written? Any insight would be most

Maybe monad construction

谁都会走 提交于 2019-12-10 11:20:30
问题 I'm currently struggling with a new element of Haskell: Monads. Therefore I was introduced to this by an example of creating a (>>=) operator that executes a function on a Maybe type (taking its actual integer value as argument to it) only if it's not equal to Nothing , and otherwise return Nothing : (>>=) :: Maybe a -> (a -> Maybe b) -> Maybe b Nothing >>= _ = Nothing (Just x) >>= f = f x However, I'm not quite sure how this works with the following usage of it: eval (Val n) = Just n eval

Why is it bind's argument's responsibility to unit its value?

▼魔方 西西 提交于 2019-12-10 10:38:55
问题 The typical monad bind function has the following signature: m a -> (a -> m b) -> m b As I understand it (and I might well be wrong,) the function (a -> m b) is just a mapping function from one structure a to another b . Assuming that is correct begs the question why bind 's signature is not simply: m a -> (a -> b) -> m b Given that unit is part of a monad's definition; why give the function (a -> m b) the responsibility to call unit on whatever value b it produced – wouldn't it be more

How to write without Do notation

有些话、适合烂在心里 提交于 2019-12-10 02:19:13
问题 I was playing around with composable failures and managed to write a function with the signature getPerson :: IO (Maybe Person) where a Person is: data Person = Person String Int deriving Show It works and I've written it in the do-notation as follows: import Control.Applicative getPerson = do name <- getLine -- step 1 age <- getInt -- step 2 return $ Just Person <*> Just name <*> age where getInt :: IO (Maybe Int) getInt = do n <- fmap reads getLine :: IO [(Int,String)] case n of ((x,""):[])

Is there any difference between “MonadIO m” and “MonadBaseControl IO m”?

让人想犯罪 __ 提交于 2019-12-10 02:17:17
问题 Function runTCPClient from network-conduit has the following signature: runTCPClient :: (MonadIO m, MonadBaseControl IO m) => ClientSettings m -> Application m -> m () MonadIO m provides liftIO :: IO a -> m a and MonadBaseControl IO m provides liftBase :: IO a -> m a There is no visible difference. Do they provide the same functionality? If yes, why the duplication in the type signature? If not, what's the difference? 回答1: liftBase is part of MonadBase which is a generalization of MonadIO for

Must I implement Applicative and Functor to implement a Monad

三世轮回 提交于 2019-12-09 17:21:34
问题 I'm trying to implement a Monad instance. As a simpler example, assume the following: data Maybee a = Notheeng | Juust a instance Monad Maybee where return x = Juust x Notheeng >>= f = Notheeng Juust x >>= f = f x fail _ = Notheeng This should be the standard implementation of Maybe as far as I know. However, this doesn't compile, because the compiler complains: No instance for (Applicative Maybee) and similarly he wants a Functor instance once the Applicative is given. So: Simple question:

Violation of the left identity law for Future monads in scalaz

走远了吗. 提交于 2019-12-09 16:55:35
问题 Suppose I define an instance of the Monad typeclass for Future : val futureMonad = new Monad[Future] { override def point[A](a: ⇒ A): Future[A] = Future(a) override def bind[A, B](fa: Future[A])(f: A => Future[B]): Future[B] = fa flatMap f } Strictly speaking, this is not a monad, since it violates the law of left identity: futureMonad.point(a) bind f == f(a) If f throws an exception, the result of the expression on the left hand side will be a failed Future , whereas the right hand side will

Where is the data constructor for 'State'?

我的未来我决定 提交于 2019-12-09 14:29:06
问题 After reading a couple of tutorials on Haskell state monads I wanted to try them out myself. The tutorials I read claim that the Control.Monad.State provide the following definition: newtype State s a = State { runState :: s -> (a,s) } However, I seem to be having trouble find the State data constructor: Prelude> import Control.Monad.State Prelude Control.Monad.State> :t State <interactive>:1:1: Not in scope: data constructor `State' Perhaps you meant `StateT' (imported from Control.Monad