monads

Why does Haskell not have an I Monad (for input only, unlike the IO monad)?

耗尽温柔 提交于 2019-12-05 12:20:39
问题 Conceptually, it seems that a computation that performs output is very different from one that performs input only. The latter is, in one sense, much purer. I, for one, would like to have a way to separate the input only parts of my programme from the ones that might actually write something out. So, why is there no input only Monad? Any reason why it wouldn't work to have an I monad (and an O Monad, which could be combined into the IO Monad)? Edit : I mostly meant input as reading files, not

How to show that a monad is a functor and an applicative functor?

旧城冷巷雨未停 提交于 2019-12-05 11:52:04
问题 Monads are known to be theoretically a subset of functors and specifically applicative functors, even though it's not indicated in Haskell's type system. Knowing that, given a monad and basing on return and bind , how to: derive fmap , derive <*> ? 回答1: Well, fmap is just (a -> b) -> f a -> f b , i.e. we want to transform the monadic action's result with a pure function. That's easy to write with do notation: fmap f m = do a <- m return (f a) or, written "raw": fmap f m = m >>= \a -> return

What is an explicit example of a monad without a monad transformer? [duplicate]

折月煮酒 提交于 2019-12-05 11:08:38
问题 This question already has answers here : Is there a monad that doesn't have a corresponding monad transformer (except IO)? (4 answers) Closed last year . Monad transformers are known for all standard monads (Reader, Writer, State, Cont, List, etc.), but each of these monad transformers works in a slightly different way. There is no general method or formula for constructing a monad transformer given a definition of a type constructor with a monad instance. So, it is not assured that a monad

`mfix` not working as expected

谁都会走 提交于 2019-12-05 10:54:28
I expected the following code to first prompt start: , then wait for a user response, before echoing the previous user response back, and awaiting a new one: import System.IO (hFlush, stdout) import Control.Monad.Fix (mfix) f :: [String] -> IO [String] f = mapM $ \x -> putStr x >> putStr ": " >> hFlush stdout >> getLine g x = f ("start":x) main = mfix g But it gives the error thread blocked indefinitely in an MVar operation after the first line is entered. Why is this and how can I fix it (excuse the pun)? Petr Pudlák The reason why this can't work is that in mfix f runs any effect in f

Relationship between fmap and bind

只愿长相守 提交于 2019-12-05 10:23:51
After looking up the Control.Monad documentation, I'm confused about this passage: The above laws imply: fmap f xs = xs >>= return . f How do they imply that? Control.Applicative says As a consequence of these laws, the Functor instance for f will satisfy fmap f x = pure f <*> x The relationship between Applicative and Monad says pure = return (<*>) = ap ap says return f `ap` x1 `ap` ... `ap` xn is equivalent to liftMn f x1 x2 ... xn Therefore fmap f x = pure f <*> x = return f `ap` x = liftM f x = do { v <- x; return (f v) } = x >>= return . f duplode Functor instances are unique , in the

Haskell: I/O and Returning From a Function

天涯浪子 提交于 2019-12-05 07:22:18
Please bear with me as I am very new to functional programming and Haskell. I am attempting to write a function in Haskell that takes a list of Integers, prints the head of said list, and then returns the tail of the list. The function needs to be of type [Integer] -> [Integer]. To give a bit of context, I am writing an interpreter and this function is called when its respective command is looked up in an associative list (key is the command, value is the function). Here is the code I have written: dot (x:xs) = do print x return xs The compiler gives the following error message: forth.hs:12:1:

How to Better Iterate over State in Clojure (monad?)

落爺英雄遲暮 提交于 2019-12-05 06:47:43
I just wrote this code: (defn parameters [transform-factory state] (lazy-seq (let [[r1 state] (uniform state) [r2 state] (uniform state) [t state] (transform-factory state)] (cons [t [r1 r2]] (parameters transform-factory state))))) (defn repeated-transform [mosaic n transform-factory state] (reduce transform-square mosaic (take n (parameters transform-factory state)))) the parameters function generates a lazy sequence of values generated from the state , which are used to parameterise a repeated transformation of something (a "mosaic" in this case). it seems to me that parameters shows a

Adjoint functors determine monad transformers, but where's lift?

走远了吗. 提交于 2019-12-05 06:41:15
I'm intrigued by the construction described here for determining a monad transformer from adjoint functors. Here's some code that summarizes the basic idea: {-# LANGUAGE MultiParamTypeClasses #-} import Control.Monad newtype Three g f m a = Three { getThree :: g (m (f a)) } class (Functor f, Functor g) => Adjoint f g where counit :: f (g a) -> a unit :: a -> g (f a) instance (Adjoint f g, Monad m) => Monad (Three g f m) where return = Three . fmap return . unit m >>= f = Three $ fmap (>>= counit . fmap (getThree . f)) (getThree m) instance (Adjoint f g, Monad m) => Applicative (Three g f m)

How to implement index-core style indexed state monad?

萝らか妹 提交于 2019-12-05 05:57:04
I'm trying to understand indexed monads in the index-core style. I have become stuck in a paradox which is that I can't understand the principles until I have constructed a few examples, and I can't construct examples until I understand the principles. I am trying to construct an indexed state monad. So far my intuition tells me it should be something like this type a :* b = forall i. (a i, b i) newtype IState f a i = IState { runIState :: f i -> (a :* f) } and that I can recover the "restricted" state monad by setting f = Identity and choosing a appropriately: type IState' s s' a = IState

Collecting IO outputs into list

元气小坏坏 提交于 2019-12-05 05:21:33
How can I issue multiple calls to SDL.pollEvent :: IO Event until the output is SDL.NoEvent and collect all the results into a list? In imperative terms something like this: events = [] event = SDL.pollEvent while ( event != SDL.NoEvent ) { events.add( event ) event = SDL.pollEvent } James Cook was so kind to extend monad-loop s with this function: unfoldWhileM :: Monad m => (a -> Bool) -> m a -> m [a] used with SDL: events <- unfoldWhileM (/= SDL.NoEvent) SDL.pollEvent You could use something like: takeWhileM :: (a -> Bool) -> IO a -> IO [a] takeWhileM p act = do x <- act if p x then do xs <-