monads

How does <*> derived from pure and (>>=)?

烈酒焚心 提交于 2020-05-26 03:30:06
问题 class Applicative f => Monad f where return :: a -> f a (>>=) :: f a -> (a -> f b) -> f b (<*>) can be derived from pure and (>>=) : fs <*> as = fs >>= (\f -> as >>= (\a -> pure (f a))) For the line fs >>= (\f -> as >>= (\a -> pure (f a))) I am confused by the usage of >>= . I think it takes a functor f a and a function, then return another functor f b . But in this expression, I feel lost. 回答1: Lets start with the type we're implementing: (<*>) :: Monad f => f (a -> b) -> f a -> f b (The

How to use variable from do block assignment line in a where clause?

孤人 提交于 2020-05-23 13:01:40
问题 I have the following sample of code: {-# LANGUAGE ScopedTypeVariables #-} main = do putStrLn "Please input a number a: " a :: Int <- readLn print a putStrLn "Please input a number b: " b :: Int <- readLn print b putStrLn ("a+b+b^2:" ++ (show $ a+b+c)) where c = b^2 For some reason I cannot use variable b in a where clause, the error I get is the following: Main3.hs:13:15: error: Variable not in scope: b | 13 | where c = b^2 | ^ Any ideas how to make b available in the where clause? 回答1: Use

Why list monad combines in that order?

社会主义新天地 提交于 2020-04-13 06:34:20
问题 I was reading about list monads and encountered: [1,2] >>= \n -> ['a','b'] >>= \ch -> return (n,ch) it produces [(1,'a'),(1,'b'),(2,'a'),(2,'b')] Here's how I understand it: Implicit parentheses are: ([1,2] >>= \n -> ['a','b']) >>= (\ch -> return (n,ch)) ([1,2] >>= \n -> ['a','b']) should give [('a',1),('b',1),('a',2),('b',2)] because instance Monad [] where return x = [x] xs >>= f = concat (map f xs) -- this line fail _ = [] so concat (map f xs) is concat (map (\n -> ['a','b']) [1,2]) which

Why list monad combines in that order?

青春壹個敷衍的年華 提交于 2020-04-13 06:34:16
问题 I was reading about list monads and encountered: [1,2] >>= \n -> ['a','b'] >>= \ch -> return (n,ch) it produces [(1,'a'),(1,'b'),(2,'a'),(2,'b')] Here's how I understand it: Implicit parentheses are: ([1,2] >>= \n -> ['a','b']) >>= (\ch -> return (n,ch)) ([1,2] >>= \n -> ['a','b']) should give [('a',1),('b',1),('a',2),('b',2)] because instance Monad [] where return x = [x] xs >>= f = concat (map f xs) -- this line fail _ = [] so concat (map f xs) is concat (map (\n -> ['a','b']) [1,2]) which

ghci special case for Applicative?

瘦欲@ 提交于 2020-04-12 09:57:54
问题 In ghci: λ> :t (pure 1) (pure 1) :: (Applicative f, Num a) => f a λ> show (pure 1) <interactive>:1:1: No instance for (Show (f0 a0)) arising from a use of `show' Possible fix: add an instance declaration for (Show (f0 a0)) In the expression: show (pure 1) In an equation for `it': it = show (pure 1) λ> pure 1 1 Does this mean that ghci execute Applicative and displays the result, just like IO ? Note that pure () and pure (+1) don't print anything. 回答1: You get the same behaviour if you use

Is every Alternative Monad Filterable?

余生颓废 提交于 2020-04-10 08:06:08
问题 The category of sets is both cartesian monoidal and cocartesian monoidal. The types of the canonical isomorphisms witnessing these two monoidal structures are listed below: type x + y = Either x y type x × y = (x, y) data Iso a b = Iso { fwd :: a -> b, bwd :: b -> a } eassoc :: Iso ((x + y) + z) (x + (y + z)) elunit :: Iso (Void + x) x erunit :: Iso (x + Void) x tassoc :: Iso ((x × y) × z) (x × (y × z)) tlunit :: Iso (() × x) x trunit :: Iso (x × ()) x For the purposes of this question I

Is every Alternative Monad Filterable?

谁说胖子不能爱 提交于 2020-04-10 08:06:08
问题 The category of sets is both cartesian monoidal and cocartesian monoidal. The types of the canonical isomorphisms witnessing these two monoidal structures are listed below: type x + y = Either x y type x × y = (x, y) data Iso a b = Iso { fwd :: a -> b, bwd :: b -> a } eassoc :: Iso ((x + y) + z) (x + (y + z)) elunit :: Iso (Void + x) x erunit :: Iso (x + Void) x tassoc :: Iso ((x × y) × z) (x × (y × z)) tlunit :: Iso (() × x) x trunit :: Iso (x × ()) x For the purposes of this question I

Is every Alternative Monad Filterable?

|▌冷眼眸甩不掉的悲伤 提交于 2020-04-10 08:05:27
问题 The category of sets is both cartesian monoidal and cocartesian monoidal. The types of the canonical isomorphisms witnessing these two monoidal structures are listed below: type x + y = Either x y type x × y = (x, y) data Iso a b = Iso { fwd :: a -> b, bwd :: b -> a } eassoc :: Iso ((x + y) + z) (x + (y + z)) elunit :: Iso (Void + x) x erunit :: Iso (x + Void) x tassoc :: Iso ((x × y) × z) (x × (y × z)) tlunit :: Iso (() × x) x trunit :: Iso (x × ()) x For the purposes of this question I

Interpreters for two polymorphic classes in one function

允我心安 提交于 2020-03-25 18:21:24
问题 I have this polymorphic code (see this question) with generic monads for model and client: import Control.Monad.Writer class Monad m => Model m where act :: Client c => String -> c a -> m a class Monad c => Client c where addServer :: String -> c () scenario1 :: forall c m. (Client c, Model m) => m () scenario1 = do act "Alice" $ addServer @c "https://example.com" and this is the pretty-print interpreter for the Client that explains the actions in the log via Writer monad: type Printer =

Interpreters for two polymorphic classes in one function

百般思念 提交于 2020-03-25 18:21:13
问题 I have this polymorphic code (see this question) with generic monads for model and client: import Control.Monad.Writer class Monad m => Model m where act :: Client c => String -> c a -> m a class Monad c => Client c where addServer :: String -> c () scenario1 :: forall c m. (Client c, Model m) => m () scenario1 = do act "Alice" $ addServer @c "https://example.com" and this is the pretty-print interpreter for the Client that explains the actions in the log via Writer monad: type Printer =