on master help sign in
Publish the Streamly.Data.Stream.MkType module
commit734d2cf874143a6b04afc052aee52273fe7c07d8 authorHarendra Kumar <harendra@composewell.com> dateFri 6 Jan 2023, 16:47 (4 years ago) parent9510828
3 files changed+86−56
src/Streamly/Data/Stream/MkType.hs+81 src/Streamly/Internal/Data/Stream/MkType.hs+4−56 streamly.cabal+1
src/Streamly/Data/Stream/MkType.hsnew file+81
@@ -0,0 +1,81 @@
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, deriving all the usual instances.
11+--
12+-- To use this module, "Streamly.Data.Stream" or "Streamly.Data.Stream.Prelude"
13+-- must be imported as "Stream". Also, it is recommended to import this module
14+-- unqualified to bring everything needed in scope without having to import
15+-- several other modules.
16+--
17+-- >>> import qualified Streamly.Data.Stream.Prelude as Stream
18+-- >>> import Streamly.Internal.Data.Stream.MkType
19+--
20+-- Instead of using these macros directly you could use the generated code as
21+-- well. Use these macros in ghci to generate the required code and paste it in
22+-- your package, you can customize it as you want. See the docs of the macros
23+-- below for examples of viewing the generated code.
24+--
25+-- Example, create an applicative type with zipping apply:
26+--
27+-- >>> import Streamly.Data.Stream.Prelude (Stream)
28+-- >>> :{
29+-- zipApply :: Monad m => Stream m (a -> b) -> Stream m a -> Stream m b
30+-- zipApply = Stream.zipWith ($)
31+-- :}
32+--
33+-- > $(mkZipType "ZipStream" "zipApply" False)
34+--
35+-- Example, create an applicative type with concurrent zipping apply:
36+--
37+-- > $(mkZipType "ParZipStream" "parApply" True)
38+--
39+-- Example, create a monad type with an interleaving cross product bind:
40+--
41+-- >>> :{
42+-- interleaveBind :: Stream m a -> (a -> Stream m b) -> Stream m b
43+-- interleaveBind = flip (Stream.concatMapWith Stream.interleave)
44+-- :}
45+--
46+-- > $(mkCrossType "InterleaveStream" "interleaveBind" False)
47+--
48+-- Example, create a monad type with an eager concurrent cross product bind:
49+--
50+-- >>> :{
51+-- parBind :: Stream.MonadAsync m => Stream m a -> (a -> Stream m b) -> Stream m b
52+-- parBind = flip (Stream.parConcatMap (Stream.eager True))
53+-- :}
54+--
55+-- > $(mkCrossType "ParEagerStream" "parBind" True)
56+--
57+module Streamly.Data.Stream.MkType
58+ (
59+ -- * Imports for Examples
60+ -- $setup
61+
62+ -- * Template Haskell Macros
63+ mkZipType
64+ , mkCrossType
65+
66+ -- * Re-exports
67+ , MonadIO(..)
68+ , MonadThrow(..)
69+ , MonadReader(..)
70+ , MonadTrans(..)
71+ , ap
72+ )
73+where
74+
75+import Streamly.Internal.Data.Stream.MkType
76+
77+-- $setup
78+-- >>> :m
79+-- >>> import Language.Haskell.TH
80+-- >>> import qualified Streamly.Data.Stream.Prelude as Stream
81+-- >>> import Streamly.Internal.Data.Stream.MkType
src/Streamly/Internal/Data/Stream/MkType.hs+4−56
@@ -6,55 +6,6 @@
66-- Stability : experimental
77-- Portability : GHC
88--
9−-- Template Haskell macros to create custom newtype wrappers for the 'Stream'
10−-- type, deriving all the usual instances.
11−--
12−-- To use this module, "Streamly.Data.Stream" or "Streamly.Data.Stream.Prelude"
13−-- must be imported as "Stream". Also, it is recommended to import this module
14−-- unqualified to bring everything needed in scope without having to import
15−-- several other modules.
16−--
17−-- >>> import Control.Monad.Reader.Class (MonadReader(..))
18−-- >>> import qualified Streamly.Data.Stream.Prelude as Stream
19−-- >>> import Streamly.Internal.Data.Stream.MkType
20−--
21−-- Instead of using these macros directly you could use the generated code as
22−-- well. Use these macros in ghci to generate the required code and paste it in
23−-- your package, you can customize it as you want. See the docs of the macros
24−-- below for examples of viewing the generated code.
25−--
26−-- Example, create an applicative type with zipping apply:
27−--
28−-- >>> import Streamly.Data.Stream.Prelude (Stream)
29−-- >>> :{
30−-- zipApply :: Monad m => Stream m (a -> b) -> Stream m a -> Stream m b
31−-- zipApply = Stream.zipWith ($)
32−-- :}
33−--
34−-- > $(mkZipType "ZipStream" "zipApply" False)
35−--
36−-- Example, create an applicative type with concurrent zipping apply:
37−--
38−-- > $(mkZipType "ParZipStream" "parApply" True)
39−--
40−-- Example, create a monad type with an interleaving cross product bind:
41−--
42−-- >>> :{
43−-- interleaveBind :: Stream m a -> (a -> Stream m b) -> Stream m b
44−-- interleaveBind = flip (Stream.concatMapWith Stream.interleave)
45−-- :}
46−--
47−-- > $(mkCrossType "InterleaveStream" "interleaveBind" False)
48−--
49−-- Example, create a monad type with an eager concurrent cross product bind:
50−--
51−-- >>> :{
52−-- parBind :: Stream.MonadAsync m => Stream m a -> (a -> Stream m b) -> Stream m b
53−-- parBind = flip (Stream.parConcatMap (Stream.eager True))
54−-- :}
55−--
56−-- > $(mkCrossType "ParEagerStream" "parBind" True)
57−--
589module Streamly.Internal.Data.Stream.MkType
5910 (
6011 -- * Imports for Examples
@@ -67,7 +18,7 @@ module Streamly.Internal.Data.Stream.MkType
6718 -- * Re-exports
6819 , MonadIO(..)
6920 , MonadThrow(..)
70− -- , MonadReader(..)
21+ , MonadReader(..)
7122 , MonadTrans(..)
7223 , ap
7324 ) where
@@ -82,16 +33,13 @@ import Language.Haskell.TH.Syntax
8233import Control.Monad (ap)
8334import Control.Monad.Catch (MonadThrow(..))
8435import Control.Monad.IO.Class (MonadIO(..))
85−-- import Control.Monad.Reader.Class (MonadReader(..))
36+import Control.Monad.Reader.Class (MonadReader(..))
8637import Control.Monad.Trans.Class (MonadTrans(..))
8738import Prelude hiding (repeat)
8839
8940-- $setup
9041-- >>> :m
91−-- >>> :set -package mtl
92−-- >>> :set -package streamly
9342-- >>> import Language.Haskell.TH
94−-- >>> import Control.Monad.Reader.Class (MonadReader(..))
9543-- >>> import qualified Streamly.Data.Stream.Prelude as Stream
9644-- >>> import Streamly.Internal.Data.Stream.MkType
9745
@@ -391,7 +339,7 @@ mkSimpleStreamInstances dtNameStr = do
391339 []
392340 ]
393341 ]
394− reader <-
342+ rdr <-
395343 instanceD
396344 (sequence
397345 [ appT (conT _Monad) (appT (conT _Type) (varT _m))
@@ -416,7 +364,7 @@ mkSimpleStreamInstances dtNameStr = do
416364 []
417365 ]
418366 ]
419− return [trans, io, throw, reader]
367+ return [trans, io, throw, rdr]
420368
421369 where
422370
streamly.cabal+1
@@ -381,6 +381,7 @@ library
381381 , Streamly.Internal.Network.Inet.TCP
382382
383383 -- Exposed modules
384+ , Streamly.Data.Stream.MkType
384385 , Streamly.Data.Stream.Prelude
385386
386387 -- Network/IO