@@ -78,14 +78,17 @@ module Streamly.Internal.Data.Stream.StreamK.Type
7878 , mapMWith
7979 , mapMSerial
8080 , unShare
81+
82+ , crossApplyWith
83+ , crossApply
84+ , crossApplySnd
85+ , crossApplyFst
86+
8187 , concatMapWith
8288 , concatMap
8389 , bindWith
8490 , concatPairsWith
85− , apWith
86− , apSerial
87− , apSerialDiscardFst
88− , apSerialDiscardSnd
91+
8992 , foldlS
9093 , reverse
9194
@@ -97,20 +100,14 @@ module Streamly.Internal.Data.Stream.StreamK.Type
97100 , interleave
98101 , interleaveFst
99102 , interleaveMin
100−
101− -- * Reader
102− , evalStateT
103− , liftInner
104103 )
105104where
106105
107−import Control.Monad (ap, (>=>))
108−import Control.Monad.Trans.Class (MonadTrans(lift))
109−import Control.Monad.Trans.State.Strict (StateT)
106+-- import Control.Applicative (liftA2)
107+import Control.Monad ((>=>))
110108import Data.Function (fix)
111109import Streamly.Internal.Data.SVar.Type (State, adaptState, defState)
112110
113−import qualified Control.Monad.Trans.State.Strict as State
114111import qualified Prelude
115112
116113import Prelude hiding
@@ -866,14 +863,6 @@ mapMWith cns f = go
866863instance Monad m => Functor (Stream m) where
867864 fmap = map
868865
869−-------------------------------------------------------------------------------
870−-- Transformers
871−-------------------------------------------------------------------------------
872−
873−instance MonadTrans Stream where
874− {-# INLINE lift #-}
875− lift = fromEffect
876−
877866-------------------------------------------------------------------------------
878867-- Nesting
879868-------------------------------------------------------------------------------
@@ -885,13 +874,13 @@ unShare x = mkStream $ \st yld sng stp ->
885874 foldStream st yld sng stp x
886875
887876-- XXX the function stream and value stream can run in parallel
888−{-# INLINE apWith #-}
889−apWith ::
877+{-# INLINE crossApplyWith #-}
878+crossApplyWith ::
890879 (Stream m b -> Stream m b -> Stream m b)
891880 -> Stream m (a -> b)
892881 -> Stream m a
893882 -> Stream m b
894−apWith par fstream stream = go1 fstream
883+crossApplyWith par fstream stream = go1 fstream
895884
896885 where
897886
@@ -908,12 +897,12 @@ apWith par fstream stream = go1 fstream
908897 yieldk a r = yld (f a) (go2 f r)
909898 in foldStream (adaptState st) yieldk single stp m
910899
911−{-# INLINE apSerial #-}
912−apSerial ::
900+{-# INLINE crossApply #-}
901+crossApply ::
913902 Stream m (a -> b)
914903 -> Stream m a
915904 -> Stream m b
916−apSerial fstream stream = go1 fstream
905+crossApply fstream stream = go1 fstream
917906
918907 where
919908
@@ -938,12 +927,12 @@ apSerial fstream stream = go1 fstream
938927 yieldk a r = yld (f a) (go3 f r)
939928 in foldStream (adaptState st) yieldk single stp m
940929
941−{-# INLINE apSerialDiscardFst #-}
942−apSerialDiscardFst ::
930+{-# INLINE crossApplySnd #-}
931+crossApplySnd ::
943932 Stream m a
944933 -> Stream m b
945934 -> Stream m b
946−apSerialDiscardFst fstream stream = go1 fstream
935+crossApplySnd fstream stream = go1 fstream
947936
948937 where
949938
@@ -962,12 +951,12 @@ apSerialDiscardFst fstream stream = go1 fstream
962951 yieldk a r = yld a (go2 r1 r)
963952 in foldStream st yieldk single stop m
964953
965−{-# INLINE apSerialDiscardSnd #-}
966−apSerialDiscardSnd ::
954+{-# INLINE crossApplyFst #-}
955+crossApplyFst ::
967956 Stream m a
968957 -> Stream m b
969958 -> Stream m a
970−apSerialDiscardSnd fstream stream = go1 fstream
959+crossApplyFst fstream stream = go1 fstream
971960
972961 where
973962
@@ -1116,11 +1105,22 @@ concatPairsWith combine f str = go (leafPairs str)
11161105 yieldk a r = yld (a1 `combine` a) $ nonLeafPairs r
11171106 in foldStream (adaptState st) yieldk single stop stream
11181107
1108+{-
11191109instance Monad m => Applicative (Stream m) where
11201110 {-# INLINE pure #-}
11211111 pure = fromPure
1112+
11221113 {-# INLINE (<*>) #-}
1123− (<*>) = ap
1114+ (<*>) = crossApply
1115+
1116+ {-# INLINE liftA2 #-}
1117+ liftA2 f x = (<*>) (fmap f x)
1118+
1119+ {-# INLINE (*>) #-}
1120+ (*>) = crossApplySnd
1121+
1122+ {-# INLINE (<*) #-}
1123+ (<*) = crossApplyFst
11241124
11251125-- NOTE: even though concatMap for StreamD is 3x faster compared to StreamK,
11261126-- the monad instance of StreamD is slower than StreamK after foldr/build
@@ -1128,8 +1128,10 @@ instance Monad m => Applicative (Stream m) where
11281128instance Monad m => Monad (Stream m) where
11291129 {-# INLINE return #-}
11301130 return = pure
1131+
11311132 {-# INLINE (>>=) #-}
11321133 (>>=) = flip concatMap
1134+-}
11331135
11341136{-
11351137-- Like concatMap but generates stream using an unfold function. Similar to
@@ -1386,34 +1388,3 @@ concatMapEffect :: Monad m => (b -> Stream m a) -> m b -> Stream m a
13861388concatMapEffect f action =
13871389 mkStream $ \st yld sng stp ->
13881390 action >>= foldStreamShared st yld sng stp . f
1389−
1390−------------------------------------------------------------------------------
1391−-- Lifting inner monad
1392−------------------------------------------------------------------------------
1393−
1394−{-# INLINE evalStateT #-}
1395−evalStateT :: Monad m => m s -> Stream (StateT s m) a -> Stream m a
1396−evalStateT = go
1397−
1398− where
1399−
1400− -- XXX Do not use stream monad
1401− go st m1 = do
1402− (res, s1) <- lift $ st >>= State.runStateT (uncons m1)
1403− case res of
1404− Just (h, t) -> cons h (go (return s1) t)
1405− Nothing -> nil
1406−
1407−{-# INLINE liftInner #-}
1408−liftInner :: (Monad m, MonadTrans t, Monad (t m)) =>
1409− Stream m a -> Stream (t m) a
1410−liftInner = go
1411−
1412− where
1413−
1414− -- XXX Do not use stream monad
1415− go m1 = do
1416− res <- fromEffect $ lift $ uncons m1
1417− case res of
1418− Just (h, t) -> cons h (go t)
1419− Nothing -> nil