Skip to content

Commit 48d6392

Browse files
Fix Category and Arrow instances for Unfold
1 parent 9c13aea commit 48d6392

1 file changed

Lines changed: 21 additions & 18 deletions

File tree

  • core/src/Streamly/Internal/Data/Unfold

core/src/Streamly/Internal/Data/Unfold/Type.hs

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,13 @@ where
129129
#include "deprecation.h"
130130
#include "inline.hs"
131131

132-
-- import Control.Arrow (Arrow(..))
133-
-- import Control.Category (Category(..))
132+
-- import Control.Arrow (Arrow(arr, (***)))
133+
import Control.Category (Category(id, (.)))
134134
import Data.Void (Void)
135135
import Fusion.Plugin.Types (Fuse(..))
136136
import Streamly.Internal.Data.Stream.Step (Step(..))
137137

138-
import Prelude hiding (map, mapM, concatMap, zipWith, takeWhile)
138+
import Prelude hiding (id, (.), map, mapM, concatMap, zipWith, takeWhile)
139139

140140
#include "DocTestDataUnfold.hs"
141141

@@ -304,7 +304,7 @@ unfoldr step = unfoldrM (pure . step)
304304
--
305305
{-# INLINE_NORMAL lmap #-}
306306
lmap :: (a -> c) -> Unfold m c b -> Unfold m a b
307-
lmap f (Unfold ustep uinject) = Unfold ustep (uinject Prelude.. f)
307+
lmap f (Unfold ustep uinject) = Unfold ustep (uinject . f)
308308

309309
-- | State used by 'lmapM' to defer the monadic input transformation to the
310310
-- first step.
@@ -593,7 +593,7 @@ fromEffect m = Unfold step inject
593593
-- /Pre-release/
594594
{-# INLINE fromPure #-}
595595
fromPure :: Applicative m => b -> Unfold m a b
596-
fromPure = fromEffect Prelude.. pure
596+
fromPure = fromEffect . pure
597597

598598
data TupleState a = TupleBoth a a | TupleOne a | TupleNone
599599

@@ -879,7 +879,7 @@ concatMapM f (Unfold step1 inject1) = Unfold step inject
879879

880880
{-# INLINE concatMap #-}
881881
concatMap :: Monad m => (b -> Unfold m a c) -> Unfold m a b -> Unfold m a c
882-
concatMap f = concatMapM (return Prelude.. f)
882+
concatMap f = concatMapM (return . f)
883883

884884
infixl 1 `bind`
885885

@@ -941,7 +941,7 @@ functionM f = Unfold step Just
941941
--
942942
{-# INLINE function #-}
943943
function :: Applicative m => (a -> b) -> Unfold m a b
944-
function f = functionM $ pure Prelude.. f
944+
function f = functionM $ pure . f
945945

946946
-- | Lift a monadic Maybe returning function into an unfold. The unfold
947947
-- generates a singleton stream.
@@ -961,12 +961,12 @@ functionMaybeM f = Unfold step Just
961961
-- | Identity unfold. The unfold generates a singleton stream having the input
962962
-- as the only element.
963963
--
964-
-- > identity = function Prelude.id
964+
-- > identity = function id
965965
--
966966
-- /Pre-release/
967967
{-# INLINE identity #-}
968968
identity :: Applicative m => Unfold m a a
969-
identity = function Prelude.id
969+
identity = function id
970970

971971
{-# ANN type ConcatState Fuse #-}
972972
data ConcatState s1 s2 = ConcatOuter s1 | ConcatInner s1 s2
@@ -1001,8 +1001,8 @@ unfoldEach (Unfold step2 inject2) (Unfold step1 inject1) = Unfold step inject
10011001
RENAME(many,unfoldEach)
10021002

10031003
{-
1004-
-- XXX There are multiple possible ways to combine the unfolds, "many" appends
1005-
-- them, we could also have other variants of "many" e.g. manyInterleave.
1004+
-- XXX There are multiple possible ways to combine the unfolds, "unfoldEach"
1005+
-- appends them, we could also have other variants e.g. unfoldEachInterleave.
10061006
-- Should we even have a category instance or just use these functions
10071007
-- directly?
10081008
--
@@ -1011,7 +1011,7 @@ instance Monad m => Category (Unfold m) where
10111011
id = identity
10121012
10131013
{-# INLINE (.) #-}
1014-
(.) = many
1014+
(.) = unfoldEach
10151015
-}
10161016

10171017
-------------------------------------------------------------------------------
@@ -1118,24 +1118,27 @@ zipArrowWith f = zipArrowWithM (\a b -> return (f a b))
11181118
-- could zip, merge, append and more. What is the preferred way for Arrow
11191119
-- instance? Should we even have an arrow instance or just use these functions
11201120
-- directly?
1121+
1122+
-- | '***' splits the input tuple between the two unfolds and zips their
1123+
-- outputs (same as @Unfold.zipArrowWith (,)@). The default '&&&' distributes
1124+
-- the same input to both unfolds and zips their outputs (same as
1125+
-- @Unfold.zipWith (,)@).
11211126
--
1122-
-- | '***' is a zip like operation, in fact it is the same as @Unfold.zipWith
1123-
-- (,)@, '&&&' is a tee like operation i.e. distributes the input to both the
1124-
-- unfolds and then zips the output.
1125-
--
1126-
{-# ANN module "HLint: ignore Use zip" #-}
11271127
instance Monad m => Arrow (Unfold m) where
11281128
{-# INLINE arr #-}
11291129
arr = function
11301130
11311131
{-# INLINE (***) #-}
1132-
u1 *** u2 = zipWith (,) (lmap fst u1) (lmap snd u2)
1132+
(***) = zipArrowWith (,)
11331133
-}
11341134

11351135
------------------------------------------------------------------------------
11361136
-- Interleaving
11371137
------------------------------------------------------------------------------
11381138

1139+
-- XXX If we have interleave, we can have append as well and all binary
1140+
-- operations that streams have.
1141+
11391142
-- We can possibly have an "interleave" operation to interleave the streams
11401143
-- from two seeds:
11411144
--

0 commit comments

Comments
 (0)