monads

MonadError section in “All about monads”

北城以北 提交于 2019-12-02 00:10:34
I'm now really confused about the Error monad in which "All about monads" describes. It claims the definition of Error monad as class (Monad m) => Monaderror e m | m -> e where throwError :: e -> m a catchError :: m a -> (e -> m a) -> m a And one of the instance is Either e. instance MonadError (Either e) where throwError = Left (Left e) `catchError` handler = handler e a `catchError` _ = a Here is what I don't understand. The MonadError class take two type parameters, and (Either e) takes one, how is this instantiation work? Is this because the functional dependencies? I still don't get it.

How can I write this simple code using the state monad?

人走茶凉 提交于 2019-12-01 23:43:44
I'm a beginner at Haskell and I've come across a situation where I would like to use the state monad. (Or at least, I think I that's what I'd like to use.) There are a million tutorials for the state monad, but all of them seem to assume that my main goal is to understand it on a deep conceptual level, and consequently they stop just before the part where they say how to actually develop software with it. So I'm looking for help with a simplified practical example. Below is a very simple version of what my current code looks like. As you can see, I'm threading state through my functions, and

Type of return in do block

安稳与你 提交于 2019-12-01 22:32:06
I am trying to understand Monads in Haskell and during my countless experiments with code I have encountered this thing: f2 = do return "da" and the fact that it doesnt want to compile with huge error regarding type. I think the only important part is this: No instance for (Monad m0) arising from a use of return' The type variable `m0' is ambiguous So then I have changed my code to: f2 = do return "da" :: IO [Char] And it worked perfectly well. But when I have tried to mess up a bit and change the type to IO Int it was an error once again. So why the type is "ambiguous" when it actually isnt?

Composing monad actions with folds

萝らか妹 提交于 2019-12-01 21:28:19
问题 Let's take a function of type (Monad m) => a -> m a . For example: ghci> let f x = Just (x+1) I'd like to be able to apply it any number of times. The first thing I tried was ghci> let times n f = foldr (>=>) return $ replicate n f The problem is that it won't work for large n : ghci> 3 `times` f $ 1 Just 4 ghci> 1000000 `times` f $ 1 Just *** Exception: stack overflow It doesn't work also the other way: ghci> let timesl n f = foldl' (<=<) return $ replicate n f ghci> 3 `timesl` f $ 1 Just 4

Combining List, Future and Option in for-comprehension - scalaz

旧城冷巷雨未停 提交于 2019-12-01 20:51:20
I've got following problem: val sth: Future[Seq[T, S]] = for { x <- whatever: Future[List[T]] y <- x: List[T] z <- f(y): Future[Option[S]] n <- z: Option[S] } yield y: T -> n: S I would want to make this code to work(I guess everyone understands the idea as I've added types). By "to work" I mean, that I would want to stay with the for-comprehension structure and fulfil expected types in the end. I know there are "ugly" ways to do it, but I want to learn how to do it pure :) As I read the internet I've reached the conclusion that my problem may be solved by the monad transformers & scalaz.

How exactly does the `(<*>) = ap` Applicative/Monad law relate the two classes?

岁酱吖の 提交于 2019-12-01 19:20:32
ap doesn't have a documented spec, and reads with a comment pointing out it could be <*> , but isn't for practical reasons: ap :: (Monad m) => m (a -> b) -> m a -> m b ap m1 m2 = do { x1 <- m1; x2 <- m2; return (x1 x2) } -- Since many Applicative instances define (<*>) = ap, we -- cannot define ap = (<*>) So I assume the ap in the (<*>) = ap law is shorthand for "right-hand side of ap" and the law actually expresses a relationship between >>= , return and <*> right? Otherwise the law is meaningless. The context is me thinking about Validation and how unsatisfying it is that it can't seem to

How to multiply two (double option)s in F#

廉价感情. 提交于 2019-12-01 18:18:32
问题 My code contains quite a few double option types; I've been using the Option.map function quite successfully so far to eliminate the need to have to match on Some and None all over the place and treat them as lifted types but am not sure what to do in the following scenario: let multiplyTwoOptions option1 option2 : double option = if not (option1.IsSome && option2.IsSome) then None else Some (option1.Value * option2.Value) I've read that you shouldn't use IsSome in this way, but the

How to multiply two (double option)s in F#

此生再无相见时 提交于 2019-12-01 18:04:28
My code contains quite a few double option types; I've been using the Option.map function quite successfully so far to eliminate the need to have to match on Some and None all over the place and treat them as lifted types but am not sure what to do in the following scenario: let multiplyTwoOptions option1 option2 : double option = if not (option1.IsSome && option2.IsSome) then None else Some (option1.Value * option2.Value) I've read that you shouldn't use IsSome in this way, but the alternative (as far as I can see, to pattern match on both sequentially, seems quite long winded). I'm still

How exactly does the `(<*>) = ap` Applicative/Monad law relate the two classes?

妖精的绣舞 提交于 2019-12-01 17:58:57
问题 ap doesn't have a documented spec, and reads with a comment pointing out it could be <*> , but isn't for practical reasons: ap :: (Monad m) => m (a -> b) -> m a -> m b ap m1 m2 = do { x1 <- m1; x2 <- m2; return (x1 x2) } -- Since many Applicative instances define (<*>) = ap, we -- cannot define ap = (<*>) So I assume the ap in the (<*>) = ap law is shorthand for "right-hand side of ap" and the law actually expresses a relationship between >>= , return and <*> right? Otherwise the law is

What should a “higher order Traversable” class look like?

大憨熊 提交于 2019-12-01 17:46:38
问题 In this answer I made up on the spot something which looks a bit like a "higher order Traversable ": like Traversable but for functors from the category of endofunctors on Hask to Hask. {-# LANGUAGE RankNTypes #-} import Data.Functor.Compose import Data.Functor.Identity class HFunctor t where hmap :: (forall x. f x -> g x) -> t f -> t g class HFunctor t => HTraversable t where htraverse :: Applicative g => (forall x. f x -> g x) -> t f -> g (t Identity) htraverse eta = hsequence . hmap eta