monads

Redefine list monad instance

一曲冷凌霜 提交于 2019-12-10 17:52:35
问题 I'd like to supply my own instance for the list monad. Unfortunately, the following causes a duplicate instance declaration error when compiling. myReturn :: a -> [a] myBind :: [a] -> (a -> [b]) -> [b] instance Monad [] where return = myReturn (>>=) = myBind From the documentation, it seems like it's not possible to hide instance declarations when importing, and since the list monad instance is already declared in the prelude, I guess I can't get rid of the import itself either. I figured

Control.Monad.Writer not working in haskell

可紊 提交于 2019-12-10 17:30:03
问题 I have been trying to compile Haskell code all day – again – involving Control.Monad.Writer. Here is a code example that won't compile from Learn You a Haskell: import Control.Monad.Writer gcd' :: Int -> Int -> Writer [String] Int gcd' a b | b == 0 = do tell ["Finished with " ++ show a] return a | otherwise = do tell [show a ++ " mod " ++ show b ++ " = " ++ show (a `mod` b)] gcd' b (a `mod` b) I receive this error: No instance for (Show (Writer [String] Int)) arising from a use of `print'

Why `Concurrently` is not a monad in Haskell?

故事扮演 提交于 2019-12-10 17:09:54
问题 I'm reading the doc of package async, and trying to find something similar to JavaScript's Promise, and I find Concurrently , which is the most close concept that implemented Functor , Applicative (Promise.all), Alternative (Promise.race). But it doesn't implement Monad (Promise.then), I'm wondering why. I think it maybe because (>>=) is a sequential operation, which conflict with the name Concurrently , but is this the only reason? is there some more important reason here? 回答1: The Monad

Nested do syntax

夙愿已清 提交于 2019-12-10 17:05:37
问题 In this question Will's answer states that the following code (let's call it code A ): reverse2lines :: IO () reverse2lines = do line1 <- getLine line2 <- getLine putStrLn (reverse line2) putStrLn (reverse line1) can be transformed into the following (let's call it code B ) : reverse2lines = do { line1 <- getLine ; do { line2 <- getLine ; do { putStrLn (reverse line2) ; do { putStrLn (reverse line1) } } } } I am confused. I understand, for example, that addOneInt :: IO () addOneInt = do line

Stateful loop with different types of breaks

你说的曾经没有我的故事 提交于 2019-12-10 15:08:56
问题 I am trying to convert the following stateful imperative code into Haskell. while (true) { while (get()) { if (put1()) { failImmediately(); } } if (put2()) { succeedImmediately(); } } Both the put1 and put2 read a state of the system and modify it. get can for simplicity just read the state. failImmediately should break out of the endless-loop and present one type of result, succeedImmediately should also break out but present a different result. What I tried to use was State Env Result where

Generating all the combinations of a set of boolean variables in Haskell

点点圈 提交于 2019-12-10 14:13:38
问题 I am trying to bend my head around list monads in haskell. I was trying to generate a list of all possible propositions given a list of strings designating boolean variables. For instance calling : mapM_ print $ allPropositions ["a","b"] would yield the following result : [("a",True),("b",True)] [("a",True),("b",False)] [("a",False),("b",True)] [("a",False),("b",False)] I have managed to do it using list comprehensions and recursion with the following code allPropositions :: [String] -> [[

Departmental restriction against unsafePerformIO

别说谁变了你拦得住时间么 提交于 2019-12-10 14:12:06
问题 There has been some talk at work about making it a department-wide policy of prohibiting the use of unsafePerformIO and its ilk. Personally, I don't really mind as I've always maintained that if I found myself wanting to use it, it usually meant that I need to rethink my approach. Does this restriction sound reasonable? I seem to remember reading somewhere that it was included mainly for FFI, but I can't remember where I read that at the moment. edit: Ok, that's my fault. It wouldn't be

Why isn't (->) implemented with Control.Monad.Instances by default

只愿长相守 提交于 2019-12-10 14:06:41
问题 I was reading LYAH. It says I need to explicitly load Control.Monad.Instances to get the following syntax to work: ( ( fmap (+5) ) (+5) ) 4 Why is that? Why if functors are this underlying and unifying technology do I have to explicitly load Control.Monad.Instances to get that functionality. How is (->) implemented without it (or is just hidden and only -> exported)? Why isn't the use of fmap over function types implemented by default? 回答1: There are 3 different concepts involved here. The

Observing isomorphism and then proving them as Monad

风流意气都作罢 提交于 2019-12-10 13:17:52
问题 This question is related to this answer. There is a type named Promise : data Promise f a = PendingPromise f | ResolvedPromise a | BrokenPromise deriving (Show) It is stated that: Promise f a ≅ Maybe (Either f a) Now I cannot understand the above expression. How are they sort of equivalent and isomorphic (and from that how can you conclude that it is a Monad) ? 回答1: Two types A and B are isomorphic if there's two functions a2b :: A -> B and b2a :: B -> A such that a2b . b2a ≡ id and b2a . a2b

Compose nested Monads in Haskell

Deadly 提交于 2019-12-10 13:08:38
问题 Is there a way to implement bind for nested monads? What I want is the following signature: (>>>=) :: (Monad m, Monad n) => m (n a) -> (a -> m (n b)) -> m (n b) It looks like it should be a trivial task, but I somehow just can't wrap my head around it. In my program, I use this pattern for several different combinations of monads and for each combination, I can implement it. But for the general case, I just don't understand it. Edit: It seems that it is not possible in the general case. But