on ed0323c help sign in
streamly/src/Streamly/Tutorial.hs
1539 lines 57 kB history raw
1{-# OPTIONS_GHC -fno-warn-unused-imports #-}
2-- |
3-- Module : Streamly.Tutorial
4-- Copyright : (c) 2017 Harendra Kumar
5--
6-- License : BSD3
7-- Maintainer : harendra.kumar@gmail.com
8--
9-- Streamly is a general computing framework based on streaming IO. The IO
10-- monad and pure lists are a special case of streamly. On one hand, streamly
11-- extends the lists of pure values to lists of monadic actions, on the other
12-- hand it extends the IO monad with concurrrent non-determinism. In simple
13-- imperative terms we can say that streamly extends the IO monad with @for@
14-- loops and nested @for@ loops with concurrency support. You can understand
15-- this analogy better once you can go through this tutorial.
16--
17-- Streaming in general enables writing modular, composable and scalable
18-- applications with ease, and concurrency allows you to make them scale and
19-- perform well. Streamly enables writing scalable concurrent applications
20-- without being aware of threads or synchronization. No explicit thread
21-- control is needed, where applicable the concurrency rate is automatically
22-- controlled based on the demand by the consumer. However, combinators can be
23-- used to fine tune the concurrency control.
24--
25-- Streaming and concurrency together enable expressing reactive applications
26-- conveniently. See the @CirclingSquare@ example in the examples directory for
27-- a simple SDL based FRP example. To summarize, streamly provides a unified
28-- computing framework for streaming, non-determinism and functional reactive
29-- programming in an elegant and simple API that is a natural extension of pure
30-- lists to monadic streams.
31--
32-- In this tutorial we will go over the basic concepts and how to
33-- use the library. See the last section for further reading resources.
35module Streamly.Tutorial
36 (
37 -- * Streams
38 -- $streams
40 -- * Flavors of Streams
41 -- $flavors
43 -- * Writing Concurrent Programs
44 -- $programs
46 -- * Imports and Supporting Code
47 -- $imports
49 -- * Generating Streams
50 -- $generating
52 -- * Eliminating Streams
53 -- $eliminating
55 -- * Transforming Streams
56 -- $transformation
58 -- * Merging Streams
60 -- ** Semigroup Style
61 -- $semigroup
63 -- *** Deep Serial Composition ('Serial')
64 -- $serial
66 -- *** Wide Serial Composition ('WSerial')
67 -- $interleaved
69 -- *** Deep Asynchronous Composition ('Async')
70 -- $async
72 -- *** Wide Asynchronous Composition ('WAsync')
73 -- $wasync
75 -- *** Parallel Asynchronous Composition ('Parallel')
76 -- $parallel
78 -- *** Custom composition
79 -- $custom
81 -- ** Monoid Style
82 -- $monoid
84 -- * Nesting Streams
85 -- $nesting
87 -- ** Monad
88 -- $monad
90 -- *** Deep Serial Nesting ('Serial')
91 -- $regularSerial
93 -- *** Wide Serial Nesting ('WSerial')
94 -- $interleavedNesting
96 -- *** Deep Asynchronous Nesting ('Async')
97 -- $concurrentNesting
99 -- *** Wide Asynchronous Nesting ('WAsync')
100 -- $wasyncNesting
102 -- *** Parallel Asynchronous Nesting ('Parallel')
103 -- $parallelNesting
105 -- *** Exercise
106 -- $monadExercise
108 -- ** Applicative
109 -- $applicative
111 -- ** Functor
112 -- $functor
114 -- * Zipping Streams
115 -- $zipping
117 -- ** Serial Zipping
118 -- $serialzip
120 -- ** Parallel Zipping
121 -- $parallelzip
123 -- * Monad transformers
124 -- $monadtransformers
126 -- * Concurrent Programming
127 -- $concurrent
129 -- * Reactive Programming
130 -- $reactive
132 -- * Performance
133 -- $performance
135 -- * Interoperation with Streaming Libraries
136 -- $interop
138 -- * Comparison with Existing Packages
139 -- $comparison
141 -- * Where to go next?
142 -- $furtherReading
143 )
144where
146import Streamly
147import Streamly.Prelude
148import Data.Semigroup
149import Control.Applicative
150import Control.Monad
151import Control.Monad.IO.Class (MonadIO(..))
152import Control.Monad.Trans.Class (MonadTrans (lift))
154-- $streams
155--
156-- The way a list represents a sequence of pure values, a stream represents a
157-- sequence of monadic actions. The monadic stream API offered by Streamly is
158-- very close to the Haskell "Prelude" pure lists' API, it can be considered as a
159-- natural extension of lists to monadic actions. Streamly streams provide
160-- concurrent composition and merging of streams. It can be considered as a
161-- concurrent list transformer. In contrast to the "Prelude" lists, merging or
162-- appending streams of arbitrary length is scalable and inexpensive.
163--
164-- The basic stream type is 'Serial', it represents a sequence of IO actions,
165-- and is a 'Monad'. The 'Serial' monad is almost a drop in replacement for
166-- the 'IO' monad, IO monad is a special case of the 'Serial' monad; IO monad
167-- represents a single IO action whereas the 'Serial' monad represents a series
168-- of IO actions. The only change you need to make to go from 'IO' to 'Serial'
169-- is to use 'runStream' to run the monad and to prefix the IO actions with
170-- either 'once' or 'liftIO'. If you use liftIO you can switch from 'Serial'
171-- to IO monad by simply removing the 'runStream' function; no other changes
172-- are needed unless you have used some stream specific composition or
173-- combinators.
174--
175-- Similarly, the 'Serial' type is almost a drop in replacement for pure lists,
176-- pure lists are a special case of monadic streams. If you use 'nil' in place
177-- of '[]' and '|:' in place ':' you can replace a list with a 'Serial' The
178-- only difference is that the elements must be monadic type and to operate on
179-- the streams we must use the corresponding functions from "Streamly.Prelude"
180-- instead of using the base "Prelude".
182-- $flavors
183--
184-- There are a few more types similar to 'Serial' that all represent streams
185-- and differ only in the 'Semigroup', 'Applicative' and 'Monad' compositions
186-- of the streams.
187--
188-- The composition of two or more streams is distinguished based on three
189-- characterstics, /traversal order/, /execution order/ and
190-- /consumption order/. Traversal of a composition of streams could be @deep@
191-- or @wide@. Deep goes depth first i.e. each stream is traversed fully
192-- before we traverse the next stream. Wide goes breadth first i.e. one element
193-- from each stream is traversed before coming back to the first stream again.
194-- Execution could be serial (i.e. synchronous) or asynchronous. In serial
195-- execution we execute an action in the next stream only after the first has
196-- finished executing. In asynchronous execution actions in both streams can be
197-- executed asynchronously i.e. the next action can start executing even before
198-- the first one has finished. The third parameter is consumption order that is
199-- in what order the output generated by the composition is consumed.
200-- Consumption could be serial or asynchronous. In serial consumption, the
201-- outputs are consumed in the traversal order, in asynchronous consumption the
202-- outputs are consumed as they arrive i.e. first come first serve order.
203--
204-- The following table summarizes different styles of streams based on how they
205-- compose. All these types are monads and they differ in 'Semigroup',
206-- 'Applicative' and 'Monad' compositions:
207--
208-- @
209-- +------------+-----------+--------------+--------------+
210-- | Type | Traversal | Execution | Consumption |
211-- +============+===========+==============+==============+
212-- | 'Serial' | Deep | Serial | Serial |
213-- +------------+-----------+--------------+--------------+
214-- | 'WSerial' | Wide | Serial | Serial |
215-- +------------+-----------+--------------+--------------+
216-- | 'Async' | Deep | Asynchronous | Asynchronous |
217-- +------------+-----------+--------------+--------------+
218-- | 'WAsync' | Wide | Asynchronous | Asynchronous |
219-- +------------+-----------+--------------+--------------+
220-- | 'Parallel' | Parallel | Asynchronous | Asynchronous |
221-- +------------+-----------+--------------+--------------+
222-- @
223--
224-- Other than these types there are also 'ZipSerial' and 'ZipAsync' types that
225-- zip streams serially or concurrently using 'Applicative' operation. These
226-- types are not monads they are only applicatives and they do not differ in
227-- 'Semigroup' composition.
228--
229-- All these types can be freely inter-converted using type conversion
230-- combinators or type annotations without any cost, to acheive the desired
231-- composition style. To force a particular type of composition we coerce the
232-- stream type using the corresponding type adapting combinator from
233-- 'serially', 'wSerially', 'asyncly', 'wAsyncly', 'parallely', 'zipSerially'
234-- or 'zipAsyncly'. The default stream type is inferred as 'Serial' unless you
235-- change it by using one of the combinators or using a type annotation.
237-- $programs
238--
239-- When writing concurrent programs it is advised to not use the concurrent
240-- style stream combinators blindly at the top level. That might create too
241-- much concurrency where it is not even required and therefore degrade
242-- performance. Please be aware of what all APIs that you have used may become
243-- concurrent when you use a concurrency combinator. For example if you have a
244-- 'repeatM' somewhere in your program and you use 'parallely' on top, the
245-- 'repeatM' becomes fully parallel, resulting into an infinite parallel
246-- execution . Instead start with a default serial program and add concurrent
247-- combinators where necessary, and use the 'serially' combinator to suppress
248-- unnecessary concurrency.
250-- $monadtransformers
251--
252-- To represent streams in an arbitrary monad use the more general monad
253-- transformer types for example the monad transformer type corresponding to
254-- the 'Serial' type is 'SerialT'. @SerialT m a@ represents a stream of values
255-- of type 'a' in some underlying monad 'm'. For example, @SerialT IO Int@ is a
256-- stream of 'Int' in 'IO' monad. In fact, the type 'Serial' is a synonym for
257-- @SerialT IO@.
258--
259-- Similarly we have monad transformer types for other stream types as well viz.
260-- 'WSerialT', 'AsyncT', 'WAsyncT' and 'ParallelT'.
261--
262-- To lift a value from an underlying monad in a monad transformer stack into a
263-- singleton stream use 'lift' and to lift from an IO action use 'liftIO'.
264--
265-- @
266-- > 'runStream' $ liftIO $ putStrLn "Hello world!"
267-- Hello world!
268-- > 'runStream' $ lift $ putStrLn "Hello world!"
269-- Hello world!
270-- @
271--
273-- $generating
274--
275-- We will assume the following imports in this tutorial. Go ahead, fire up a
276-- GHCi session and import these lines to start playing.
277--
278-- @
279-- > import "Streamly"
280-- > import "Streamly.Prelude" ((|:))
281-- > import qualified "Streamly.Prelude" as S
282-- @
283--
284-- 'nil' represents an empty stream and 'consM' or its operator form '|:' adds
285-- a monadic action at the head of the stream.
286--
287-- @
288-- > S.'toList' S.'nil'
289-- []
290-- > S.'toList' $ 'getLine' |: 'getLine' |: S.'nil'
291-- hello
292-- world
293-- ["hello","world"]
294-- @
295--
296-- To create a singleton stream from a pure value use 'pure' and to create a
297-- singleton stream from a monadic action use 'once'.
298--
299-- @
300-- > S.'toList' $ 'pure' 1
301-- [1]
302-- > S.'toList' $ S.'once' 'getLine'
303-- hello
304-- ["hello"]
305-- @
306--
307-- To create a stream from pure values in a 'Foldable' container use
308-- 'fromFoldable' which is equivalent to a fold using 'cons' and 'nil':
309--
310-- @
311-- > S.'toList' $ S.'fromFoldable' [1..3]
312-- [1,2,3]
313-- > S.'toList' $ 'Prelude.foldr' S.'cons' S.'nil' [1..3]
314-- [1,2,3]
315-- @
316--
317-- To create a stream from monadic actions in a 'Foldable' container just use a
318-- right fold using 'consM' and 'nil':
319--
320-- @
321-- > 'runStream' $ 'Prelude.foldr' ('|:') S.'nil' ['putStr' "Hello ", 'putStrLn' "world!"]
322-- Hello world!
323-- @
324--
325-- For more ways to construct a stream see the module "Streamly.Prelude".
326--
327-- Note that monadic generation functions e.g. 'consM', 'unfoldrM',
328-- 'replicateM', 'repeatM', 'iterateM' and 'fromFoldableM' etc work
329-- concurrently when used with appropriate stream type combinator. The pure
330-- versions do not work concurrently, however you can use the monadic versions
331-- even for pure computations to get the concurrent generation capability where
332-- required.
333--
334-- TBD add more details and examples about concurrent generation.
336-- $eliminating
337--
338-- We have already seen 'runStream' and 'toList' to eliminate a stream in the
339-- examples above. 'runStream' runs a stream discarding the results i.e. only
340-- for effects. 'toList' runs the stream and collects the results in a list.
341--
342-- For other ways to eliminate a stream see the @Folding@ section in
343-- "Streamly.Prelude" module.
345-- $transformation
346--
347-- Transformation over a stream is the equivalent of a @for@ loop construct in
348-- imperative paradigm. We iterate over every element in the stream and perform
349-- certain transformations for each element. Transformations may involve
350-- mapping functions over the elements, filtering elements from the stream or
351-- folding all the elements in the stream into a single value. Streamly streams
352-- are exactly like lists and you can perform all the transformations in the
353-- same way as you would on lists.
354--
355-- Here is a simple console echo program that just echoes every input line,
356-- forever:
357--
358-- @
359-- > 'runStream' $ S.'repeatM' getLine & S.'mapM' putStrLn
360-- @
361--
362-- The following code snippet reads lines from standard input, filters blank
363-- lines, drops the first non-blank line, takes the next two, up cases them,
364-- numbers them and prints them:
365--
366-- @
367-- import "Streamly"
368-- import qualified "Streamly.Prelude" as S
369-- import Data.Char (toUpper)
370-- import Data.Function ((&))
371--
372-- main = 'runStream' $
373-- S.'repeatM' getLine
374-- & S.'filter' (not . null)
375-- & S.'drop' 1
376-- & S.'take' 2
377-- & fmap (map toUpper)
378-- & S.'zipWith' (\\n s -> show n ++ " " ++ s) (S.'fromFoldable' [1..])
379-- & S.'mapM' putStrLn
380-- @
381--
382-- Note that monadic transformation functions 'mapM' and 'sequence' work
383-- concurrently when used with appropriate stream type combinator. The pure
384-- versions do not work concurrently, however you can use the monadic versions
385-- even for pure computations to get the concurrent transformation capability
386-- where required.
387--
388-- TBD add more details and examples about concurrent transformation.
389-- TBD add details about concurrent function application.
391-- $semigroup
392--
393-- We can combine two streams into a single stream using semigroup composition
394-- operation '<>'. Streams can be combined in many different ways as described
395-- in the following sections, the '<>' operation behaves differently depending
396-- on the stream type in effect. The stream type and therefore the composition
397-- style can be changed at any point using one of the type combinators as
398-- discussed earlier.
400-- $imports
401--
402-- In most of example snippets we do not repeat the imports. Where imports are
403-- not explicitly specified use the imports shown below.
404--
405-- @
406-- import "Streamly"
407-- import "Streamly.Prelude" ((|:), nil)
408-- import qualified "Streamly.Prelude" as S
409--
410-- import Control.Concurrent
411-- import Control.Monad (forever)
412-- @
413--
414-- To illustrate concurrent vs serial composition aspects, we will use the
415-- following @delay@ function to introduce a sleep or delay specified in
416-- seconds. After the delay it prints the number of seconds it slept.
417--
418-- @
419-- delay n = S.'once' $ do
420-- threadDelay (n * 1000000)
421-- tid \<- myThreadId
422-- putStrLn (show tid ++ ": Delay " ++ show n)
423-- @
425-- $serial
426--
427-- The 'Semigroup' operation '<>' of the 'Serial' type combines the two streams
428-- in a /serial depth first/ manner. We use the 'serially' type combinator to
429-- effect 'Serial' style of composition. We can also use an explicit 'Serial'
430-- type annotation for the stream to acheive the same effect. However, since
431-- 'Serial' is the default type unless explicitly specified by using a
432-- combinator, we can omit using an explicit combinator or type annotation for
433-- this style of composition.
434--
435-- When two streams with multiple elements are combined in this manner, the
436-- monadic actions in the two streams are performed sequentially i.e. first all
437-- actions in the first stream are performed sequentially and then all actions
438-- in the second stream are performed sequentially. We call it
439-- /serial depth first/ as the full depth of one stream is fully traversed
440-- before we move to the next. The following example prints the sequence 1, 2,
441-- 3, 4:
442--
443-- @
444-- main = 'runStream' $ (print 1 |: print 2 |: nil) <> (print 3 |: print 4 |: nil)
445-- @
446-- @
447-- 1
448-- 2
449-- 3
450-- 4
451-- @
452--
453-- All actions in both the streams are performed serially in the same thread.
454-- In the following example we can see that all actions are performed in the
455-- same thread and take a combined total of @3 + 2 + 1 = 6@ seconds:
456--
457-- @
458-- main = 'runStream' $ delay 3 <> delay 2 <> delay 1
459-- @
460-- @
461-- ThreadId 36: Delay 3
462-- ThreadId 36: Delay 2
463-- ThreadId 36: Delay 1
464-- @
465--
466-- The polymorphic version of the binary operation '<>' of the 'Serial' type is
467-- 'serial'. We can use 'serial' to join streams in a sequential manner
468-- irrespective of the type of stream:
469--
470-- @
471-- main = 'runStream' $ (print 1 |: print 2 |: nil) \`serial` (print 3 |: print 4 |: nil)
472-- @
474-- $interleaved
475--
476-- The 'Semigroup' operation '<>' of the 'WSerial' type combines the two
477-- streams in a /serial breadth first/ manner. We use the 'wSerially' type
478-- combinator to effect 'WSerial' style of composition. We can also use the
479-- 'WSerial' type annotation for the stream to acheive the same effect.
480--
481-- When two streams with multiple elements are combined in this manner, we
482-- traverse all the streams in a breadth first manner i.e. one action from each
483-- stream is peformed and yielded to the resulting stream before we come back
484-- to the first stream again and so on.
485-- The following example prints the sequence 1, 3, 2, 4
486--
487-- @
488-- main = 'runStream' . 'wSerially' $ (print 1 |: print 2 |: nil) <> (print 3 |: print 4 |: nil)
489-- @
490-- @
491-- 1
492-- 3
493-- 2
494-- 4
495-- @
496--
497-- Even though the monadic actions of the two streams are performed in an
498-- interleaved manner they are all performed serially in the same thread. In
499-- the following example we can see that all actions are performed in the same
500-- thread and take a combined total of @3 + 2 + 1 = 6@ seconds:
501--
502-- @
503-- main = 'runStream' . 'wSerially' $ delay 3 <> delay 2 <> delay 1
504-- @
505-- @
506-- ThreadId 36: Delay 3
507-- ThreadId 36: Delay 2
508-- ThreadId 36: Delay 1
509-- @
510--
511-- The polymorphic version of the 'WSerial' binary operation '<>' is called
512-- 'wSerial'. We can use 'wSerial' to join streams in an interleaved manner
513-- irrespective of the type, notice that we have not used the 'wSerially'
514-- combinator in the following example:
515--
516-- @
517-- main = 'runStream' $ (print 1 |: print 2 |: nil) \`wSerial` (print 3 |: print 4 |: nil)
518-- @
519-- @
520-- 1
521-- 3
522-- 2
523-- 4
524-- @
525--
526-- Note that this composition cannot be used to fold infinite number of streams
527-- since it requires preserving the state until a stream is finished.
529-- $async
530--
531-- The 'Semigroup' operation '<>' of the 'Async' type combines the two
532-- streams in a depth first manner with parallel look ahead. We use the
533-- 'asyncly' type combinator to effect 'Async' style of composition. We
534-- can also use the 'Async' type annotation for the stream type to acheive
535-- the same effect.
536--
537-- When two streams with multiple elements are combined in this manner, the
538-- streams are traversed in depth first manner just like 'Serial', however it
539-- can execute the next stream concurrently and return the results from it
540-- as they arrive i.e. the results from the next stream may be yielded even
541-- before the results from the first stream. Concurrent execution of the next
542-- stream(s) is performed if the first stream blocks or if it cannot produce
543-- output at the rate that is enough to meet the consumer demand. Multiple
544-- streams can be executed concurrently to meet the demand.
545-- In the following example the first stream does not block,
546-- therefore the first stream is completely exhausted before the second.
547--
548-- @
549-- main = 'runStream' . 'asyncly' $ (print 1 |: print 2 |: nil) <> (print 3 |: print 4 |: nil)
550-- @
551-- @
552-- 1
553-- 2
554-- 3
555-- 4
556-- @
557--
558-- If the first stream blocks, we can yield from the second. In the example
559-- below each yield in the stream has a constant delay of 1 second therefore 1
560-- and 3 would be yielded first and then 2 and 4 would be yielded.
561--
562-- @
563-- main = 'runStream' . 'asyncly' $ (p 1 |: p 2 |: nil) <> (p 3 |: p 4 |: nil)
564-- where p n = threadDelay 1000000 >> print n
565-- @
566-- @
567-- 1
568-- 3
569-- 2
570-- 4
571-- @
572--
573-- In the following example we can see that new threads are started when a
574-- computation blocks. Notice that the output from the stream with the
575-- shortest delay is printed first. The whole computation takes @maximum of
576-- (3, 2, 1) = 3@ seconds:
577--
578-- @
579-- main = 'runStream' . 'asyncly' $ delay 3 '<>' delay 2 '<>' delay 1
580-- @
581-- @
582-- ThreadId 42: Delay 1
583-- ThreadId 41: Delay 2
584-- ThreadId 40: Delay 3
585-- @
586--
587-- When we have a tree of computations composed using this style, the tree is
588-- traversed in DFS style just like the 'Serial' style, the only difference is
589-- that here we can move on to executing the next stream if a stream blocks.
590-- However, we will not start new threads if we have sufficient output to
591-- saturate the consumer. This is why we call it left-biased demand driven or
592-- adaptive concurrency style, the concurrency tends to stay on the left side
593-- of the composition as long as possible. More threads are started based on
594-- the pull rate of the consumer. The following example prints an output every
595-- second as all of the actions are concurrent.
596--
597-- @
598-- main = 'runStream' . 'asyncly' $ (delay 1 <> delay 2) <> (delay 3 <> delay 4)
599-- @
600-- @
601-- 1
602-- 2
603-- 3
604-- 4
605-- @
606--
607-- All the computations may even run in a single thread when more threads are
608-- not needed. As you can see, in the following example the computations are
609-- run in a single thread one after another, because none of them blocks.
610-- However, if the thread consuming the stream were faster than the producer
611-- then it would have started parallel threads for each computation to keep up
612-- even if none of them blocks:
613--
614-- @
615-- main = 'runStream' . 'asyncly' $ traced (sqrt 9) '<>' traced (sqrt 16) '<>' traced (sqrt 25)
616-- where traced m = S.'once' (myThreadId >>= print) >> return m
617-- @
618-- @
619-- ThreadId 40
620-- ThreadId 40
621-- ThreadId 40
622-- @
623--
624-- Note that the order of printing in the above examples may change due to
625-- variations in scheduling latencies for concurrent threads.
626--
627-- The polymorphic version of the 'Async' binary operation '<>' is called
628-- 'async'. We can use 'async' to join streams in a left biased
629-- adaptively concurrent manner irrespective of the type, notice that we have
630-- not used the 'asyncly' combinator in the following example:
631--
632-- @
633-- main = 'runStream' $ delay 3 \`async` delay 2 \`async` delay 1
634-- @
635-- @
636-- ThreadId 42: Delay 1
637-- ThreadId 41: Delay 2
638-- ThreadId 40: Delay 3
639-- @
640--
641-- Since the concurrency provided by this operator is demand driven it cannot
642-- be used when the composed computations start timers that are relative to
643-- each other because all computations may not be started at the same time and
644-- therefore timers in all of them may not start at the same time. When
645-- relative timing among all computations is important or when we need to start
646-- all computations at once for any reason 'Parallel' style must be used
647-- instead.
648--
649-- 'Async' style should be preferred over 'Parallel' or 'WAsync' unless you
650-- really need those. It utilizes the resources optimally. It should be used
651-- when we know that the computations can run in parallel but we do not care if
652-- they actually run in parallel or not, that decision can be left to the
653-- scheduler based on demand. Also, note that this operator can be used to fold
654-- infinite number of streams in contrast to the 'Parallel' or 'WAsync' styles,
655-- because it does not require us to run all of them at the same time in a fair
656-- manner.
658-- $wasync
659--
660-- The 'Semigroup' operation '<>' of the 'WAsync' type combines two streams in
661-- a concurrent manner using /breadth first traversal/. We use the 'wAsyncly'
662-- type combinator to effect 'WAsync' style of composition. We can also use the
663-- 'WAsync' type annotation for the stream to acheive the same effect.
664--
665-- When streams with multiple elements are combined in this manner, we traverse
666-- all the streams concurrently in a breadth first manner i.e. one action from
667-- each stream is peformed and yielded to the resulting stream before we come
668-- back to the first stream again and so on. Even though we execute the actions
669-- in a breadth first order the outputs may be consumed in a different order
670-- because they are consumed on a first come first serve basis.
671--
672-- In the following example we can see that outputs are produced in the breadth
673-- first travresal order but this is not guaranteed.
674--
675-- @
676-- main = 'runStream' . 'wAsyncly' $ (print 1 |: print 2 |: nil) <> (print 3 |: print 4 |: nil)
677-- @
678-- @
679-- 1
680-- 3
681-- 2
682-- 4
683-- @
684--
685-- The polymorphic version of the binary operation '<>' of the 'WAsync' type is
686-- 'wAsync'. We can use 'wAsync' to join streams using a breadth first
687-- concurrent traversal irrespective of the type, notice that we have not used
688-- the 'wAsyncly' combinator in the following example:
689--
690-- @
691-- main = 'runStream' $ delay 3 \`wAsync` delay 2 \`wAsync` delay 1
692-- @
693-- @
694-- ThreadId 42: Delay 1
695-- ThreadId 41: Delay 2
696-- ThreadId 40: Delay 3
697-- @
698--
699-- Since the concurrency provided by this style is demand driven it may not
700-- be used when the composed computations start timers that are relative to
701-- each other because all computations may not be started at the same time and
702-- therefore timers in all of them may not start at the same time. When
703-- relative timing among all computations is important or when we need to start
704-- all computations at once for any reason 'Parallel' style must be used
705-- instead.
706--
708-- $parallel
709--
710-- The 'Semigroup' operation '<>' of the 'Parallel' type combines the two
711-- streams in a fairly concurrent manner with round robin scheduling. We use
712-- the 'parallely' type combinator to effect 'Parallel' style of composition.
713-- We can also use the 'Parallel' type annotation for the stream type to
714-- acheive the same effect.
715--
716-- When two streams with multiple elements are combined in this manner, the
717-- monadic actions in both the streams are performed concurrently with a fair
718-- round robin scheduling. The outputs are yielded in the order in which the
719-- actions complete. This is pretty similar to the 'WAsync' type, the
720-- difference is that 'WAsync' is adaptive to the consumer demand and may or
721-- may not execute all actions in parallel depending on the demand, whereas
722-- 'Parallel' runs all the streams in parallel irrespective of the demand.
723--
724-- The following example sends a query to all the three search engines in
725-- parallel and prints the name of the search engines in the order in which the
726-- responses arrive:
727--
728-- @
729-- import "Streamly"
730-- import qualified Streamly.Prelude as S
731-- import Network.HTTP.Simple
732--
733-- main = 'runStream' . 'parallely' $ google \<> bing \<> duckduckgo
734-- where
735-- google = get "https://www.google.com/search?q=haskell"
736-- bing = get "https://www.bing.com/search?q=haskell"
737-- duckduckgo = get "https://www.duckduckgo.com/?q=haskell"
738-- get s = S.'once' (httpNoBody (parseRequest_ s) >> putStrLn (show s))
739-- @
740--
741-- The polymorphic version of the binary operation '<>' of the 'Parallel' type
742-- is 'parallel'. We can use 'parallel' to join streams in a fairly concurrent
743-- manner irrespective of the type, notice that we have not used the
744-- 'parallely' combinator in the following example:
745--
746-- @
747-- main = 'runStream' $ delay 3 \`parallel` delay 2 \`wAsync` delay 1
748-- @
749-- @
750-- ThreadId 42: Delay 1
751-- ThreadId 41: Delay 2
752-- ThreadId 40: Delay 3
753-- @
754--
755-- Note that this style of composition cannot be used to combine infinite
756-- number of streams, as it will lead to an infinite sized scheduling queue.
757--
759-- $custom
760--
761-- The 'mkAsync' API can be used to create references to asynchronously running
762-- stream computations. We can then use 'uncons' to explore the streams
763-- arbitrarily and then recompose individual elements to create a new stream.
764-- This way we can dynamically decide which stream to explore at any given
765-- time. Take an example of a merge sort of two sorted streams. We need to
766-- keep consuming items from the stream which has the lowest item in the sort
767-- order. This can be achieved using async references to streams. See
768-- "MergeSort.hs" in the examples directory.
770-- $monoid
771--
772-- We can use 'Monoid' instances to fold a container of streams in the desired
773-- style using 'fold' or 'foldMap'. We have also provided some fold utilities
774-- to fold streams using the polymorphic combine operations:
775--
776-- * 'foldWith' is like 'fold', it folds a 'Foldable' container of streams
777-- using the given composition operator.
778-- * 'foldMapWith' is like 'foldMap', it folds like @foldWith@ but also maps a
779-- function before folding.
780-- * 'forEachWith' is like @foldMapwith@ but the container argument comes before
781-- the function argument.
782--
783-- All of the following are equivalent and start ten concurrent tasks each with
784-- a delay from 1 to 10 seconds, resulting in the printing of each number every
785-- second:
786--
787-- @
788-- import "Streamly"
789-- import qualified "Streamly.Prelude" as S
790-- import Control.Concurrent
791--
792-- main = do
793-- 'runStream' $ 'asyncly' $ foldMap delay [1..10]
794-- 'runStream' $ 'foldWith' 'async' (map delay [1..10])
795-- 'runStream' $ 'foldMapWith' 'async' delay [1..10]
796-- 'runStream' $ 'forEachWith' 'async' [1..10] delay
797-- where delay n = S.'once' $ threadDelay (n * 1000000) >> print n
798-- @
800-- $nesting
801--
802-- Till now we discussed ways to apply transformations on a stream or to merge
803-- streams together to create another stream. We mentioned earlier that
804-- transforming a stream is similar to a @for@ loop in the imperative paradigm.
805-- We will now discuss the concept of a nested composition of streams which is
806-- analogous to nested @for@ loops in the imperative paradigm. Functional
807-- programmers call this style of composition a list transformer or @ListT@.
808-- Logic programmers call it a logic monad or non-deterministic composition,
809-- but for ordinary imperative minded people like me it is easier to think in
810-- terms of good old nested @for@ loops.
811--
812-- $monad
813--
814-- In functional programmer's parlance the 'Monad' instances of different
815-- 'IsStream' types implement non-determinism, exploring all possible
816-- combination of choices from both the streams. From an imperative
817-- programmer's point of view it behaves like nested loops i.e. for each
818-- element in the first stream and for each element in the second stream
819-- execute the body of the loop.
820--
821-- The 'Monad' instances of 'Serial', 'WSerial', 'Async' and 'WAsync'
822-- stream types support different flavors of nested looping. In other words,
823-- they are all variants of list transformer. The nesting behavior of these
824-- types correspond exactly to the way they merge streams as we discussed in
825-- the previous section.
826--
828-- $regularSerial
829--
830-- The 'Monad' composition of the 'Serial' type behaves like a standard list
831-- transformer. This is the default when we do not use an explicit type
832-- combinator. However, the 'serially' type combinator can be used to switch to
833-- this style of composition. We will see how this style of composition works
834-- in the following examples.
835--
836-- Let's start with an example with a simple @for@ loop without any nesting.
837-- For simplicity of illustration we are using streams of pure values in all
838-- the examples. However, the streams could also be made of monadic actions
839-- instead.
840--
841-- @
842-- import "Streamly"
843-- import qualified "Streamly.Prelude" as S
844--
845-- main = 'runStream' $ do
846-- x <- S.'fromFoldable' [3,2,1]
847-- delay x
848-- @
849-- @
850-- ThreadId 30: Delay 3
851-- ThreadId 30: Delay 2
852-- ThreadId 30: Delay 1
853-- @
854--
855-- As we can see, the code after the @fromFoldable@ statement is run three
856-- times, once for each value of @x@ drawn from the stream. All the three
857-- iterations are serial and run in the same thread one after another. In
858-- imperative terms this is equivalent to a @for@ loop with three iterations.
859--
860-- A console echo loop copying standard input to standard output can simply be
861-- written like this:
862--
863-- @
864-- import "Streamly"
865-- import qualified "Streamly.Prelude" as S
866--
867-- main = 'runStream' $ forever $ S.once getLine >>= S.once . putStrLn
868-- @
869--
870-- When multiple streams are composed using this style they nest in a DFS
871-- manner i.e. nested iterations of a loop are executed before we proceed to
872-- the next iteration of the parent loop. This behaves just like nested @for@
873-- loops in imperative programming.
874--
875-- @
876-- import "Streamly"
877-- import qualified "Streamly.Prelude" as S
878--
879-- main = 'runStream' $ do
880-- x <- S.'fromFoldable' [1,2]
881-- y <- S.'fromFoldable' [3,4]
882-- S.'once' $ putStrLn $ show (x, y)
883-- @
884-- @
885-- (1,3)
886-- (1,4)
887-- (2,3)
888-- (2,4)
889-- @
890--
891-- Notice that this is analogous to merging streams of type 'Serial' or merging
892-- streams using 'serial'.
894-- $concurrentNesting
895--
896-- The 'Monad' composition of 'Async' type can perform the iterations of a
897-- loop concurrently. Concurrency is demand driven i.e. more concurrent
898-- iterations are started only if the previous iterations are not able to
899-- produce enough output for the consumer of the output stream. This works
900-- exactly the same way as the merging of two streams 'asyncly' works.
901-- This is the concurrent analogue of 'Serial' style monadic composition.
902--
903-- The 'asyncly' type combinator can be used to switch to this style of
904-- composition. Alternatively, a type annotation can be used to specify the
905-- type of the stream as 'Async'.
906--
907-- @
908-- import "Streamly"
909-- import "Streamly.Prelude"
910--
911-- main = 'runStream' . 'asyncly' $ do
912-- x <- S.'fromFoldable' [3,2,1]
913-- delay x
914-- @
915-- @
916-- ThreadId 40: Delay 1
917-- ThreadId 39: Delay 2
918-- ThreadId 38: Delay 3
919-- @
920--
921-- As we can see the code after the @fromFoldable@ statement is run three
922-- times, once for each value of @x@. All the three iterations are concurrent
923-- and run in different threads. The iteration with least delay finishes first.
924-- When compared to imperative programming, this can be viewed as a @for@ loop
925-- with three concurrent iterations.
926--
927-- Concurrency is demand driven just as in the case of 'async' merging.
928-- When multiple streams are composed using this style, the iterations are
929-- triggered in a depth first manner just like 'Serial' i.e. nested iterations are
930-- executed before we proceed to the next iteration at higher level. However,
931-- unlike 'Serial' more than one iterations may be started concurrently based
932-- on the demand from the consumer of the stream.
933--
934-- @
935-- import "Streamly"
936-- import qualified "Streamly.Prelude" as S
937--
938-- main = 'runStream' . 'asyncly' $ do
939-- x <- S.'fromFoldable' [1,2]
940-- y <- S.'fromFoldable' [3,4]
941-- S.'once' $ putStrLn $ show (x, y)
942-- @
943-- @
944-- (1,3)
945-- (1,4)
946-- (2,3)
947-- (2,4)
948-- @
950-- $interleavedNesting
951--
952-- The 'Monad' composition of 'WSerial' type interleaves the iterations of
953-- outer and inner loops in a nested loop composition. This works exactly the
954-- same way as the merging of two streams in 'wSerially' fashion works. The
955-- 'wSerially' type combinator can be used to switch to this style of
956-- composition. Alternatively, a type annotation can be used to specify the
957-- type of the stream as 'WSerial'.
958--
959-- @
960-- import "Streamly"
961-- import qualified "Streamly.Prelude" as S
962--
963-- main = 'runStream' . 'wSerially' $ do
964-- x <- S.'fromFoldable' [1,2]
965-- y <- S.'fromFoldable' [3,4]
966-- S.once $ putStrLn $ show (x, y)
967-- @
968-- @
969-- (1,3)
970-- (2,3)
971-- (1,4)
972-- (2,4)
973-- @
974--
976-- $wasyncNesting
977--
978-- Just like 'Async' the 'Monad' composition of 'WAsync' runs the iterations of
979-- a loop concurrently. The difference is in the nested loop behavior. The
980-- nested loops in this type are traversed and executed in a breadth first
981-- manner rather than the depth first manner of 'Async' style.
982-- The loop nesting works exactly the same way as the merging of streams
983-- 'wAsyncly' works. The 'wAsyncly' type combinator can be used to switch to
984-- this style of composition. Alternatively, a type annotation can be used to
985-- specify the type of the stream as 'WAsync'.
986--
987-- @
988-- import "Streamly"
989-- import qualified "Streamly.Prelude" as S
990--
991-- main = 'runStream' . 'wAsyncly' $ do
992-- x <- S.'fromFoldable' [1,2]
993-- y <- S.'fromFoldable' [3,4]
994-- S.'once' $ putStrLn $ show (x, y)
995-- @
996-- @
997-- (1,3)
998-- (2,3)
999-- (1,4)
1000-- (2,4)
1001-- @
1003-- $parallelNesting
1005-- Just like 'Async' or 'WAsync' the 'Monad' composition of 'Parallel' runs the
1006-- iterations of a loop concurrently. The difference is in the nested loop
1007-- behavior. The streams at each nest level is run fully concurrently
1008-- irrespective of the demand. The loop nesting works exactly the same way as
1009-- the merging of streams 'parallely' works. The 'parallely' type combinator
1010-- can be used to switch to this style of composition. Alternatively, a type
1011-- annotation can be used to specify the type of the stream as 'Parallel'.
1013-- @
1014-- import "Streamly"
1015-- import qualified "Streamly.Prelude" as S
1017-- main = 'runStream' . 'parallely' $ do
1018-- x <- S.'fromFoldable' [3,2,1]
1019-- delay x
1020-- @
1021-- @
1022-- ThreadId 40: Delay 1
1023-- ThreadId 39: Delay 2
1024-- ThreadId 38: Delay 3
1025-- @
1027-- $monadExercise
1029-- Streamly code is usually written in a way that is agnostic of the
1030-- specific monadic composition type. We use a polymorphic type with a
1031-- 'IsStream' type class constraint. When running the stream we can choose the
1032-- specific mode of composition. For example take a look at the following code.
1034-- @
1035-- import "Streamly"
1036-- import qualified "Streamly.Prelude" as S
1038-- composed :: (IsStream t, Monad (t IO)) => t IO ()
1039-- composed = do
1040-- sz <- sizes
1041-- cl <- colors
1042-- sh <- shapes
1043-- S.'once' $ putStrLn $ show (sz, cl, sh)
1045-- where
1047-- sizes = S.'fromFoldable' [1, 2, 3]
1048-- colors = S.'fromFoldable' ["red", "green", "blue"]
1049-- shapes = S.'fromFoldable' ["triangle", "square", "circle"]
1050-- @
1052-- Now we can interpret this in whatever way we want:
1054-- @
1055-- main = 'runStream' . 'serially' $ composed
1056-- main = 'runStream' . 'wSerially' $ composed
1057-- main = 'runStream' . 'asyncly' $ composed
1058-- main = 'runStream' . 'wAsyncly' $ composed
1059-- main = 'runStream' . 'parallely' $ composed
1060-- @
1062-- As an exercise try to figure out the output of this code for each mode of
1063-- composition.
1065-- $functor
1067-- 'fmap' transforms a stream by mapping a function on all elements of the
1068-- stream. 'fmap' behaves in the same way for all stream types, it is always
1069-- serial.
1071-- @
1072-- import "Streamly"
1073-- import qualified "Streamly.Prelude" as S
1075-- main = (S.'toList' $ fmap show $ S.'fromFoldable' [1..10]) >>= print
1076-- @
1078-- Also see the 'mapM' and 'sequence' functions for mapping actions, in the
1079-- "Streamly.Prelude" module.
1081-- $applicative
1083-- Applicative is precisely the same as the 'ap' operation of 'Monad'. For
1084-- zipping applicatives separate types 'ZipSerial' and 'ZipAsync' are
1085-- provided.
1087-- The following example uses the 'Serial' applicative, it runs all iterations
1088-- serially and takes a total 17 seconds (1 + 3 + 4 + 2 + 3 + 4):
1090-- @
1091-- import "Streamly"
1092-- import qualified "Streamly.Prelude" as S
1093-- import Control.Concurrent
1095-- s1 = d 1 <> d 2
1096-- s2 = d 3 <> d 4
1097-- d n = delay n >> return n
1099-- main = (S.'toList' . 'serially' $ (,) \<$> s1 \<*> s2) >>= print
1100-- @
1101-- @
1102-- ThreadId 36: Delay 1
1103-- ThreadId 36: Delay 3
1104-- ThreadId 36: Delay 4
1105-- ThreadId 36: Delay 2
1106-- ThreadId 36: Delay 3
1107-- ThreadId 36: Delay 4
1108-- [(1,3),(1,4),(2,3),(2,4)]
1109-- @
1111-- Similalrly 'WSerial' applicative runs the iterations in an interleaved
1112-- order but since it is serial it takes a total of 17 seconds:
1114-- @
1115-- main = (S.'toList' . 'wSerially' $ (,) \<$> s1 \<*> s2) >>= print
1116-- @
1117-- @
1118-- ThreadId 36: Delay 1
1119-- ThreadId 36: Delay 3
1120-- ThreadId 36: Delay 2
1121-- ThreadId 36: Delay 3
1122-- ThreadId 36: Delay 4
1123-- ThreadId 36: Delay 4
1124-- [(1,3),(2,3),(1,4),(2,4)]
1125-- @
1127-- 'Async' can run the iterations concurrently and therefore takes a total
1128-- of 10 seconds (1 + 2 + 3 + 4):
1130-- @
1131-- main = (S.'toList' . 'asyncly' $ (,) \<$> s1 \<*> s2) >>= print
1132-- @
1133-- @
1134-- ThreadId 34: Delay 1
1135-- ThreadId 36: Delay 2
1136-- ThreadId 35: Delay 3
1137-- ThreadId 36: Delay 3
1138-- ThreadId 35: Delay 4
1139-- ThreadId 36: Delay 4
1140-- [(1,3),(2,3),(1,4),(2,4)]
1141-- @
1143-- Similalrly 'WAsync' as well can run the iterations concurrently and
1144-- therefore takes a total of 10 seconds (1 + 2 + 3 + 4):
1146-- @
1147-- main = (S.'toList' . 'wAsyncly' $ (,) \<$> s1 \<*> s2) >>= print
1148-- @
1149-- @
1150-- ThreadId 34: Delay 1
1151-- ThreadId 36: Delay 2
1152-- ThreadId 35: Delay 3
1153-- ThreadId 36: Delay 3
1154-- ThreadId 35: Delay 4
1155-- ThreadId 36: Delay 4
1156-- [(1,3),(2,3),(1,4),(2,4)]
1157-- @
1159-- $zipping
1161-- Zipping is a special transformation where the corresponding elements of two
1162-- streams are combined together using a zip function producing a new stream of
1163-- outputs. Two different types are provided for serial and concurrent zipping.
1164-- These types provide an applicative instance that can be used to lift
1165-- functions to zip the argument streams.
1166-- Also see the zipping functions in the "Streamly.Prelude" module.
1168-- $serialzip
1170-- The applicative instance of 'ZipSerial' type zips streams serially.
1171-- 'zipSerially' type combinator can be used to switch to serial applicative
1172-- zip composition:
1174-- @
1175-- import "Streamly"
1176-- import qualified "Streamly.Prelude" as S
1177-- import Control.Concurrent
1179-- d n = delay n >> return n
1180-- s1 = 'serially' $ d 1 <> d 2
1181-- s2 = 'serially' $ d 3 <> d 4
1183-- main = (S.'toList' . 'zipSerially' $ (,) \<$> s1 \<*> s2) >>= print
1184-- @
1186-- This takes total 10 seconds to zip, which is (1 + 2 + 3 + 4) since
1187-- everything runs serially:
1189-- @
1190-- ThreadId 29: Delay 1
1191-- ThreadId 29: Delay 3
1192-- ThreadId 29: Delay 2
1193-- ThreadId 29: Delay 4
1194-- [(1,3),(2,4)]
1195-- @
1197-- $parallelzip
1199-- The applicative instance of 'ZipAsync' type zips streams concurrently.
1200-- 'zipAsyncly' type combinator can be used to switch to parallel applicative
1201-- zip composition:
1204-- @
1205-- import "Streamly"
1206-- import qualified "Streamly.Prelude" as S
1207-- import Control.Concurrent
1208-- import System.IO (stdout, hSetBuffering, BufferMode(LineBuffering))
1210-- d n = delay n >> return n
1211-- s1 = 'serially' $ d 1 <> d 2
1212-- s2 = 'serially' $ d 3 <> d 4
1214-- main = do
1215-- hSetBuffering stdout LineBuffering
1216-- (S.'toList' . 'zipAsyncly' $ (,) \<$> s1 \<*> s2) >>= print
1217-- @
1219-- This takes 7 seconds to zip, which is max (1,3) + max (2,4) because 1 and 3
1220-- are produced concurrently, and 2 and 4 are produced concurrently:
1222-- @
1223-- ThreadId 32: Delay 1
1224-- ThreadId 32: Delay 2
1225-- ThreadId 33: Delay 3
1226-- ThreadId 33: Delay 4
1227-- [(1,3),(2,4)]
1228-- @
1230-- $concurrent
1232-- When writing concurrent programs there are two distinct places where the
1233-- programmer can control the concurrency. First, when /composing/ a stream by
1234-- merging multiple streams we can choose an appropriate sum style operators to
1235-- combine them concurrently or serially. Second, when /processing/ a stream in
1236-- a monadic composition we can choose one of the monad composition types to
1237-- choose the desired type of concurrency.
1239-- In the following example the squares of @x@ and @y@ are computed
1240-- concurrently using the 'async' operation and the square roots of their
1241-- sum are computed serially because of the 'streamly' combinator. We can
1242-- choose different combinators for the monadic processing and the stream
1243-- generation, to control the concurrency. We can also use the 'asyncly'
1244-- combinator instead of explicitly folding with 'async'.
1246-- @
1247-- import "Streamly"
1248-- import qualified "Streamly.Prelude" as S
1249-- import Data.List (sum)
1251-- main = do
1252-- z \<- S.'toList'
1253-- $ 'serially' -- Serial monadic processing (sqrt below)
1254-- $ do
1255-- x2 \<- 'forEachWith' 'async' [1..100] $ -- Concurrent @"for"@ loop
1256-- \\x -> return $ x * x -- body of the loop
1257-- y2 \<- 'forEachWith' 'async' [1..100] $
1258-- \\y -> return $ y * y
1259-- return $ sqrt (x2 + y2)
1260-- print $ sum z
1261-- @
1263-- We can see how this directly maps to the imperative style
1264-- <https://en.wikipedia.org/wiki/OpenMP OpenMP> model, we use combinators
1265-- and operators instead of the ugly pragmas.
1267-- For more concurrent programming examples see,
1268-- "ListDir.hs", "MergeSort.hs" and "SearchQuery.hs" in the examples directory.
1270-- $reactive
1272-- Reactive programming is nothing but concurrent streaming which is what
1273-- streamly is all about. With streamly we can generate streams of events,
1274-- merge streams that are generated concurrently and process events
1275-- concurrently. We can do all this without any knowledge about the specifics
1276-- of the implementation of concurrency. In the following example you will see
1277-- that the code is just regular Haskell code without much streamly APIs used
1278-- (active hyperlinks are the streamly APIs) and yet it is a reactive
1279-- application.
1281-- This application has two independent and concurrent sources of event
1282-- streams, @acidRain@ and @userAction@. @acidRain@ continuously generates
1283-- events that deteriorate the health of the character in the game.
1284-- @userAction@ can be "potion" or "quit". When the user types "potion" the
1285-- health improves and the game continues.
1287-- @
1288-- {-\# LANGUAGE FlexibleContexts #-}
1290-- import "Streamly"
1291-- import Control.Concurrent (threadDelay)
1292-- import Control.Monad (when)
1293-- import Control.Monad.IO.Class (MonadIO(..))
1294-- import Control.Monad.State (MonadState, get, modify, runStateT)
1295-- import Data.Semigroup (cycle1)
1297-- data Event = Harm Int | Heal Int | Quit deriving (Show)
1299-- userAction :: MonadIO m => 'SerialT' m Event
1300-- userAction = cycle1 $ liftIO askUser
1301-- where
1302-- askUser = do
1303-- command <- getLine
1304-- case command of
1305-- "potion" -> return (Heal 10)
1306-- "quit" -> return Quit
1307-- _ -> putStrLn "What?" >> askUser
1309-- acidRain :: MonadIO m => 'SerialT' m Event
1310-- acidRain = cycle1 $ liftIO (threadDelay 1000000) >> return (Harm 1)
1312-- game :: ('MonadAsync' m, MonadState Int m) => 'SerialT' m ()
1313-- game = do
1314-- event \<- userAction \`parallel` acidRain
1315-- case event of
1316-- Harm n -> modify $ \\h -> h - n
1317-- Heal n -> modify $ \\h -> h + n
1318-- Quit -> fail "quit"
1320-- h <- get
1321-- when (h <= 0) $ fail "You die!"
1322-- liftIO $ putStrLn $ "Health = " ++ show h
1324-- main = do
1325-- putStrLn "Your health is deteriorating due to acid rain,\\
1326-- \\ type \\"potion\\" or \\"quit\\""
1327-- _ <- runStateT ('runStream' game) 60
1328-- return ()
1329-- @
1331-- You can also find the source of this example in the examples directory as
1332-- "AcidRain.hs". It has been adapted from Gabriel's
1333-- <https://hackage.haskell.org/package/pipes-concurrency-2.0.8/docs/Pipes-Concurrent-Tutorial.html pipes-concurrency>
1334-- package.
1335-- This is much simpler compared to the pipes version because of the builtin
1336-- concurrency in streamly. You can also find a SDL based reactive programming
1337-- example adapted from Yampa in "Streamly.Examples.CirclingSquare".
1339-- $performance
1341-- Streamly is highly optimized for performance, it is designed for serious
1342-- high performing, concurrent and scalable applications. We have created the
1343-- <https://hackage.haskell.org/package/streaming-benchmarks streaming-benchmarks>
1344-- package which is specifically and carefully designed to measure the
1345-- performance of Haskell streaming libraries fairly and squarely in the right
1346-- way. Streamly performs at par or even better than most streaming libraries
1347-- for serial operations even though it needs to deal with the concurrency
1348-- capability.
1350-- $interop
1352-- We can use @unfoldr@ and @uncons@ to convert one streaming type to another.
1354-- Interop with @vector@:
1356-- @
1357-- import Streamly
1358-- import qualified Streamly.Prelude as S
1359-- import qualified Data.Vector.Fusion.Stream.Monadic as V
1361-- -- | vector to streamly
1362-- fromVector :: (IsStream t, Monad m) => V.Stream m a -> t m a
1363-- fromVector = S.unfoldrM unconsV
1364-- where
1365-- unconsV v = do
1366-- r <- V.null v
1367-- if r
1368-- then return Nothing
1369-- else do
1370-- h <- V.head v
1371-- return $ Just (h, V.tail v)
1373-- -- | streamly to vector
1374-- toVector :: Monad m => SerialT m a -> V.Stream m a
1375-- toVector = V.unfoldrM (S.uncons . adapt)
1377-- main = do
1378-- S.toList (fromVector (V.fromList [1..3])) >>= print
1379-- V.toList (toVector (S.fromFoldable [1..3])) >>= print
1380-- @
1382-- Interop with @pipes@:
1384-- @
1385-- import "Streamly"
1386-- import qualified "Streamly.Prelude" as S
1387-- import qualified Pipes as P
1388-- import qualified Pipes.Prelude as P
1390-- -- | pipes to streamly
1391-- fromPipes :: (IsStream t, Monad m) => P.Producer a m r -> t m a
1392-- fromPipes = S.'unfoldrM' unconsP
1393-- where
1394-- -- Adapt P.next to return a Maybe instead of Either
1395-- unconsP p = P.next p >>= either (\\_ -> return Nothing) (return . Just)
1397-- -- | streamly to pipes
1398-- toPipes :: Monad m => SerialT m a -> P.Producer a m ()
1399-- toPipes = P.unfoldr unconsS
1400-- where
1401-- -- Adapt S.uncons to return an Either instead of Maybe
1402-- unconsS s = S.'uncons' s >>= maybe (return $ Left ()) (return . Right)
1404-- main = do
1405-- S.'toList' (fromPipes (P.each [1..3])) >>= print
1406-- P.toListM (toPipes (S.'fromFoldable' [1..3])) >>= print
1407-- @
1409-- Interop with @streaming@:
1411-- @
1412-- import "Streamly"
1413-- import qualified "Streamly.Prelude" as S
1414-- import qualified Streaming as SG
1415-- import qualified Streaming.Prelude as SG
1417-- -- | streaming to streamly
1418-- fromStreaming :: (IsStream t, Monad m) => SG.Stream (SG.Of a) m r -> t m a
1419-- fromStreaming = S.unfoldrM SG.uncons
1421-- -- | streamly to streaming
1422-- toStreaming :: Monad m => SerialT m a -> SG.Stream (SG.Of a) m ()
1423-- toStreaming = SG.unfoldr unconsS
1424-- where
1425-- -- Adapt S.uncons to return an Either instead of Maybe
1426-- unconsS s = S.'uncons' s >>= maybe (return $ Left ()) (return . Right)
1428-- main = do
1429-- S.toList (fromStreaming (SG.each [1..3])) >>= print
1430-- SG.toList (toStreaming (S.fromFoldable [1..3])) >>= print
1431-- @
1433-- Interop with @conduit@:
1435-- @
1436-- import "Streamly"
1437-- import qualified "Streamly.Prelude" as S
1438-- import qualified Data.Conduit as C
1439-- import qualified Data.Conduit.List as C
1440-- import qualified Data.Conduit.Combinators as C
1442-- -- It seems there is no way out of a conduit as it does not provide an
1443-- -- uncons or a tail function. We can convert streamly to conduit though.
1445-- -- | streamly to conduit
1446-- toConduit :: Monad m => SerialT m a -> C.ConduitT i a m ()
1447-- toConduit s = C.unfoldM S.'uncons' s
1449-- main = do
1450-- C.runConduit (toConduit (S.'fromFoldable' [1..3]) C..| C.sinkList) >>= print
1451-- @
1453-- $comparison
1455-- List transformers and logic programming monads also provide a product style
1456-- composition similar to streamly, however streamly generalizes it with the
1457-- time dimension; allowing streams to be composed in an asynchronous and
1458-- concurrent fashion in many different ways. It also provides multiple
1459-- alternative ways of composing streams e.g. serial, interleaved or
1460-- concurrent.
1462-- This seemingly simple addition of asynchronicity and concurrency to product
1463-- style streaming composition unifies a number of disparate abstractions into
1464-- one powerful, concise and elegant abstraction. A wide variety of
1465-- programming problems can be solved elegantly with this abstraction. In
1466-- particular, it unifies three major programming domains namely
1467-- non-deterministic (logic) programming, concurrent programming and functional
1468-- reactive programming. In other words, you can do everything with this one
1469-- abstraction that you could do with the popular libraries listed under these
1470-- categories in the list below.
1472-- @
1473-- +-----------------+----------------+
1474-- | Non-determinism | <https://hackage.haskell.org/package/pipes pipes> |
1475-- | +----------------+
1476-- | | <https://hackage.haskell.org/package/list-t list-t> |
1477-- | +----------------+
1478-- | | <https://hackage.haskell.org/package/logict logict> |
1479-- +-----------------+----------------+
1480-- | Streaming | <https://hackage.haskell.org/package/vector vector> |
1481-- | +----------------+
1482-- | | <https://hackage.haskell.org/package/streaming streaming> |
1483-- | +----------------+
1484-- | | <https://hackage.haskell.org/package/pipes pipes> |
1485-- | +----------------+
1486-- | | <https://hackage.haskell.org/package/conduit conduit> |
1487-- +-----------------+----------------+
1488-- | Concurrency | <https://hackage.haskell.org/package/async async> |
1489-- | +----------------+
1490-- | | <https://hackage.haskell.org/package/transient transient> |
1491-- +-----------------+----------------+
1492-- | FRP | <https://hackage.haskell.org/package/Yampa Yampa> |
1493-- | +----------------+
1494-- | | <https://hackage.haskell.org/package/dunai dunai> |
1495-- | +----------------+
1496-- | | <https://hackage.haskell.org/package/reflex reflex> |
1497-- +-----------------+----------------+
1498-- @
1500-- Streamly is a list-transformer. It provides all the functionality provided
1501-- by any of the list transformer and logic programming packages listed above.
1502-- In addition, Streamly naturally integrates the concurrency dimension to the
1503-- basic list transformer functionality.
1505-- When it comes to streaming, in terms of the streaming API streamly is almost
1506-- identical to the vector package. Streamly, vector and streaming packages all
1507-- represent a stream as data and are therefore similar in the fundamental
1508-- approach to streaming. The fundamental difference is that streamly adds
1509-- concurrency support and the monad instance provides concurrent looping.
1510-- Other streaming libraries like pipes, conduit and machines represent and
1511-- compose stream processors rather than the stream data and therefore fall in
1512-- another class of streaming libraries and have comparatively more complicated
1513-- types.
1515-- When it comes to concurrency, streamly can do everything that the @async@
1516-- package can do and more. async provides applicative concurrency whereas
1517-- streamly provides both applicative and monadic concurrency. The
1518-- 'ZipAsync' type behaves like the applicative instance of async. In
1519-- comparison to transient streamly has a first class streaming interface and
1520-- is a monad transformer that can be used universally in any Haskell monad
1521-- transformer stack. Streamly was in fact originally inspired by the
1522-- concurrency implementation in @transient@ though it has no resemblence with
1523-- that and takes a lazy pull approach versus transient's strict push approach.
1525-- The non-determinism, concurrency and streaming combination make streamly a
1526-- strong FRP capable library as well. FRP is fundamentally stream of events
1527-- that can be processed concurrently. The example in this tutorial as well as
1528-- the "Streamly.Examples.CirclingSquare" example from Yampa demonstrate the
1529-- basic FRP capability of streamly. In core concepts streamly is strikingly
1530-- similar to @dunai@. dunai was designed from a FRP perspective and streamly
1531-- was originally designed from a concurrency perspective. However, both have
1532-- similarity at the core.
1534-- $furtherReading
1536-- * Read the documentation of "Streamly" module
1537-- * Read the documentation of "Streamly.Prelude" module
1538-- * See the examples in the "examples" directory of the package
1539-- * See the tests in the "test" directory of the package