monads

Control.MonadPlus.Free without unnecessary distribution

梦想与她 提交于 2020-01-03 11:31:49
问题 I'm trying to use a free monad to build an EDSL for constructing AND/OR decision trees like Prolog, with >>= mapped to AND, and mplus mapped to OR. I want to be able to describe something like A AND (B OR C) AND (D OR E) , but I don't want distributivity to turn this into (A AND B AND D) OR (A AND B AND E) OR (A AND C AND D) OR (A AND C AND E) . Ultimately, I want to transform the AND/OR nodes into reified constraints in a constraint solver, without causing the combinatorial explosion in the

Generalized Newtype Deriving

不问归期 提交于 2020-01-03 10:06:27
问题 Haskell can derive the instance for MonadState s in T1 below but not in T2 which is however a very similar type. In which way should I modify the code for T2 so that the instance for MonadState s can be automatically derived? {-# LANGUAGE GeneralizedNewtypeDeriving #-} import Control.Monad.Reader import Control.Monad.State newtype T1 r s a = T1 { runT1 :: ReaderT r (State s) a } deriving (Monad, MonadReader r, MonadState s) newtype T2 r s a = T2 { runT2 :: StateT r (State s) a } deriving

Generalized Newtype Deriving

社会主义新天地 提交于 2020-01-03 10:06:25
问题 Haskell can derive the instance for MonadState s in T1 below but not in T2 which is however a very similar type. In which way should I modify the code for T2 so that the instance for MonadState s can be automatically derived? {-# LANGUAGE GeneralizedNewtypeDeriving #-} import Control.Monad.Reader import Control.Monad.State newtype T1 r s a = T1 { runT1 :: ReaderT r (State s) a } deriving (Monad, MonadReader r, MonadState s) newtype T2 r s a = T2 { runT2 :: StateT r (State s) a } deriving

Reader monad - how does it conform to Monad interface?

元气小坏坏 提交于 2020-01-03 02:10:14
问题 I'm learning category theory. I understand the concept of reader monad, it's even pretty easy to implement: case class Reader[DEP, A](g: DEP => A) { def apply(dep: DEP): A = g(dep) def map[B](f: A => B): Reader[DEP, B] = Reader(dep => f(apply(dep))) def flatMap[B](f: A => Reader[DEP, B]): Reader[DEP, B] = Reader(dep => f(apply(dep)) apply dep) } However, I have problems implementing it with constraint to some generic Monad interface, i.e trait Monad[A] { def pure(a: A): Monad[A] def map[B](f:

Understanding of Parallel typeclass from Cats

倾然丶 夕夏残阳落幕 提交于 2020-01-02 16:53:27
问题 There is a typeclass called Parallel in Cats . The purpose of this class is to provide parallel computations for some monads that don't support parallel computations out of the box like Either for example. I know that Monad is used for dependent computations and thus requires sequential execution. Applicative is used for independent computations, so such computations can be parallelized. It's also known that each Monad is Applicative (monad is applicative functor). So now I can't put together

Understanding of Parallel typeclass from Cats

喜夏-厌秋 提交于 2020-01-02 16:52:43
问题 There is a typeclass called Parallel in Cats . The purpose of this class is to provide parallel computations for some monads that don't support parallel computations out of the box like Either for example. I know that Monad is used for dependent computations and thus requires sequential execution. Applicative is used for independent computations, so such computations can be parallelized. It's also known that each Monad is Applicative (monad is applicative functor). So now I can't put together

Can I mock an interactive program using the state monad?

时光毁灭记忆、已成空白 提交于 2020-01-02 10:18:35
问题 Based on an answer here I was inspired to try and make a program where the state monad could be swapped for the IO monad and it would still work. So far I came up with: {-# LANGUAGE FlexibleInstances #-} import Control.Monad.State class Monad m => Interaction m where getInput :: m String produceOutput :: String -> m () instance Interaction IO where getInput = getLine produceOutput = putStrLn instance Interaction (State String) where getInput = get produceOutput = put interactiveProgram ::

Can I mock an interactive program using the state monad?

倖福魔咒の 提交于 2020-01-02 10:18:17
问题 Based on an answer here I was inspired to try and make a program where the state monad could be swapped for the IO monad and it would still work. So far I came up with: {-# LANGUAGE FlexibleInstances #-} import Control.Monad.State class Monad m => Interaction m where getInput :: m String produceOutput :: String -> m () instance Interaction IO where getInput = getLine produceOutput = putStrLn instance Interaction (State String) where getInput = get produceOutput = put interactiveProgram ::

Can I mock an interactive program using the state monad?

大兔子大兔子 提交于 2020-01-02 10:18:08
问题 Based on an answer here I was inspired to try and make a program where the state monad could be swapped for the IO monad and it would still work. So far I came up with: {-# LANGUAGE FlexibleInstances #-} import Control.Monad.State class Monad m => Interaction m where getInput :: m String produceOutput :: String -> m () instance Interaction IO where getInput = getLine produceOutput = putStrLn instance Interaction (State String) where getInput = get produceOutput = put interactiveProgram ::

What purpose does the complexity of `Except` serve in Haskell?

巧了我就是萌 提交于 2020-01-02 04:56:31
问题 I understand (I think) that there is a close relationship between Either and Except in Haskell, and that it is easy to convert from one to the other. But I'm a bit confused about best practices for handling errors in Haskell and under what circumstances and scenarios I would choose one over the other. For example, in the example provided in Control.Monad.Except , Either is used in the definition type LengthMonad = Either LengthError so that calculateLength "abc" is Right 3 If instead one were