monads

How to use bind with nested monads?

蓝咒 提交于 2019-11-29 17:51:50
问题 I have two functions, one that tries to get a token from a webservice and may fail, and one that tries to use this token to get the username and may fail. getToken :: IO (Maybe Token) getUsername :: Token -> IO (Maybe String) I would like to take the result of getToken and feed it to getUsername . If there was only IO or Maybe , I could simply use bind, but since there are down nested monads, I can't. How can I write something equivalent to getToken >>= getUsername :: IO (Maybe String) ? More

How to use SmallCheck in Haskell?

ε祈祈猫儿з 提交于 2019-11-29 17:37:15
问题 I am trying to use SmallCheck to test a Haskell program, but I cannot understand how to use the library to test my own data types. Apparently, I need to use the Test.SmallCheck.Series. However, I find the documentation for it extremely confusing. I am interested in both cookbook-style solutions and an understandable explanation of the logical (monadic?) structure. Here are some questions I have (all related): If I have a data type data Person = SnowWhite | Dwarf Integer , how do I explain to

Is there no standard (Either a) monad instance?

拥有回忆 提交于 2019-11-29 17:32:21
问题 I was under the impression that there was an instance for Either a somewhere, but I can't seem to find it. I have tried importing Control.Monad, Control.Monad.Instances and Data.Either as shown module Main where import Control.Monad import Data.Either import Control.Monad.Instances test :: [Either a b] -> Either a [b] test = sequence main = return () but ghc tells me that it could not deduce (Monad (Either a)). Adding instance Monad (Either a) where return = Right Right b >>= f = f b Left a >

Can I make a Lens with a Monad constraint?

牧云@^-^@ 提交于 2019-11-29 17:27:00
问题 Context: This question is specifically in reference to Control.Lens (version 3.9.1 at the time of this writing) I've been using the lens library and it is very nice to be able to read and write to a piece (or pieces for traversals) of a structure. I then had a though about whether a lens could be used against an external database. Of course, I would then need to execute in the IO Monad . So to generalize: Question: Given a getter, (s -> m a) and an setter (b -> s -> m t) where m is a Monad,

Does an IO monad make sense in a language like C#

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-29 17:01:47
问题 After spending a lot of time reading and thinking, I think I have finally grasped what monads are, how they work, and what they're useful for. My main goal was to figure out if monads were something I could apply to my daily work in C#. When I started learning about monads, I got the impression that they are magical, and that they somehow make IO and other non-pure functions pure. I understand the importance of monads for things like LINQ in .Net, and Maybe is very useful for dealing with

Binding functions that take multiple arguments

℡╲_俬逩灬. 提交于 2019-11-29 16:56:24
问题 After reading some very basic haskell now I know how to "chain" monadic actions using bind , like: echo = getLine >>= putStrLn (>>=) operator is very handy in this fashion, but what if I want to chain monadic actions (or functors) that take multiple arguments? Given that (>>=) :: m a -> (a -> m b) -> m b it seems like (>>=) can supply only one argument. For example, writeFile takes two arguments (a FilePath and the contents). Suppose I have a monadic action that returns a FilePath , and

Delimiting the IO monad

血红的双手。 提交于 2019-11-29 16:41:06
问题 It's nice to know (in Safe Haskell, at least) from the signature whether or not something performs IO actions, but IO encompasses a lot of different things - putStr , database access, removing and writing to files, IORefs, etc. If I'm using the type signatures as a security measure when running arbitrary code, it might be the case that I'm willing to accept some IO actions - putStr and the ilk, for instance - but not others. Is there a way to define a restricted version of the IO monad, with

Difference between Monads and Functions

我的梦境 提交于 2019-11-29 15:34:37
Ok, about Monad , I am aware that there are enough questions having been asked. I am not trying to bother anyone to ask what is monad again. Actually, I read What is a monad? , it is very helpful. And I feel I am very close to really understand it. I create this question here is just to describe some of my thoughts on Monad and Function, and hope someone could correct me or confirm it correct. Some answers in that post let me feel monad is a little bit like function . Monad takes a type, return a wrapper type ( return ), also, it can take a type, doing some operations on it and returns a

Why does haskell's bind function take a function from non-monadic to monadic

为君一笑 提交于 2019-11-29 15:07:21
I have some questions about the definition of the binding function (>>=) in Haskell. Because Haskell is a pure language, so we can use Monad to handle operations with side effects. I think this strategy is somewhat like putting all actions may cause side effects to another world, and we can control them from our "pure" haskell world though do or >>= . So when I look at definition of >>= function (>>=) :: Monad m => m a -> (a -> m b) -> m b it takes a (a -> m b) function, so result m a of the former action can be "unpack" to a non-monadic a in >>= . Then the function (a -> m b) takes a as its

Continuation monad for a yield/await function in Haskell

故事扮演 提交于 2019-11-29 14:53:18
I want to create an automata type with a type like this: newtype Auto i o = Auto {runAuto :: i -> (o, Auto i o)} I know this is the type of the Automata arrow , but I'm not looking for an arrow. I want to make this a monad, so presumably its going to have a type like newtype Auto i o a = ???? What goes here? with a function like this: yield :: o -> Auto i o i So when I call "yield" from within the Auto monad the "runAuto" function returns a pair consisting of the argument to "yield" and the continuation function. When the application program calls the continuation function the argument is then