summaryrefslogtreecommitdiff
path: root/libraries/base
diff options
context:
space:
mode:
authorBen Gamari <ben@smart-cactus.org>2018-01-12 15:03:11 -0500
committerBen Gamari <ben@smart-cactus.org>2018-01-12 15:20:50 -0500
commitb2f10d8981bebe44f1ab39e417818dfa2d50639d (patch)
tree3305d9b2190d25364c9cb00df51540a4a19bfb79 /libraries/base
parent6c3eafb35eb7c664963d08a5904faf8c6471218e (diff)
downloadhaskell-b2f10d8981bebe44f1ab39e417818dfa2d50639d.tar.gz
Fix mistaken merge
When merging D4259 I had to resort to manual merge due to some conflicts that arc couldn't sort out. Unfortunately in the process I merged the wrong version of the patch. Fix this. Thanks to @ntc2 for the great documentation and noticing my mistake.
Diffstat (limited to 'libraries/base')
-rw-r--r--libraries/base/Control/Monad.hs37
1 files changed, 15 insertions, 22 deletions
diff --git a/libraries/base/Control/Monad.hs b/libraries/base/Control/Monad.hs
index d9bfdebd54..09066c70cc 100644
--- a/libraries/base/Control/Monad.hs
+++ b/libraries/base/Control/Monad.hs
@@ -156,33 +156,26 @@ f >=> g = \x -> f x >>= g
--
-- ==== __Examples__
--
--- Simple network servers can be created by writing a function to
--- handle a single client connection and then using 'forever' to
--- accept client connections and fork threads to handle them.
+-- A common use of 'forever' is to process input from network sockets,
+-- 'System.IO.Handle's, and channels
+-- (e.g. 'Control.Concurrent.MVar.MVar' and
+-- 'Control.Concurrent.Chan.Chan').
--
--- For example, here is a [TCP echo
--- server](https://en.wikipedia.org/wiki/Echo_Protocol) implemented
--- with 'forever':
+-- For example, here is how we might implement an [echo
+-- server](https://en.wikipedia.org/wiki/Echo_Protocol), using
+-- 'forever' both to listen for client connections on a network socket
+-- and to echo client input on client connection handles:
--
-- @
--- import "Control.Concurrent" ( 'Control.Concurrent.forkFinally' )
--- import "Control.Monad" ( 'forever' )
--- import Network ( PortID(..), accept, listenOn )
--- import "System.IO" ( 'System.IO.hClose', 'System.IO.hGetLine', 'System.IO.hPutStrLn' )
---
--- main :: IO ()
--- main = do
--- sock <- listenOn (PortNumber 7)
--- 'forever' $ do
--- (handle, _, _) <- accept sock
--- echo handle \`forkFinally\` const (hClose handle)
+-- echoServer :: Socket -> IO ()
+-- echoServer socket = 'forever' $ do
+-- client <- accept socket
+-- 'Control.Concurrent.forkFinally' (echo client) (\\_ -> hClose client)
-- where
--- echo handle = 'forever' $
--- hGetLine handle >>= hPutStrLn handle
+-- echo :: Handle -> IO ()
+-- echo client = 'forever' $
+-- hGetLine client >>= hPutStrLn client
-- @
---
--- The @Network@ module is provided by the [network
--- package](https://hackage.haskell.org/package/network).
forever :: (Applicative f) => f a -> f b
{-# INLINE forever #-}
forever a = let a' = a *> a' in a'