on e01057f help sign in
150 lines 4.6 kB history raw
1{-# OPTIONS_GHC -Wno-deprecations #-}
3-- |
4-- Module : Streamly.Internal.Data.Stream.IsStream.Lift
5-- Copyright : (c) 2019 Composewell Technologies
6-- License : BSD-3-Clause
7-- Maintainer : streamly@composewell.com
8-- Stability : experimental
9-- Portability : GHC
11module Streamly.Internal.Data.Stream.IsStream.Lift {-# DEPRECATED "Please use \"Streamly.Data.Stream.*\" instead." #-}
12 (
13 -- * Generalize Inner Monad
14 hoist
15 , generally
17 -- * Transform Inner Monad
18 , liftInner
19 , usingReaderT
20 , runReaderT
21 , evalStateT
22 , usingStateT
23 , runStateT
24 )
25where
27#include "inline.hs"
29import Control.Monad.Trans.Reader (ReaderT)
30import Control.Monad.Trans.State.Strict (StateT)
31import Control.Monad.Trans.Class (MonadTrans(..))
32import Data.Functor.Identity (Identity (..))
33import Streamly.Internal.Data.Stream.IsStream.Type
34 (IsStream(..), fromStreamD, toStreamD)
35import Streamly.Internal.Data.Stream.Serial (SerialT)
37import qualified Streamly.Internal.Data.Stream.StreamD as D
38 (morphInner, liftInner, runReaderT, evalStateT, runStateT)
40------------------------------------------------------------------------------
41-- Generalize the underlying monad
42------------------------------------------------------------------------------
44-- | Transform the inner monad of a stream using a natural transformation.
45--
46-- / Internal/
47--
48{-# INLINE hoist #-}
49hoist :: (Monad m, Monad n)
50 => (forall x. m x -> n x) -> SerialT m a -> SerialT n a
51hoist f xs = fromStreamD $ D.morphInner f (toStreamD xs)
53-- | Generalize the inner monad of the stream from 'Identity' to any monad.
54--
55-- / Internal/
56--
57{-# INLINE generally #-}
58generally :: (IsStream t, Monad m) => t Identity a -> t m a
59generally xs = fromStreamD $ D.morphInner (return . runIdentity) (toStreamD xs)
61------------------------------------------------------------------------------
62-- Add and remove a monad transformer
63------------------------------------------------------------------------------
65-- | Lift the inner monad @m@ of a stream @t m a@ to @tr m@ using the monad
66-- transformer @tr@.
67--
68-- @since 0.8.0
69--
70{-# INLINE liftInner #-}
71liftInner :: (Monad m, IsStream t, MonadTrans tr, Monad (tr m))
72 => t m a -> t (tr m) a
73liftInner xs = fromStreamD $ D.liftInner (toStreamD xs)
75------------------------------------------------------------------------------
76-- Sharing read only state in a stream
77------------------------------------------------------------------------------
79-- | Evaluate the inner monad of a stream as 'ReaderT'.
80--
81-- @since 0.8.0
82--
83{-# INLINE runReaderT #-}
84runReaderT :: (IsStream t, Monad m) => m s -> t (ReaderT s m) a -> t m a
85runReaderT s xs = fromStreamD $ D.runReaderT s (toStreamD xs)
87-- | Run a stream transformation using a given environment.
88--
89-- See also: 'Serial.map'
90--
91-- / Internal/
92--
93{-# INLINE usingReaderT #-}
94usingReaderT
95 :: (Monad m, IsStream t)
96 => m r
97 -> (t (ReaderT r m) a -> t (ReaderT r m) a)
98 -> t m a
99 -> t m a
100usingReaderT r f xs = runReaderT r $ f $ liftInner xs
102------------------------------------------------------------------------------
103-- Sharing read write state in a stream
104------------------------------------------------------------------------------
106-- | Evaluate the inner monad of a stream as 'StateT'.
107--
108-- This is supported only for 'SerialT' as concurrent state updation may not be
109-- safe.
110--
111-- > evalStateT s = Stream.map snd . Stream.runStateT s
112--
113-- / Internal/
114--
115{-# INLINE evalStateT #-}
116evalStateT :: Monad m => m s -> SerialT (StateT s m) a -> SerialT m a
117-- evalStateT s = fmap snd . runStateT s
118evalStateT s xs = fromStreamD $ D.evalStateT s (toStreamD xs)
120-- | Run a stateful (StateT) stream transformation using a given state.
121--
122-- This is supported only for 'SerialT' as concurrent state updation may not be
123-- safe.
124--
125-- > usingStateT s f = evalStateT s . f . liftInner
126--
127-- See also: 'scanl''
128--
129-- / Internal/
130--
131{-# INLINE usingStateT #-}
132usingStateT
133 :: Monad m
134 => m s
135 -> (SerialT (StateT s m) a -> SerialT (StateT s m) a)
136 -> SerialT m a
137 -> SerialT m a
138usingStateT s f = evalStateT s . f . liftInner
140-- | Evaluate the inner monad of a stream as 'StateT' and emit the resulting
141-- state and value pair after each step.
142--
143-- This is supported only for 'SerialT' as concurrent state updation may not be
144-- safe.
145--
146-- @since 0.8.0
147--
148{-# INLINE runStateT #-}
149runStateT :: Monad m => m s -> SerialT (StateT s m) a -> SerialT m (s, a)
150runStateT s xs = fromStreamD $ D.runStateT s (toStreamD xs)