@@ -10,6 +10,8 @@
10{-# LANGUAGE RankNTypes #-}
10{-# LANGUAGE RankNTypes #-}
11{-# LANGUAGE UndecidableInstances #-} -- XXX
11{-# LANGUAGE UndecidableInstances #-} -- XXX
13+#include "../inline.hs"
14-- Module : Streamly.Streams.StreamK.Type
16-- Module : Streamly.Streams.StreamK.Type
15-- Copyright : (c) 2017 Harendra Kumar
17-- Copyright : (c) 2017 Harendra Kumar
@@ -182,10 +184,13 @@ adapt = fromStream . toStream
182-- Currently we always use "SVar Stream" and therefore a different State type
184-- Currently we always use "SVar Stream" and therefore a different State type
183-- parameterized by that stream.
185-- parameterized by that stream.
187+-- XXX Since t is coercible we should be able to coerce k
188+-- mkStream k = fromStream $ MkStream $ coerce k
185-- | Build a stream from an 'SVar', a stop continuation, a singleton stream
190-- | Build a stream from an 'SVar', a stop continuation, a singleton stream
186-- continuation and a yield continuation.
191-- continuation and a yield continuation.
187−{-# INLINE mkStream #-}
192+{-# INLINE_EARLY mkStream #-}
188−mkStream:: IsStream t
193+mkStream :: IsStream t
189 => (forall r. State Stream m a
194 => (forall r. State Stream m a
190 -> (a -> t m a -> m r)
195 -> (a -> t m a -> m r)
191 -> (a -> m r)
196 -> (a -> m r)
@@ -196,6 +201,26 @@ mkStream k = fromStream $ MkStream $ \st yld sng stp ->
196 let yieldk a r = yld a (toStream r)
201 let yieldk a r = yld a (toStream r)
197 in k st yieldk sng stp
202 in k st yieldk sng stp
204+{-# RULES "mkStream from stream" mkStream = mkStreamFromStream #-}
205+mkStreamFromStream :: IsStream t
206+ => (forall r. State Stream m a
207+ -> (a -> Stream m a -> m r)
212+mkStreamFromStream k = fromStream $ MkStream k
214+{-# RULES "mkStream stream" mkStream = mkStreamStream #-}
216+ :: (forall r. State Stream m a
217+ -> (a -> Stream m a -> m r)
222+mkStreamStream = MkStream
199-- | A terminal function that has no continuation to follow.
224-- | A terminal function that has no continuation to follow.
200type StopK m = forall r. m r -> m r
225type StopK m = forall r. m r -> m r
@@ -229,7 +254,7 @@ consK k r = mkStream $ \_ yld _ _ -> k (\x -> yld x r)
229-- | Fold a stream by providing an SVar, a stop continuation, a singleton
254-- | Fold a stream by providing an SVar, a stop continuation, a singleton
230-- continuation and a yield continuation. The stream would share the current
255-- continuation and a yield continuation. The stream would share the current
231-- SVar passed via the State.
256-- SVar passed via the State.
232−{-# INLINE foldStreamShared #-}
257+{-# INLINE_EARLY foldStreamShared #-}
233foldStreamShared
258foldStreamShared
234 :: IsStream t
259 :: IsStream t
235 => State Stream m a
260 => State Stream m a
@@ -243,6 +268,20 @@ foldStreamShared st yld sng stp m =
243 MkStream k = toStream m
268 MkStream k = toStream m
244 in k st yieldk sng stp
269 in k st yieldk sng stp
271+-- XXX write a similar rule for foldStream as well?
272+{-# RULES "foldStreamShared from stream"
273+ foldStreamShared = foldStreamSharedStream #-}
274+foldStreamSharedStream
276+ -> (a -> Stream m a -> m r)
281+foldStreamSharedStream st yld sng stp m =
282+ let MkStream k = toStream m
246-- | Fold a stream by providing a State, stop continuation, a singleton
285-- | Fold a stream by providing a State, stop continuation, a singleton
247-- continuation and a yield continuation. The stream will not use the SVar
286-- continuation and a yield continuation. The stream will not use the SVar
248-- passed via State.
287-- passed via State.