Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Haskell
Wiki community
Recent changes
Random page
HaskellWiki
Search
Search
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Maintaining laziness
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Special pages
Page information
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==== Reader-Writer-State monad ==== I do not know whether the following example can be simplified. In this form it occurred in a real application, namely the HTTP package. Consider the following action of the <hask>Control.Monad.RWS</hask> which fetches a certain number of elements from a list. The state of the monad is the input list we fetch the elements from. The reader part provides an element which means that the input is consumed. It is returned as singleton when the caller tries to read from a completely read input. The writer allows to log some information, however the considered action does not output anything to the log. <haskell> getN :: Int -> RWS a [Int] [a] [a] getN n = do input <- get if null input then asks (:[]) else let (fetched,rest) = splitAt n input in put rest >> return fetched </haskell> As we learned as good imperative programmers, we only call <hask>splitAt</hask> when the input is non-empty, that is, only if there is something to fetch. This works in even more many corner cases, but not in the following one. Although <hask>getN</hask> does obviously not log something (i.e. it does not call <hask>tell</hask>), it requires to read the input in order to find out, that nothing was logged: <haskell> *Test> (\(_a,_s,w) -> w) $ runRWS (getN 5) '\n' undefined *** Exception: Prelude.undefined </haskell> The problem is again, that <hask>if</hask> checks the emptiness of the input, which is undefined, since the input is undefined. Thus we must ensure, that the invoked monadic actions are run independent from the input. Only this way, the run-time system can see that the logging stream is never touched. We start refactoring by calling <hask>put</hask> independently from <hask>input</hask>'s content. It works as well for empty lists, since <hask>splitAt</hask> will just return empty lists in this case. <haskell> getN :: Int -> RWS a [Int] [a] [a] getN n = do input <- get let (fetched,rest) = splitAt n input put rest if null input then asks (:[]) else return fetched </haskell> This doesn't resolve the problem. There is still a choice between <hask>asks</hask> and <hask>return</hask>. We have to pull out <hask>ask</hask> as well. <haskell> getN :: Int -> RWS a [Int] [a] [a] getN n = do input <- get let (fetched,rest) = splitAt n input put rest endOfInput <- ask return $ if null input then [endOfInput] else fetched </haskell> Now things work as expected: <haskell> *Test> (\(_a,_s,w) -> w) $ runRWS (getN 5) '\n' undefined [] </haskell> We learn from this example, that sometimes in Haskell it is more efficient to call functions that are not needed under some circumstances. Always remind, that the [[Do notation considered harmful|do notation]] looks only imperative, but it is not imperative. E.g., <hask>endOfInput</hask> is only evaluated if the end of the input is really reached. Thus, the call <hask>ask</hask> does not mean that there is actually an action performed between <hask>put</hask> and <hask>return</hask>.
Summary:
Please note that all contributions to HaskellWiki are considered to be released under simple permissive license (see
HaskellWiki:Copyrights
for details). If you don't want your writing to be edited mercilessly and redistributed at will, then don't submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
DO NOT SUBMIT COPYRIGHTED WORK WITHOUT PERMISSION!
Cancel
Editing help
(opens in new window)
Toggle limited content width