on master help sign in
Rename hoist/generally to morphInner/generalizeInner
ffcb268Harendra Kumar
benchmark/Streamly/Benchmark/Data/Stream/Eliminate.hs+2−2
@@ -465,8 +465,8 @@ o_1_space_elimination_folds value =
465
465
466 -- deconstruction
466 -- deconstruction
467 , benchIOSink value "uncons" uncons
467 , benchIOSink value "uncons" uncons
468− , benchHoistSink value "length . generally"
468+ , benchHoistSink value "length . generalizeInner"
469− (S.fold Fold.length . S.generally)
469+ (S.fold Fold.length . S.generalizeInner)
470#ifdef USE_PRELUDE
470#ifdef USE_PRELUDE
471 , benchIOSink value "init" init
471 , benchIOSink value "init" init
472
472
benchmark/Streamly/Benchmark/Data/Stream/Lift.hs+3−2
@@ -76,8 +76,9 @@ o_1_space_hoisting value =
76 [ bgroup "hoisting"
76 [ bgroup "hoisting"
77 [ benchIOSrc "evalState" (evalStateT value)
77 [ benchIOSrc "evalState" (evalStateT value)
78 , benchIOSrc "withState" (withState value)
78 , benchIOSrc "withState" (withState value)
79− , benchHoistSink value "generally"
79+ , benchHoistSink value "generalizeInner"
80− ((\xs -> Stream.fold Fold.length xs :: IO Int) . Stream.generally)
80+ ((\xs -> Stream.fold Fold.length xs :: IO Int)
81+ . Stream.generalizeInner)
81 ]
82 ]
82 ]
83 ]
83
84
core/src/Streamly/Data/Array.hs+6−6
@@ -47,18 +47,18 @@
47--
47--
48-- Array creation folds require 'MonadIO' because they need to sequence effects
48-- Array creation folds require 'MonadIO' because they need to sequence effects
49-- in IO streams. To operate on streams in pure Monads like 'Identity' you can
49-- in IO streams. To operate on streams in pure Monads like 'Identity' you can
50−-- hoist it to IO monad as follows:
50+-- morph it to IO monad as follows:
51--
51--
52−-- The 'MonadIO' based folds can be hoisted to 'Identity' stream folds:
52+-- The 'MonadIO' based folds can be morphed to 'Identity' stream folds:
53--
53--
54−-- >>> purely = Fold.hoist (Identity . unsafePerformIO)
54+-- >>> purely = Fold.morphInner (Identity . unsafePerformIO)
55-- >>> Stream.fold (purely Array.write) $ Stream.fromList [1,2,3::Int]
55-- >>> Stream.fold (purely Array.write) $ Stream.fromList [1,2,3::Int]
56-- Identity fromList [1,2,3]
56-- Identity fromList [1,2,3]
57--
57--
58-- Alternatively, 'Identity' streams can be generalized to IO streams:
58-- Alternatively, 'Identity' streams can be generalized to IO streams:
59--
59--
60-- >>> pure = Stream.fromList [1,2,3] :: Stream Identity Int
60-- >>> pure = Stream.fromList [1,2,3] :: Stream Identity Int
61−-- >>> generally = Stream.hoist (return . runIdentity)
61+-- >>> generally = Stream.morphInner (return . runIdentity)
62-- >>> Stream.fold Array.write (generally pure :: Stream IO Int)
62-- >>> Stream.fold Array.write (generally pure :: Stream IO Int)
63-- fromList [1,2,3]
63-- fromList [1,2,3]
64--
64--
@@ -146,9 +146,9 @@ import Prelude hiding (read)
146-- >>> import System.IO.Unsafe (unsafePerformIO)
146-- >>> import System.IO.Unsafe (unsafePerformIO)
147-- >>> import qualified Streamly.Data.Array as Array
147-- >>> import qualified Streamly.Data.Array as Array
148-- >>> import qualified Streamly.Data.Fold as Fold
148-- >>> import qualified Streamly.Data.Fold as Fold
149−-- >>> import qualified Streamly.Internal.Data.Fold as Fold (hoist)
149+-- >>> import qualified Streamly.Internal.Data.Fold as Fold (morphInner)
150-- >>> import qualified Streamly.Data.Stream as Stream
150-- >>> import qualified Streamly.Data.Stream as Stream
151−-- >>> import qualified Streamly.Internal.Data.Stream as Stream (hoist)
151+-- >>> import qualified Streamly.Internal.Data.Stream as Stream (morphInner)
152
152
153-- | Same as 'reader'
153-- | Same as 'reader'
154--
154--
core/src/Streamly/Data/Fold.hs+3
@@ -298,6 +298,9 @@ module Streamly.Data.Fold
298 -- ** Nesting
298 -- ** Nesting
299 , concatMap
299 , concatMap
300
300
301+ -- * Transforming the Monad
302+ , morphInner
303+
301 -- * Deprecated
304 -- * Deprecated
302 , foldr
305 , foldr
303 , drainBy
306 , drainBy
core/src/Streamly/Data/Stream.hs+2−3
@@ -448,10 +448,9 @@ module Streamly.Data.Stream
448 , bracketIO
448 , bracketIO
449 , bracket3IO
449 , bracket3IO
450
450
451− -- * Lifting Inner Monad
451+ -- * Transforming Inner Monad
452− -- | See also: "Streamly.Internal.Data.Stream.Lift" for
453− -- @Pre-release@ functions.
454
452
453+ , morphInner
455 , liftInner
454 , liftInner
456 , runReaderT
455 , runReaderT
457 , runStateT
456 , runStateT
core/src/Streamly/Internal/Data/Fold.hs+2−2
@@ -172,8 +172,8 @@ module Streamly.Internal.Data.Fold
172 , with
172 , with
173
173
174 -- ** Transforming the Monad
174 -- ** Transforming the Monad
175− , hoist
175+ , morphInner
176− , generally
176+ , generalizeInner
177
177
178 -- ** Mapping on output
178 -- ** Mapping on output
179 , rmapM
179 , rmapM
core/src/Streamly/Internal/Data/Fold/Type.hs+12−11
@@ -410,9 +410,9 @@ module Streamly.Internal.Data.Fold.Type
410 , close
410 , close
411 , isClosed
411 , isClosed
412
412
413− -- * Lifting inner monad
413+ -- * Transforming inner monad
414− , hoist
414+ , morphInner
415− , generally
415+ , generalizeInner
416
416
417 -- * Deprecated
417 -- * Deprecated
418 , foldr
418 , foldr
@@ -440,6 +440,7 @@ import Prelude hiding (concatMap, filter, foldr, map, take)
440-- $setup
440-- $setup
441-- >>> :m
441-- >>> :m
442-- >>> :set -XFlexibleContexts
442-- >>> :set -XFlexibleContexts
443+-- >>> import Data.Functor.Identity (runIdentity)
443-- >>> import Data.Maybe (fromJust, isJust)
444-- >>> import Data.Maybe (fromJust, isJust)
444-- >>> import Data.Monoid (Endo(..))
445-- >>> import Data.Monoid (Endo(..))
445-- >>> import Streamly.Data.Fold (Fold)
446-- >>> import Streamly.Data.Fold (Fold)
@@ -1824,20 +1825,20 @@ refold (Refold step inject extract) f =
1824 Fold step (extractM f >>= inject) extract
1825 Fold step (extractM f >>= inject) extract
1825
1826
1826------------------------------------------------------------------------------
1827------------------------------------------------------------------------------
1827−-- hoist
1828+-- morphInner
1828------------------------------------------------------------------------------
1829------------------------------------------------------------------------------
1829
1830
1830−-- | Change the underlying monad of a fold
1831+-- | Change the underlying monad of a fold. Also known as hoist.
1831--
1832--
1832-- /Pre-release/
1833-- /Pre-release/
1833−hoist :: (forall x. m x -> n x) -> Fold m a b -> Fold n a b
1834+morphInner :: (forall x. m x -> n x) -> Fold m a b -> Fold n a b
1834−hoist f (Fold step initial extract) =
1835+morphInner f (Fold step initial extract) =
1835 Fold (\x a -> f $ step x a) (f initial) (f . extract)
1836 Fold (\x a -> f $ step x a) (f initial) (f . extract)
1836
1837
1837−-- | Adapt a pure fold to any monad
1838+-- | Adapt a pure fold to any monad.
1838--
1839--
1839−-- >>> generally = Fold.hoist (return . runIdentity)
1840+-- >>> generalizeInner = Fold.morphInner (return . runIdentity)
1840--
1841--
1841-- /Pre-release/
1842-- /Pre-release/
1842−generally :: Monad m => Fold Identity a b -> Fold m a b
1843+generalizeInner :: Monad m => Fold Identity a b -> Fold m a b
1843−generally = hoist (return . runIdentity)
1844+generalizeInner = morphInner (return . runIdentity)
core/src/Streamly/Internal/Data/Stream/Lift.hs+18−10
@@ -9,8 +9,8 @@
9module Streamly.Internal.Data.Stream.Lift
9module Streamly.Internal.Data.Stream.Lift
10 (
10 (
11 -- * Generalize Inner Monad
11 -- * Generalize Inner Monad
12− hoist
12+ morphInner
13− , generally
13+ , generalizeInner
14
14
15 -- * Transform Inner Monad
15 -- * Transform Inner Monad
16 , liftInnerWith
16 , liftInnerWith
@@ -28,6 +28,7 @@ import qualified Streamly.Internal.Data.Stream.StreamK as K
28
28
29-- $setup
29-- $setup
30-- >>> :m
30-- >>> :m
31+-- >>> import Data.Functor.Identity (runIdentity)
31-- >>> import Streamly.Internal.Data.Stream as Stream
32-- >>> import Streamly.Internal.Data.Stream as Stream
32
33
33------------------------------------------------------------------------------
34------------------------------------------------------------------------------
@@ -36,23 +37,30 @@ import qualified Streamly.Internal.Data.Stream.StreamK as K
36
37
37-- | Transform the inner monad of a stream using a natural transformation.
38-- | Transform the inner monad of a stream using a natural transformation.
38--
39--
39−-- / Internal/
40+-- Example, generalize the inner monad from Identity to any other:
41+--
42+-- >>> generalizeInner = Stream.morphInner (return . runIdentity)
43+--
44+-- Also known as hoist.
40--
45--
41-- /CPS/
46-- /CPS/
42−{-# INLINE hoist #-}
47+{-# INLINE morphInner #-}
43−hoist :: (Monad m, Monad n)
48+morphInner :: (Monad m, Monad n)
44 => (forall x. m x -> n x) -> Stream m a -> Stream n a
49 => (forall x. m x -> n x) -> Stream m a -> Stream n a
45−hoist f xs = fromStreamK $ K.hoist f (toStreamK xs)
50+morphInner f xs = fromStreamK $ K.hoist f (toStreamK xs)
46
51
47-- | Generalize the inner monad of the stream from 'Identity' to any monad.
52-- | Generalize the inner monad of the stream from 'Identity' to any monad.
48--
53--
49−-- / Internal/
54+-- Definition:
55+--
56+-- >>> generalizeInner = Stream.morphInner (return . runIdentity)
50--
57--
51-- /CPS/
58-- /CPS/
52--
59--
53−{-# INLINE generally #-}
60+{-# INLINE generalizeInner #-}
54−generally :: Monad m => Stream Identity a -> Stream m a
61+generalizeInner :: Monad m => Stream Identity a -> Stream m a
62+generalizeInner = morphInner (return . runIdentity)
55−generally xs = fromStreamK $ K.hoist (return . runIdentity) (toStreamK xs)
63+ -- fromStreamK $ K.hoist (return . runIdentity) (toStreamK xs)
56
64
57------------------------------------------------------------------------------
65------------------------------------------------------------------------------
58-- Add and remove a monad transformer
66-- Add and remove a monad transformer
core/src/Streamly/Internal/Data/Stream/Transformer.hs+4−4
@@ -62,12 +62,12 @@ foldrT f z s = D.foldrT f z (toStreamD s)
62-- Add and remove a monad transformer
62-- Add and remove a monad transformer
63------------------------------------------------------------------------------
63------------------------------------------------------------------------------
64
64
65−-- | Lift the inner monad @m@ of a stream @Stream m a@ to @tr m@ using the monad
65+-- | Lift the inner monad @m@ of @Stream m a@ to @t m@ where @t@ is a monad
66−-- transformer @tr@.
66+-- transformer.
67--
67--
68{-# INLINE liftInner #-}
68{-# INLINE liftInner #-}
69−liftInner :: (Monad m, MonadTrans tr, Monad (tr m))
69+liftInner :: (Monad m, MonadTrans t, Monad (t m))
70− => Stream m a -> Stream (tr m) a
70+ => Stream m a -> Stream (t m) a
71liftInner xs = fromStreamD $ D.liftInner (toStreamD xs)
71liftInner xs = fromStreamD $ D.liftInner (toStreamD xs)
72
72
73------------------------------------------------------------------------------
73------------------------------------------------------------------------------