on master help sign in
Remove deepseq and mtl dependencies from streamly-core
commit449d48d379310d1b2a1b22be0c49a9ae29925dff authorHarendra Kumar <harendra@composewell.com> dateThu 17 Nov 2022, 13:21 (4 years ago) parent4f86266
33 files changed+180−148
benchmark/Streamly/Benchmark/Data/Array.hs+6 benchmark/Streamly/Benchmark/Data/Array/Generic.hs+6 benchmark/Streamly/Benchmark/Data/Array/Mut.hs+5 benchmark/Streamly/Benchmark/Data/Fold.hs+12−1 benchmark/Streamly/Benchmark/Data/Stream/Eliminate.hs+6 benchmark/Streamly/Benchmark/Data/Stream/Generate.hs+9−1 benchmark/Streamly/Benchmark/Data/Stream/Lift.hs+6−7 benchmark/Streamly/Benchmark/Data/Stream/Transform.hs+8−1 benchmark/Streamly/Benchmark/Unicode/Char.hs+9−3 benchmark/Streamly/Benchmark/Unicode/Utf8.hs+2−2 core/src/Streamly/Internal/Data/Array/Generic.hs−5 core/src/Streamly/Internal/Data/Array/Mut/Type.hs−11 core/src/Streamly/Internal/Data/Array/Type.hs−10 core/src/Streamly/Internal/Data/List.hs+2−4 core/src/Streamly/Internal/Data/Parser/ParserD/Type.hs−19 core/src/Streamly/Internal/Data/Parser/ParserK/Type.hs+8−20 core/src/Streamly/Internal/Data/Stream/StreamK.hs−1 core/src/Streamly/Internal/Data/Stream/StreamK/Type.hs+2−16 core/src/Streamly/Internal/Data/Stream/Transform.hs+1 core/src/Streamly/Internal/Data/Stream/Type.hs−30 core/src/Streamly/Internal/Data/Stream/Zip.hs−3 core/streamly-core.cabal−3 hie.yaml+2 src/Streamly/Data/Array/Foreign.hs+13 core/src/Streamly/Internal/Data/Cont.hs → src/Streamly/Internal/Data/Cont.hs src/Streamly/Internal/Data/Stream/Ahead.hs+9−1 src/Streamly/Internal/Data/Stream/Async.hs+9−1 src/Streamly/Internal/Data/Stream/Instances.hs+1−1 src/Streamly/Internal/Data/Stream/IsStream.hs+9−1 src/Streamly/Internal/Data/Stream/Parallel.hs+9−1 src/Streamly/Internal/Data/Stream/Serial.hs+44−2 src/Streamly/Internal/Unicode/Utf8.hs+1−4 streamly.cabal+1
benchmark/Streamly/Benchmark/Data/Array.hs+6
@@ -1,4 +1,5 @@
11{-# OPTIONS_GHC -Wno-deprecations #-}
2+{-# OPTIONS_GHC -Wno-orphans #-}
23
34{-# LANGUAGE CPP #-}
45
@@ -11,10 +12,15 @@ import qualified GHC.Exts as GHC
1112
1213-- import qualified Streamly.Data.Array as A
1314import qualified Streamly.Internal.Data.Array as A
15+
1416type Stream = A.Array
1517
1618#include "Streamly/Benchmark/Data/Array/Common.hs"
1719
20+instance NFData (A.Array a) where
21+ {-# INLINE rnf #-}
22+ rnf _ = ()
23+
1824-------------------------------------------------------------------------------
1925-- Benchmark helpers
2026-------------------------------------------------------------------------------
benchmark/Streamly/Benchmark/Data/Array/Generic.hs+6
@@ -1,4 +1,5 @@
11{-# OPTIONS_GHC -Wno-deprecations #-}
2+{-# OPTIONS_GHC -Wno-orphans #-}
23
34{-# LANGUAGE CPP #-}
45
@@ -8,10 +9,15 @@ import Control.DeepSeq (deepseq)
89
910import qualified Streamly.Internal.Data.Array.Generic as IA
1011import qualified Streamly.Internal.Data.Array.Generic as A
12+
1113type Stream = A.Array
1214
1315#include "Streamly/Benchmark/Data/Array/Common.hs"
1416
17+instance NFData a => NFData (A.Array a) where
18+ {-# INLINE rnf #-}
19+ rnf = A.foldl' (\_ x -> rnf x) ()
20+
1521-------------------------------------------------------------------------------
1622-- Benchmark helpers
1723-------------------------------------------------------------------------------
benchmark/Streamly/Benchmark/Data/Array/Mut.hs+5
@@ -1,4 +1,5 @@
11{-# OPTIONS_GHC -Wno-deprecations #-}
2+{-# OPTIONS_GHC -Wno-orphans #-}
23
34-- |
45-- Module : Streamly.Benchmark.Data.Array.Unboxed.Mut
@@ -53,6 +54,10 @@ import Streamly.Benchmark.Common hiding (benchPureSrc)
5354
5455type Stream = MArray.Array
5556
57+instance NFData (MArray.Array a) where
58+ {-# INLINE rnf #-}
59+ rnf _ = ()
60+
5661-------------------------------------------------------------------------------
5762-- Benchmark helpers
5863-------------------------------------------------------------------------------
benchmark/Streamly/Benchmark/Data/Fold.hs+12−1
@@ -6,16 +6,18 @@
66-- Maintainer : streamly@composewell.com
77
88{-# LANGUAGE FlexibleContexts #-}
9+{-# LANGUAGE FlexibleInstances #-}
910{-# LANGUAGE RankNTypes #-}
1011{-# LANGUAGE ScopedTypeVariables #-}
1112{-# OPTIONS_GHC -fno-warn-warnings-deprecations #-}
13+{-# OPTIONS_GHC -Wno-orphans #-}
1214
1315module Main (main) where
1416
1517import Control.DeepSeq (NFData(..))
1618import Control.Monad (when)
1719import Control.Monad.IO.Class (MonadIO(..))
18−import Data.Functor.Identity (Identity)
20+import Data.Functor.Identity (Identity(..))
1921import Data.Map.Strict (Map)
2022import Data.Hashable (Hashable)
2123import Data.HashMap.Strict (HashMap)
@@ -27,6 +29,7 @@ import Streamly.Internal.Data.Stream (Stream)
2729import Streamly.Internal.Data.Fold (Fold(..))
2830import Streamly.Internal.Data.IsMap.HashMap ()
2931
32+import qualified Streamly.Internal.Data.Array.Mut.Type as MArray
3033import qualified Streamly.Internal.Data.Fold as FL
3134import qualified Streamly.Internal.Data.Fold.Extra as FL
3235import qualified Streamly.Internal.Data.Unfold as Unfold
@@ -288,6 +291,14 @@ unfoldMany val =
288291moduleName :: String
289292moduleName = "Data.Fold"
290293
294+instance NFData (MArray.Array a) where
295+ {-# INLINE rnf #-}
296+ rnf _ = ()
297+
298+instance NFData a => NFData (Stream Identity a) where
299+ {-# INLINE rnf #-}
300+ rnf xs = runIdentity $ Stream.fold (FL.foldl' (\_ x -> rnf x) ()) xs
301+
291302o_1_space_serial_elimination :: Int -> [Benchmark]
292303o_1_space_serial_elimination value =
293304 [ bgroup "elimination"
benchmark/Streamly/Benchmark/Data/Stream/Eliminate.hs+6
@@ -6,9 +6,11 @@
66
77{-# LANGUAGE CPP #-}
88{-# LANGUAGE FlexibleContexts #-}
9+{-# LANGUAGE FlexibleInstances #-}
910{-# LANGUAGE ScopedTypeVariables #-}
1011{-# LANGUAGE RankNTypes #-}
1112
13+{-# OPTIONS_GHC -Wno-orphans #-}
1214#ifdef USE_PRELUDE
1315{-# OPTIONS_GHC -Wno-deprecations #-}
1416#endif
@@ -252,6 +254,10 @@ o_n_space_elimination_foldable value =
252254-- Stream folds
253255-------------------------------------------------------------------------------
254256
257+instance NFData a => NFData (Stream Identity a) where
258+ {-# INLINE rnf #-}
259+ rnf xs = runIdentity $ S.fold (Fold.foldl' (\_ x -> rnf x) ()) xs
260+
255261{-# INLINE benchPureSink #-}
256262benchPureSink :: NFData b
257263 => Int -> String -> (Stream Identity Int -> b) -> Benchmark
benchmark/Streamly/Benchmark/Data/Stream/Generate.hs+9−1
@@ -6,19 +6,23 @@
66
77{-# LANGUAGE CPP #-}
88{-# LANGUAGE FlexibleContexts #-}
9+{-# LANGUAGE FlexibleInstances #-}
910{-# LANGUAGE ScopedTypeVariables #-}
1011{-# LANGUAGE RankNTypes #-}
1112
13+{-# OPTIONS_GHC -Wno-orphans #-}
1214#ifdef USE_PRELUDE
1315{-# OPTIONS_GHC -Wno-deprecations #-}
1416#endif
1517
1618module Stream.Generate (benchmarks) where
1719
18−import Data.Functor.Identity (Identity)
20+import Control.DeepSeq (NFData(..))
21+import Data.Functor.Identity (Identity(..))
1922
2023import qualified GHC.Exts as GHC
2124import qualified Stream.Common as Common
25+import qualified Streamly.Internal.Data.Fold as Fold
2226#ifdef USE_PRELUDE
2327import Streamly.Benchmark.Prelude (sourceFromFoldableM, absTimes)
2428import qualified Streamly.Prelude as S
@@ -224,6 +228,10 @@ o_1_space_generation value =
224228 ]
225229 ]
226230
231+instance NFData a => NFData (Stream Identity a) where
232+ {-# INLINE rnf #-}
233+ rnf xs = runIdentity $ Stream.fold (Fold.foldl' (\_ x -> rnf x) ()) xs
234+
227235o_n_heap_generation :: Int -> [Benchmark]
228236o_n_heap_generation value =
229237 [ bgroup "buffered"
benchmark/Streamly/Benchmark/Data/Stream/Lift.hs+6−7
@@ -17,7 +17,7 @@ module Stream.Lift (benchmarks) where
1717
1818import Control.DeepSeq (NFData(..))
1919import Control.Monad.Trans.Class (lift)
20−import Control.Monad.State.Strict (StateT, get, put, MonadState)
20+import Control.Monad.State.Strict (StateT, get, put)
2121import Data.Functor.Identity (Identity)
2222import Stream.Common
2323 (benchIO, sourceUnfoldr, sourceUnfoldrM, benchIOSrc, drain)
@@ -107,15 +107,14 @@ iterateStateT n = do
107107
108108{-# INLINE iterateState #-}
109109{-# SPECIALIZE iterateState :: Int -> Stream (StateT Int IO) Int #-}
110−iterateState ::
111− MonadState Int m
112− => Int
113− -> Stream m Int
110+iterateState :: Monad m =>
111+ Int
112+ -> Stream (StateT Int m) Int
114113iterateState n = do
115− x <- get
114+ x <- Stream.fromEffect get
116115 if x > n
117116 then do
118− put (x - 1)
117+ Stream.fromEffect $ put (x - 1)
119118 iterateState n
120119 else return x
121120
benchmark/Streamly/Benchmark/Data/Stream/Transform.hs+8−1
@@ -6,9 +6,11 @@
66
77{-# LANGUAGE CPP #-}
88{-# LANGUAGE FlexibleContexts #-}
9+{-# LANGUAGE FlexibleInstances #-}
910{-# LANGUAGE ScopedTypeVariables #-}
1011{-# LANGUAGE RankNTypes #-}
1112
13+{-# OPTIONS_GHC -Wno-orphans #-}
1214#ifdef USE_PRELUDE
1315{-# OPTIONS_GHC -Wno-deprecations #-}
1416#endif
@@ -26,7 +28,7 @@ module Stream.Transform (benchmarks) where
2628
2729import Control.DeepSeq (NFData(..))
2830import Control.Monad.IO.Class (MonadIO(..))
29−import Data.Functor.Identity (Identity)
31+import Data.Functor.Identity (Identity(..))
3032
3133import System.Random (randomRIO)
3234
@@ -34,6 +36,7 @@ import qualified Streamly.Internal.Data.Fold as FL
3436
3537import qualified Prelude
3638import qualified Stream.Common as Common
39+import qualified Streamly.Internal.Data.Fold as Fold
3740import qualified Streamly.Internal.Data.Unfold as Unfold
3841#ifdef USE_PRELUDE
3942import qualified Streamly.Internal.Data.Stream.IsStream as Stream
@@ -84,6 +87,10 @@ benchPureSinkIO
8487benchPureSinkIO value name f =
8588 bench name $ nfIO $ randomRIO (1, 1) >>= f . sourceUnfoldr value
8689
90+instance NFData a => NFData (Stream Identity a) where
91+ {-# INLINE rnf #-}
92+ rnf xs = runIdentity $ Stream.fold (Fold.foldl' (\_ x -> rnf x) ()) xs
93+
8794o_n_space_traversable :: Int -> [Benchmark]
8895o_n_space_traversable value =
8996 -- Buffering operations using heap proportional to number of elements.
benchmark/Streamly/Benchmark/Unicode/Char.hs+9−3
@@ -1,4 +1,4 @@
1−{-# OPTIONS_GHC -Wno-deprecations #-}
1+{-# OPTIONS_GHC -Wno-deprecations -Wno-orphans #-}
22
33--
44-- Module : Streamly.Unicode.Char
@@ -12,7 +12,7 @@
1212-- Imports
1313--------------------------------------------------------------------------------
1414
15−import Control.DeepSeq (NFData)
15+import Control.DeepSeq (NFData(..))
1616import Streamly.Internal.Data.Array (Array)
1717import System.FilePath (dropExtensions, takeFileName)
1818import System.FilePath.Posix ((</>))
@@ -43,7 +43,13 @@ dataDir = "benchmark/Streamly/Benchmark/Unicode/data"
4343dataSetSize :: Int
4444dataSetSize = 1000000
4545
46−makeBench :: (NFData a) => (String, a -> IO ()) -> (String, IO a) -> Benchmark
46+-- Unboxed arrays are fully evaluated.
47+instance NFData (Array a) where
48+ {-# INLINE rnf #-}
49+ rnf _ = ()
50+
51+makeBench ::
52+ (String, Array Char -> IO ()) -> (String, IO (Array Char)) -> Benchmark
4753makeBench (implName, func) (dataName, setup) =
4854 env setup (bench (implName ++ "/" ++ dataName) . nfIO . func)
4955
benchmark/Streamly/Benchmark/Unicode/Utf8.hs+2−2
@@ -49,7 +49,7 @@ main = runWithCLIOpts defaultStreamSize allBenchmarks
4949 allBenchmarks value =
5050 [ bgroup
5151 moduleName
52− [ bench "pack" $ nf pack value
53− , bench "pack + unpack" $ nf packUnpack value
52+ [ bench "pack" $ whnf pack value
53+ , bench "pack + unpack" $ whnf packUnpack value
5454 ]
5555 ]
core/src/Streamly/Internal/Data/Array/Generic.hs−5
@@ -50,7 +50,6 @@ module Streamly.Internal.Data.Array.Generic
5050 )
5151where
5252
53−import Control.DeepSeq (NFData(..))
5453import Control.Monad (when, replicateM)
5554import Control.Monad.IO.Class (liftIO, MonadIO)
5655import GHC.Base (MutableArray#, RealWorld)
@@ -226,10 +225,6 @@ foldl' f z arr = unsafePerformIO $ D.foldl' f z $ readStreamD arr
226225foldr :: (a -> b -> b) -> b -> Array a -> b
227226foldr f z arr = unsafePerformIO $ D.foldr f z $ readStreamD arr
228227
229−instance NFData a => NFData (Array a) where
230− {-# INLINE rnf #-}
231− rnf = foldl' (\_ x -> rnf x) ()
232−
233228{-# INLINE fold #-}
234229fold :: MonadIO m => Fold m a b -> Array a -> m b
235230fold f arr = D.fold f (readStreamD arr)
core/src/Streamly/Internal/Data/Array/Mut/Type.hs−11
@@ -214,7 +214,6 @@ where
214214#include "ArrayMacros.h"
215215#include "MachDeps.h"
216216
217−import Control.DeepSeq (NFData(..), NFData1(..))
218217import Control.Monad (when, void)
219218import Control.Monad.IO.Class (MonadIO(..))
220219import Data.Bits (shiftR, (.|.), (.&.))
@@ -2278,16 +2277,6 @@ cmp arr1 arr2 =
22782277-- NFData
22792278-------------------------------------------------------------------------------
22802279
2281−-- This is an Unbox array, we cannot have unevaluated data in it so this is
2282−-- just a no op.
2283−instance NFData (Array a) where
2284− {-# INLINE rnf #-}
2285− rnf Array {} = ()
2286−
2287−instance NFData1 Array where
2288− {-# INLINE liftRnf #-}
2289− liftRnf _ Array{} = ()
2290−
22912280-- | Strip elements which match with predicate from both ends.
22922281--
22932282-- /Pre-release/
core/src/Streamly/Internal/Data/Array/Type.hs−10
@@ -78,7 +78,6 @@ where
7878#include "inline.hs"
7979
8080import Control.Exception (assert)
81−import Control.DeepSeq (NFData(..), NFData1(..))
8281import Control.Monad (replicateM)
8382import Control.Monad.IO.Class (MonadIO(..))
8483import Data.Functor.Identity (Identity(..))
@@ -515,15 +514,6 @@ instance (Unbox a, Eq a) => Eq (Array a) where
515514 arr1 == arr2 =
516515 (==) EQ $ unsafeInlineIO $! unsafeThaw arr1 `MA.cmp` unsafeThaw arr2
517516
518−-- Since this is an Unbox array we cannot have unevaluated data in it so
519−-- this is just a no op.
520−instance NFData (Array a) where
521− {-# INLINE rnf #-}
522− rnf Array {} = ()
523−
524−instance NFData1 Array where
525− liftRnf _ Array{} = ()
526−
527517instance (Unbox a, Ord a) => Ord (Array a) where
528518 {-# INLINE compare #-}
529519 compare arr1 arr2 = runIdentity $
core/src/Streamly/Internal/Data/List.hs+2−4
@@ -62,8 +62,6 @@ module Streamly.Internal.Data.List
6262where
6363
6464import Control.Arrow (second)
65−import Control.DeepSeq (NFData(..))
66−import Control.DeepSeq (NFData1(..))
6765import Data.Functor.Identity (Identity, runIdentity)
6866import GHC.Exts (IsList(..), IsString(..))
6967import Streamly.Internal.Data.Stream.Type (Stream)
@@ -89,7 +87,7 @@ import qualified Streamly.Internal.Data.Stream.Type as Stream
8987-- /Pre-release/
9088newtype List a = List { toStream :: Stream Identity a }
9189 deriving
92− ( Show, Read, Eq, Ord, NFData , NFData1
90+ ( Show, Read, Eq, Ord
9391 , Semigroup, Monoid, Functor, Foldable
9492 , Applicative, Traversable, Monad, IsList)
9593
@@ -144,7 +142,7 @@ pattern Cons x xs <-
144142-- @since 0.6.0
145143newtype ZipList a = ZipList { toZipStream :: ZipStream Identity a }
146144 deriving
147− ( Show, Read, Eq, Ord, NFData , NFData1
145+ ( Show, Read, Eq, Ord
148146 , Semigroup, Monoid, Functor, Foldable
149147 , Applicative, Traversable, IsList
150148 )
core/src/Streamly/Internal/Data/Parser/ParserD/Type.hs−19
@@ -211,8 +211,6 @@ import Control.Applicative (Alternative(..), liftA2)
211211import Control.Exception (Exception(..))
212212import Control.Monad (MonadPlus(..), (>=>))
213213import Control.Monad.IO.Class (MonadIO, liftIO)
214−import Control.Monad.Reader.Class (MonadReader, ask, local)
215−import Control.Monad.State.Class (MonadState, get, put)
216214import Data.Bifunctor (Bifunctor(..))
217215import Fusion.Plugin.Types (Fuse(..))
218216import Streamly.Internal.Data.Fold.Type (Fold(..), toList)
@@ -1481,23 +1479,6 @@ instance Monad m => MonadPlus (Parser a m) where
14811479 {-# INLINE mplus #-}
14821480 mplus = alt
14831481
1484−instance (Monad m, MonadReader r m) => MonadReader r (Parser a m) where
1485− {-# INLINE ask #-}
1486− ask = fromEffect ask
1487−
1488− {-# INLINE local #-}
1489− local f (Parser step init' extract) =
1490− Parser ((local f .) . step)
1491− (local f init')
1492− (local f . extract)
1493−
1494−instance (Monad m, MonadState s m) => MonadState s (Parser a m) where
1495− {-# INLINE get #-}
1496− get = fromEffect get
1497−
1498− {-# INLINE put #-}
1499− put = fromEffect . put
1500−
15011482instance (Monad m, MonadIO m) => MonadIO (Parser a m) where
15021483 {-# INLINE liftIO #-}
15031484 liftIO = fromEffect . liftIO
core/src/Streamly/Internal/Data/Parser/ParserK/Type.hs+8−20
@@ -32,10 +32,8 @@ where
3232
3333import Control.Applicative (Alternative(..), liftA2)
3434import Control.Monad (MonadPlus(..), ap)
35−import Control.Monad.Catch (MonadCatch, MonadThrow(..))
3635import Control.Monad.IO.Class (MonadIO, liftIO)
37−import Control.Monad.Reader.Class (MonadReader, ask, local)
38−import Control.Monad.State.Class (MonadState, get, put)
36+-- import Control.Monad.Trans.Class (MonadTrans(lift))
3937import qualified Control.Monad.Fail as Fail
4038
4139-- | The intermediate result of running a parser step. The parser driver may
@@ -248,23 +246,7 @@ instance Monad m => Fail.MonadFail (Parser a m) where
248246 {-# INLINE fail #-}
249247 fail = die
250248
251−instance (MonadThrow m, MonadReader r m, MonadCatch m) =>
252− MonadReader r (Parser a m) where
253−
254− {-# INLINE ask #-}
255− ask = fromEffect ask
256−
257− {-# INLINE local #-}
258− local f p = MkParser $ \n st k -> local f $ runParser p n st k
259−
260−instance (MonadThrow m, MonadState s m) => MonadState s (Parser a m) where
261− {-# INLINE get #-}
262− get = fromEffect get
263−
264− {-# INLINE put #-}
265− put = fromEffect . put
266−
267−instance (MonadThrow m, MonadIO m) => MonadIO (Parser a m) where
249+instance MonadIO m => MonadIO (Parser a m) where
268250 {-# INLINE liftIO #-}
269251 liftIO = fromEffect . liftIO
270252
@@ -324,3 +306,9 @@ instance Monad m => MonadPlus (Parser a m) where
324306
325307 {-# INLINE mplus #-}
326308 mplus = (<|>)
309+
310+{-
311+instance MonadTrans (Parser a) where
312+ {-# INLINE lift #-}
313+ lift = fromEffect
314+-}
core/src/Streamly/Internal/Data/Stream/StreamK.hs−1
@@ -171,7 +171,6 @@ module Streamly.Internal.Data.Stream.StreamK
171171
172172 -- * Utilities
173173 , consM
174− , withLocal
175174 , mfix
176175 )
177176where
core/src/Streamly/Internal/Data/Stream/StreamK/Type.hs+2−16
@@ -99,20 +99,18 @@ module Streamly.Internal.Data.Stream.StreamK.Type
9999 , interleaveMin
100100
101101 -- * Reader
102− , withLocal
103102 , evalStateT
104103 , liftInner
105104 )
106105where
107106
108107import Control.Monad (ap, (>=>))
109−import Control.Monad.Reader.Class (MonadReader(..))
110−import Control.Monad.State.Strict (StateT)
111108import Control.Monad.Trans.Class (MonadTrans(lift))
109+import Control.Monad.Trans.State.Strict (StateT)
112110import Data.Function (fix)
113111import Streamly.Internal.Data.SVar.Type (State, adaptState, defState)
114112
115−import qualified Control.Monad.State.Strict as State
113+import qualified Control.Monad.Trans.State.Strict as State
116114import qualified Prelude
117115
118116import Prelude hiding
@@ -1187,18 +1185,6 @@ interleaveMin m1 m2 = mkStream $ \st yld _ stp -> do
11871185 yieldk a r = yld a (interleaveMin m2 r)
11881186 foldStream st yieldk single stop m1
11891187
1190−------------------------------------------------------------------------------
1191−-- MonadReader
1192−------------------------------------------------------------------------------
1193−
1194−{-# INLINABLE withLocal #-}
1195−withLocal :: MonadReader r m => (r -> r) -> Stream m a -> Stream m a
1196−withLocal f m =
1197− mkStream $ \st yld sng stp ->
1198− let single = local f . sng
1199− yieldk a r = local f $ yld a (withLocal f r)
1200− in foldStream st yieldk single (local f stp) m
1201−
12021188-------------------------------------------------------------------------------
12031189-- Generation
12041190-------------------------------------------------------------------------------
core/src/Streamly/Internal/Data/Stream/Transform.hs+1
@@ -169,6 +169,7 @@ import Prelude hiding
169169--
170170-- $setup
171171-- >>> :m
172+-- >>> :set -package mtl
172173-- >>> import Control.Concurrent (threadDelay)
173174-- >>> import Control.Monad.IO.Class (MonadIO (liftIO))
174175-- >>> import Control.Monad.Trans (lift)
core/src/Streamly/Internal/Data/Stream/Type.hs−30
@@ -41,11 +41,8 @@ where
4141#include "inline.hs"
4242
4343import Control.Applicative (liftA2)
44−import Control.DeepSeq (NFData(..), NFData1(..))
4544import Control.Monad.Catch (MonadThrow, throwM)
4645import Control.Monad.IO.Class (MonadIO(..))
47−import Control.Monad.Reader.Class (MonadReader(..))
48−import Control.Monad.State.Class (MonadState(..))
4946import Control.Monad.Trans.Class (MonadTrans(lift))
5047import Data.Foldable (Foldable(foldl'), fold)
5148import Data.Functor.Identity (Identity(..), runIdentity)
@@ -241,33 +238,6 @@ instance (MonadIO m) => MonadIO (Stream m) where
241238instance (MonadThrow m) => MonadThrow (Stream m) where
242239 throwM = lift . throwM
243240
244−instance (MonadReader r m) => MonadReader r (Stream m) where
245− ask = lift ask
246−
247− local f (Stream m) = Stream $ K.withLocal f m
248−
249−instance (MonadState s m) => MonadState s (Stream m) where
250− {-# INLINE get #-}
251− get = lift get
252−
253− {-# INLINE put #-}
254− put x = lift (put x)
255−
256− {-# INLINE state #-}
257− state k = lift (state k)
258−
259−------------------------------------------------------------------------------
260−-- NFData
261−------------------------------------------------------------------------------
262−
263−instance NFData a => NFData (Stream Identity a) where
264− {-# INLINE rnf #-}
265− rnf (Stream xs) = runIdentity $ P.foldl' (\_ x -> rnf x) () xs
266−
267−instance NFData1 (Stream Identity) where
268− {-# INLINE liftRnf #-}
269− liftRnf f (Stream xs) = runIdentity $ P.foldl' (\_ x -> f x) () xs
270−
271241------------------------------------------------------------------------------
272242-- Lists
273243------------------------------------------------------------------------------
core/src/Streamly/Internal/Data/Stream/Zip.hs−3
@@ -24,7 +24,6 @@ module Streamly.Internal.Data.Stream.Zip
2424 )
2525where
2626
27−import Control.DeepSeq (NFData(..), NFData1(..))
2827import Data.Functor.Identity (Identity(..))
2928import GHC.Exts (IsList(..), IsString(..))
3029import Streamly.Internal.Data.Stream.Type (Stream)
@@ -62,8 +61,6 @@ import qualified Streamly.Internal.Data.Stream as Stream
6261newtype ZipStream m a = ZipStream {getZipStream :: Stream m a}
6362 deriving (Functor, Semigroup, Monoid)
6463
65−deriving instance NFData a => NFData (ZipStream Identity a)
66−deriving instance NFData1 (ZipStream Identity)
6764deriving instance IsList (ZipStream Identity a)
6865deriving instance (a ~ Char) => IsString (ZipStream Identity a)
6966deriving instance Eq a => Eq (ZipStream Identity a)
core/streamly-core.cabal−3
@@ -237,7 +237,6 @@ library
237237 , Streamly.Internal.Control.Exception
238238 , Streamly.Internal.Control.Monad
239239 , Streamly.Internal.Control.ForkIO
240− , Streamly.Internal.Data.Cont
241240 , Streamly.Internal.System.IO
242241
243242 -- streamly-strict-data
@@ -410,10 +409,8 @@ library
410409 -- depending on doctest is a common example) can
411410 -- depend on streamly.
412411 base >= 4.12 && < 4.18
413− , deepseq >= 1.4.4 && < 1.5
414412 , exceptions >= 0.8.0 && < 0.11
415413 , ghc-prim >= 0.5.3 && < 0.10
416− , mtl >= 2.2.2 && < 2.4
417414 , transformers >= 0.5.5 && < 0.7
418415
419416 -- streamly-unicode-core
hie.yaml+2
@@ -70,6 +70,8 @@ cradle:
7070 component: "bench:Unicode.Char"
7171 - path: "./benchmark/Streamly/Benchmark/Unicode/Stream.hs"
7272 component: "bench:Unicode.Stream"
73+ - path: "./benchmark/Streamly/Benchmark/Unicode/Utf8.hs"
74+ component: "bench:Unicode.Utf8"
7375 - path: "./benchmark/lib/"
7476 component: "lib:streamly-benchmarks"
7577 - path: "./benchmark/Streamly/Benchmark/Data/Fold/Window.hs"
src/Streamly/Data/Array/Foreign.hs+13
@@ -1,3 +1,4 @@
1+{-# OPTIONS_GHC -Wno-orphans #-}
12#include "inline.hs"
23
34-- |
@@ -93,6 +94,7 @@ module Streamly.Data.Array.Foreign
9394 )
9495where
9596
97+import Control.DeepSeq (NFData(..), NFData1(..))
9698import Streamly.Internal.Data.Array as A
9799
98100-- $setup
@@ -104,3 +106,14 @@ import Streamly.Internal.Data.Array as A
104106-- >>> import Streamly.Data.Array (Array)
105107-- >>> import qualified Streamly.Internal.Data.Stream.IsStream as Stream
106108-- >>> import qualified Streamly.Data.Array as Array
109+
110+-- Orphan instances for backward compatibility
111+-- Since this is an Unbox array we cannot have unevaluated data in it so
112+-- this is just a no op.
113+
114+instance NFData (Array a) where
115+ {-# INLINE rnf #-}
116+ rnf _ = ()
117+
118+instance NFData1 Array where
119+ liftRnf _ _ = ()
core/src/Streamly/Internal/Data/Cont.hs → src/Streamly/Internal/Data/Cont.hsrenamed

The content is the same.

src/Streamly/Internal/Data/Stream/Ahead.hs+9−1
@@ -52,7 +52,7 @@ import Streamly.Internal.Data.Stream.StreamK.Type (Stream)
5252
5353import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
5454 (foldStreamShared, cons, mkStream, foldStream, fromEffect
55− , nil, concatMapWith, fromPure, bindWith, withLocal)
55+ , nil, concatMapWith, fromPure, bindWith)
5656import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
5757 (mapM, fromStreamK, toStreamK)
5858import qualified Streamly.Internal.Data.Stream as Stream (toStreamK)
@@ -74,6 +74,14 @@ import Prelude hiding (map)
7474-- return n -- IO Int
7575-- :}
7676
77+{-# INLINABLE withLocal #-}
78+withLocal :: MonadReader r m => (r -> r) -> Stream m a -> Stream m a
79+withLocal f m =
80+ K.mkStream $ \st yld sng stp ->
81+ let single = local f . sng
82+ yieldk a r = local f $ yld a (withLocal f r)
83+ in K.foldStream st yieldk single (local f stp) m
84+
7785-------------------------------------------------------------------------------
7886-- Ahead
7987-------------------------------------------------------------------------------
src/Streamly/Internal/Data/Stream/Async.hs+9−1
@@ -63,7 +63,7 @@ import Streamly.Internal.Data.Stream.SVar.Generate (fromSVar, fromSVarD)
6363
6464import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
6565 (foldStreamShared, mkStream, foldStream, fromEffect
66− , nil, concatMapWith, fromPure, bindWith, withLocal)
66+ , nil, concatMapWith, fromPure, bindWith)
6767import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
6868 (Stream(..), Step(..), mapM, toStreamK, fromStreamK)
6969import qualified Streamly.Internal.Data.Stream as Stream (toStreamK)
@@ -84,6 +84,14 @@ import Streamly.Internal.Data.SVar
8484-- :}
8585--
8686
87+{-# INLINABLE withLocal #-}
88+withLocal :: MonadReader r m => (r -> r) -> Stream m a -> Stream m a
89+withLocal f m =
90+ K.mkStream $ \st yld sng stp ->
91+ let single = local f . sng
92+ yieldk a r = local f $ yld a (withLocal f r)
93+ in K.foldStream st yieldk single (local f stp) m
94+
8795-------------------------------------------------------------------------------
8896-- Async
8997-------------------------------------------------------------------------------
src/Streamly/Internal/Data/Stream/Instances.hs+1−1
@@ -33,7 +33,7 @@ instance (MonadError e m CONSTRAINT) => MonadError e (STREAM m) where { \
3333 \
3434instance (MonadReader r m CONSTRAINT) => MonadReader r (STREAM m) where { \
3535 ask = lift ask; \
36− local f (STREAM m) = STREAM $ K.withLocal f m }; \
36+ local f (STREAM m) = STREAM $ withLocal f m }; \
3737 \
3838instance (MonadState s m CONSTRAINT) => MonadState s (STREAM m) where { \
3939 {-# INLINE get #-}; \
src/Streamly/Internal/Data/Stream/IsStream.hs+9−1
@@ -1,4 +1,5 @@
1−{-# OPTIONS_GHC -Wno-deprecations #-}
1+{-# OPTIONS_GHC -Wno-deprecations -Wno-orphans #-}
2+{-# LANGUAGE StandaloneDeriving #-}
23
34-- |
45-- Module : Streamly.Internal.Data.Stream.IsStream
@@ -25,6 +26,10 @@ module Streamly.Internal.Data.Stream.IsStream {-# DEPRECATED "Please use \"Stre
2526 )
2627where
2728
29+import Control.DeepSeq (NFData(..), NFData1(..))
30+import Data.Functor.Identity (Identity(..))
31+import Streamly.Internal.Data.Stream.Zip (ZipStream(..))
32+
2833import Streamly.Internal.Data.Stream.IsStream.Top
2934import Streamly.Internal.Data.Stream.IsStream.Eliminate
3035import Streamly.Internal.Data.Stream.IsStream.Exception
@@ -36,3 +41,6 @@ import Streamly.Internal.Data.Stream.IsStream.Transform
3641import Streamly.Internal.Data.Stream.IsStream.Type
3742 hiding (cmpBy, drain, eqBy, foldl', fold, toList, toStream
3843 , fromEffect, fromPure, repeat)
44+
45+deriving instance NFData a => NFData (ZipStream Identity a)
46+deriving instance NFData1 (ZipStream Identity)
src/Streamly/Internal/Data/Stream/Parallel.hs+9−1
@@ -71,7 +71,7 @@ import Streamly.Internal.Data.Stream.Type (Stream)
7171import qualified Data.Set as Set
7272import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
7373 (Stream, foldStreamShared, mkStream, foldStream, fromEffect
74− , nil, concatMapWith, fromPure, bindWith, withLocal)
74+ , nil, concatMapWith, fromPure, bindWith)
7575import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
7676 (Stream(..), mapM, toStreamK, fromStreamK)
7777import qualified Streamly.Internal.Data.Stream.SVar.Generate as SVar
@@ -96,6 +96,14 @@ import Prelude hiding (map)
9696-- return n -- IO Int
9797-- :}
9898
99+{-# INLINABLE withLocal #-}
100+withLocal :: MonadReader r m => (r -> r) -> K.Stream m a -> K.Stream m a
101+withLocal f m =
102+ K.mkStream $ \st yld sng stp ->
103+ let single = local f . sng
104+ yieldk a r = local f $ yld a (withLocal f r)
105+ in K.foldStream st yieldk single (local f stp) m
106+
99107-------------------------------------------------------------------------------
100108-- Parallel
101109-------------------------------------------------------------------------------
src/Streamly/Internal/Data/Stream/Serial.hs+44−2
@@ -1,4 +1,5 @@
11{-# LANGUAGE UndecidableInstances #-}
2+{-# OPTIONS_GHC -Wno-orphans #-}
23
34-- |
45-- Module : Streamly.Internal.Data.Stream.Serial
@@ -65,13 +66,15 @@ import Streamly.Internal.BaseCompat ((#.))
6566import Streamly.Internal.Data.Maybe.Strict (Maybe'(..), toMaybe)
6667import Streamly.Data.Stream (Stream)
6768
69+import qualified Streamly.Data.Fold as Fold
6870import qualified Streamly.Data.Stream as Stream
71+import qualified Streamly.Internal.Data.Stream as Stream (toStreamK, fromStreamK)
6972import qualified Streamly.Internal.Data.Stream.Common as P
7073import qualified Streamly.Internal.Data.Stream.StreamD as D
7174 (fromStreamK, toStreamK, mapM)
7275import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
73− (Stream, cons, consM, nil, concatMapWith, fromPure, bindWith
74− , withLocal, interleave, interleaveFst, interleaveMin)
76+ (Stream, mkStream, foldStream, cons, consM, nil, concatMapWith, fromPure
77+ , bindWith, interleave, interleaveFst, interleaveMin)
7578
7679import Prelude hiding (map, mapM, repeat, filter)
7780
@@ -83,6 +86,45 @@ import Prelude hiding (map, mapM, repeat, filter)
8386-- >>> import qualified Streamly.Data.Stream as Stream
8487-- >>> import qualified Streamly.Prelude as IsStream
8588
89+{-# INLINABLE withLocal #-}
90+withLocal :: MonadReader r m => (r -> r) -> K.Stream m a -> K.Stream m a
91+withLocal f m =
92+ K.mkStream $ \st yld sng stp ->
93+ let single = local f . sng
94+ yieldk a r = local f $ yld a (withLocal f r)
95+ in K.foldStream st yieldk single (local f stp) m
96+
97+------------------------------------------------------------------------------
98+-- mtl orphan instances
99+------------------------------------------------------------------------------
100+
101+instance (MonadReader r m) => MonadReader r (Stream m) where
102+ ask = lift ask
103+
104+ local f m = Stream.fromStreamK $ withLocal f (Stream.toStreamK m)
105+
106+instance (MonadState s m) => MonadState s (Stream m) where
107+ {-# INLINE get #-}
108+ get = lift get
109+
110+ {-# INLINE put #-}
111+ put x = lift (put x)
112+
113+ {-# INLINE state #-}
114+ state k = lift (state k)
115+
116+------------------------------------------------------------------------------
117+-- NFData - orphan instances
118+------------------------------------------------------------------------------
119+
120+instance NFData a => NFData (Stream Identity a) where
121+ {-# INLINE rnf #-}
122+ rnf xs = runIdentity $ Stream.fold (Fold.foldl' (\_ x -> rnf x) ()) xs
123+
124+instance NFData1 (Stream Identity) where
125+ {-# INLINE liftRnf #-}
126+ liftRnf f xs = runIdentity $ Stream.fold (Fold.foldl' (\_ x -> f x) ()) xs
127+
86128------------------------------------------------------------------------------
87129-- SerialT
88130------------------------------------------------------------------------------
src/Streamly/Internal/Unicode/Utf8.hs+1−4
@@ -27,7 +27,6 @@ where
2727-- Imports
2828--------------------------------------------------------------------------------
2929
30−import Control.DeepSeq (NFData)
3130import Data.Word (Word8)
3231import Streamly.Data.Array (Array)
3332import System.IO.Unsafe (unsafePerformIO)
@@ -45,9 +44,7 @@ import qualified Streamly.Internal.Unicode.Stream as Unicode
4544--------------------------------------------------------------------------------
4645
4746-- | A space efficient, packed, unboxed Unicode container.
48−newtype Utf8 =
49− Utf8 (Array Word8)
50− deriving (NFData)
47+newtype Utf8 = Utf8 (Array Word8)
5148
5249--------------------------------------------------------------------------------
5350-- Functions
streamly.cabal+1
@@ -342,6 +342,7 @@ library
342342
343343 Streamly.Internal.Data.IsMap
344344 , Streamly.Internal.Data.IsMap.HashMap
345+ , Streamly.Internal.Data.Cont
345346
346347 -- streamly-concurrent
347348 , Streamly.Internal.Control.Concurrent