on defafa6 help sign in
115 lines 3.2 kB history raw
1-- |
2-- Module : Streamly.Data.Stream.MkType
3-- Copyright : (c) 2022 Composewell Technologies
4-- License : BSD-3-Clause
5-- Maintainer : streamly@composewell.com
6-- Stability : released
7-- Portability : GHC
8--
9-- Template Haskell macros to create custom newtype wrappers for the 'Stream'
10-- type. See the examples below to create the standard stream types that were
11-- available in streamly versions before 0.9.0.
12--
13-- To use this module, the following extensions must be enabled:
14--
15-- >>> :set -XStandaloneDeriving
16-- >>> :set -XTemplateHaskell
17-- >>> :set -XTypeFamilies
18-- >>> :set -XUndecidableInstances
19--
20-- Import this module unqualified to bring everything needed in scope without
21-- having to import several other modules. Also, "Streamly.Data.Stream" or
22-- "Streamly.Data.Stream.Prelude" must be imported @as Stream@.
23--
24-- >>> import Streamly.Data.Stream.MkType
25-- >>> import qualified Streamly.Data.Stream.Prelude as Stream
26--
27-- For 'Streamly.Prelude.AsyncT' monad type with a concurrent cross product
28-- bind:
29--
30-- >>> :{
31-- bind = flip (Stream.parConcatMap id)
32-- $(mkCrossType "AsyncT" "bind" True)
33-- :}
34--
35-- For 'Streamly.Prelude.WAsyncT' monad type with a concurrent interleaved
36-- bind:
37--
38-- >>> :{
39-- bind = flip (Stream.parConcatMap (Stream.interleaved True))
40-- $(mkCrossType "WAsyncT" "bind" True)
41-- :}
42--
43-- For 'Streamly.Prelude.AheadT' monad type with a concurrent ordered
44-- cross product bind:
45--
46-- >>> :{
47-- bind = flip (Stream.parConcatMap (Stream.ordered True))
48-- $(mkCrossType "AheadT" "bind" True)
49-- :}
50--
51-- For 'Streamly.Prelude.ParallelT' monad type with an eager concurrent cross
52-- product bind:
53--
54-- >>> :{
55-- parBind = flip (Stream.parConcatMap (Stream.eager True))
56-- $(mkCrossType "ParallelT" "parBind" True)
57-- :}
58--
59-- For 'Streamly.Prelude.ZipSerialM' serial zipping applicative type:
60--
61-- >>> :{
62-- zipApply = Stream.zipWith ($)
63-- $(mkZipType "ZipSerialM" "zipApply" False)
64-- :}
65--
66-- For 'Streamly.Prelude.ZipAsync' concurrent zipping applicative type:
67--
68-- >>> :{
69-- parApply = Stream.parApply id
70-- $(mkZipType "ZipAsync" "parApply" True)
71-- :}
72--
73-- Instead of using these macros directly you could use the generated code as
74-- well. Use these macros in ghci to generate the required code and paste it in
75-- your package, you can customize the code as desired. See the docs of the
76-- macros below for examples about how to view the generated code. For example:
77--
78-- >>> bind = flip (Stream.parConcatMap id)
79-- >>> expr <- runQ (mkCrossType "AsyncT" "bind" True)
80--
81-- >> putStrLn $ pprint expr
82--
83module Streamly.Data.Stream.MkType
84 (
85 -- * Imports for Examples
86 -- $setup
88 -- * Template Haskell Macros
89 mkZipType
90 , mkCrossType
92 -- * Re-exports
93 , Read(..)
94 , MonadIO(..)
95 , MonadThrow(..)
96 , MonadReader(..)
97 , MonadTrans(..)
98 , Identity
99 , IsList
100 , IsString
101 , ap
102 )
103where
105import Data.Functor.Identity (Identity)
106import GHC.Exts (IsList, IsString)
107import Text.Read (Read(..))
109import Streamly.Internal.Data.Stream.MkType
111-- $setup
112-- >>> :m
113-- >>> import Language.Haskell.TH
114-- >>> import qualified Streamly.Data.Stream.Prelude as Stream
115-- >>> import Streamly.Data.Stream.MkType