{-# OPTIONS_GHC -Wno-name-shadowing #-}
{-# OPTIONS_HADDOCK hide #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE LinearTypes #-}
{-# LANGUAGE QualifiedDo #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE RebindableSyntax #-}
{-# LANGUAGE ScopedTypeVariables #-}

-- | This module provides all functions which produce a
-- 'Stream (Of a) m r' from some given non-stream inputs.
module Streaming.Internal.Produce
  ( -- * Constructing Finite 'Stream's
    yield
  , each'
  , unfoldr
  , fromHandle
  , readFile
  , replicate
  , replicateM
  , replicateZip
  , untilRight
  -- * Working with infinite 'Stream's
  , stdinLnN
  , stdinLnUntil
  , stdinLnUntilM
  , stdinLnZip
  , readLnN
  , readLnUntil
  , readLnUntilM
  , readLnZip
  , iterateN
  , iterateZip
  , iterateMN
  , iterateMZip
  , cycleN
  , cycleZip
  , enumFromN
  , enumFromZip
  , enumFromThenN
  , enumFromThenZip
  ) where

import Streaming.Internal.Type
import Streaming.Internal.Process
import Streaming.Internal.Consume (effects)
import Prelude.Linear (($), (&))
import Prelude (Either(..), Read, Bool(..), FilePath, Enum, otherwise,
               Num(..), Int, otherwise, Eq(..), Ord(..), fromEnum, toEnum)
import qualified Prelude
import qualified Control.Functor.Linear as Control
import Data.Unrestricted.Linear
import System.IO.Linear
import System.IO.Resource
import qualified System.IO as System
import Data.Text (Text)
import qualified Data.Text as Text
import GHC.Stack


-- # The Finite Stream Constructors
-------------------------------------------------------------------------------

{-| A singleton stream

@
\>\>\> stdoutLn $ yield "hello"
hello
@

@
\>\>\> S.sum $ do {yield 1; yield 2; yield 3}
6 :> ()
@
-}
yield :: Control.Monad m => a -> Stream (Of a) m ()
yield :: forall (m :: * -> *) a. Monad m => a -> Stream (Of a) m ()
yield a
x = Of a (Stream (Of a) m ()) %1 -> Stream (Of a) m ()
forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step (Of a (Stream (Of a) m ()) %1 -> Stream (Of a) m ())
%1 -> Of a (Stream (Of a) m ()) %1 -> Stream (Of a) m ()
forall a b. (a %1 -> b) %1 -> a %1 -> b
$ a
x a -> Stream (Of a) m () -> Of a (Stream (Of a) m ())
forall a b. a -> b -> Of a b
:> () -> Stream (Of a) m ()
forall r (f :: * -> *) (m :: * -> *). r -> Stream f m r
Return ()
{-# INLINE yield #-}

{- | Stream the elements of a pure, foldable container.

@
\>\>\> S.print $ each' [1..3]
1
2
3
@
-}
each' :: Control.Monad m => [a] -> Stream (Of a) m ()
each' :: forall (m :: * -> *) a. Monad m => [a] -> Stream (Of a) m ()
each' [a]
xs = (a -> Stream (Of a) m () -> Stream (Of a) m ())
-> Stream (Of a) m () -> [a] -> Stream (Of a) m ()
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
Prelude.foldr (\a
a Stream (Of a) m ()
stream -> Of a (Stream (Of a) m ()) %1 -> Stream (Of a) m ()
forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step (Of a (Stream (Of a) m ()) %1 -> Stream (Of a) m ())
%1 -> Of a (Stream (Of a) m ()) %1 -> Stream (Of a) m ()
forall a b. (a %1 -> b) %1 -> a %1 -> b
$ a
a a -> Stream (Of a) m () -> Of a (Stream (Of a) m ())
forall a b. a -> b -> Of a b
:> Stream (Of a) m ()
stream) (() -> Stream (Of a) m ()
forall r (f :: * -> *) (m :: * -> *). r -> Stream f m r
Return ()) [a]
xs
{-# INLINABLE each' #-}

{-| Build a @Stream@ by unfolding steps starting from a seed. In particular note
    that @S.unfoldr S.next = id@.

-}
unfoldr :: Control.Monad m =>
  (s %1-> m (Either r (Ur a, s))) -> s %1-> Stream (Of a) m r
unfoldr :: forall (m :: * -> *) s r a.
Monad m =>
(s %1 -> m (Either r (Ur a, s))) -> s %1 -> Stream (Of a) m r
unfoldr s %1 -> m (Either r (Ur a, s))
step s
s = (s %1 -> m (Either r (Ur a, s))) -> s %1 -> Stream (Of a) m r
forall (m :: * -> *) s r a.
Monad m =>
(s %1 -> m (Either r (Ur a, s))) -> s %1 -> Stream (Of a) m r
unfoldr' s %1 -> m (Either r (Ur a, s))
step s
s
  where
    unfoldr' :: Control.Monad m =>
      (s %1-> m (Either r (Ur a, s))) -> s %1-> Stream (Of a) m r
    unfoldr' :: forall (m :: * -> *) s r a.
Monad m =>
(s %1 -> m (Either r (Ur a, s))) -> s %1 -> Stream (Of a) m r
unfoldr' s %1 -> m (Either r (Ur a, s))
step s
s = m (Stream (Of a) m r) %1 -> Stream (Of a) m r
forall (m :: * -> *) (f :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect (m (Stream (Of a) m r) %1 -> Stream (Of a) m r)
%1 -> m (Stream (Of a) m r) %1 -> Stream (Of a) m r
forall a b. (a %1 -> b) %1 -> a %1 -> b
$ s %1 -> m (Either r (Ur a, s))
step s
s m (Either r (Ur a, s))
%1 -> (Either r (Ur a, s) %1 -> m (Stream (Of a) m r))
%1 -> m (Stream (Of a) m r)
forall (m :: * -> *) a b.
Monad m =>
m a %1 -> (a %1 -> m b) %1 -> m b
Control.>>= \case
      Left r
r -> Stream (Of a) m r %1 -> m (Stream (Of a) m r)
forall (m :: * -> *) a. Monad m => a %1 -> m a
Control.return (Stream (Of a) m r %1 -> m (Stream (Of a) m r))
%1 -> Stream (Of a) m r %1 -> m (Stream (Of a) m r)
forall a b. (a %1 -> b) %1 -> a %1 -> b
$ r %1 -> Stream (Of a) m r
forall r (f :: * -> *) (m :: * -> *). r -> Stream f m r
Return r
r
      Right (Ur a
a,s
s') ->
        Stream (Of a) m r %1 -> m (Stream (Of a) m r)
forall (m :: * -> *) a. Monad m => a %1 -> m a
Control.return (Stream (Of a) m r %1 -> m (Stream (Of a) m r))
%1 -> Stream (Of a) m r %1 -> m (Stream (Of a) m r)
forall a b. (a %1 -> b) %1 -> a %1 -> b
$ Of a (Stream (Of a) m r) %1 -> Stream (Of a) m r
forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step (Of a (Stream (Of a) m r) %1 -> Stream (Of a) m r)
%1 -> Of a (Stream (Of a) m r) %1 -> Stream (Of a) m r
forall a b. (a %1 -> b) %1 -> a %1 -> b
$ a
a a -> Stream (Of a) m r %1 -> Of a (Stream (Of a) m r)
forall a b. a -> b -> Of a b
:> (s %1 -> m (Either r (Ur a, s))) -> s %1 -> Stream (Of a) m r
forall (m :: * -> *) s r a.
Monad m =>
(s %1 -> m (Either r (Ur a, s))) -> s %1 -> Stream (Of a) m r
unfoldr s %1 -> m (Either r (Ur a, s))
step s
s'
{-# INLINABLE unfoldr #-}

-- Note: we use the RIO monad from linear base to enforce
-- the protocol of file handles and file I/O
fromHandle :: Handle %1-> Stream (Of Text) RIO ()
fromHandle :: Handle %1 -> Stream (Of Text) RIO ()
fromHandle Handle
h = Handle %1 -> Stream (Of Text) RIO ()
loop Handle
h
  where
    loop :: Handle %1-> Stream (Of Text) RIO ()
    loop :: Handle %1 -> Stream (Of Text) RIO ()
loop Handle
h = Control.do
      (Ur Bool
isEOF, Handle
h') <- RIO (Ur Bool, Handle) %1 -> Stream (Of Text) RIO (Ur Bool, Handle)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a %1 -> t m a
Control.lift (RIO (Ur Bool, Handle)
 %1 -> Stream (Of Text) RIO (Ur Bool, Handle))
%1 -> RIO (Ur Bool, Handle)
%1 -> Stream (Of Text) RIO (Ur Bool, Handle)
forall a b. (a %1 -> b) %1 -> a %1 -> b
$ Handle %1 -> RIO (Ur Bool, Handle)
hIsEOF Handle
h
      case Bool
isEOF of
        Bool
True -> Control.do
          RIO () %1 -> Stream (Of Text) RIO ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a %1 -> t m a
Control.lift (RIO () %1 -> Stream (Of Text) RIO ())
%1 -> RIO () %1 -> Stream (Of Text) RIO ()
forall a b. (a %1 -> b) %1 -> a %1 -> b
$ Handle %1 -> RIO ()
hClose Handle
h'
          () %1 -> Stream (Of Text) RIO ()
forall (m :: * -> *) a. Monad m => a %1 -> m a
Control.return ()
        Bool
False -> Control.do
          (Ur Text
text, Handle
h'') <- RIO (Ur Text, Handle) %1 -> Stream (Of Text) RIO (Ur Text, Handle)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a %1 -> t m a
Control.lift (RIO (Ur Text, Handle)
 %1 -> Stream (Of Text) RIO (Ur Text, Handle))
%1 -> RIO (Ur Text, Handle)
%1 -> Stream (Of Text) RIO (Ur Text, Handle)
forall a b. (a %1 -> b) %1 -> a %1 -> b
$ Handle %1 -> RIO (Ur Text, Handle)
hGetLine Handle
h'
          Text -> Stream (Of Text) RIO ()
forall (m :: * -> *) a. Monad m => a -> Stream (Of a) m ()
yield Text
text
          Handle %1 -> Stream (Of Text) RIO ()
fromHandle Handle
h''
{-# INLINABLE fromHandle #-}

{-| Read the lines of a file given the filename.

-}
readFile :: FilePath -> Stream (Of Text) RIO ()
readFile :: FilePath -> Stream (Of Text) RIO ()
readFile FilePath
path = Control.do
  Handle
handle <- RIO Handle %1 -> Stream (Of Text) RIO Handle
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a %1 -> t m a
Control.lift (RIO Handle %1 -> Stream (Of Text) RIO Handle)
%1 -> RIO Handle %1 -> Stream (Of Text) RIO Handle
forall a b. (a %1 -> b) %1 -> a %1 -> b
$ FilePath -> IOMode -> RIO Handle
openFile FilePath
path IOMode
System.ReadMode
  Handle %1 -> Stream (Of Text) RIO ()
fromHandle Handle
handle

-- | Repeat an element several times.
replicate :: (HasCallStack, Control.Monad m) => Int -> a -> Stream (Of a) m ()
replicate :: forall (m :: * -> *) a.
(HasCallStack, Monad m) =>
Int -> a -> Stream (Of a) m ()
replicate Int
n a
a
  | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0 = FilePath -> Stream (Of a) m ()
forall a. HasCallStack => FilePath -> a
Prelude.error FilePath
"Cannot replicate a stream of negative length"
  | Bool
otherwise = Int -> a -> Stream (Of a) m ()
forall (m :: * -> *) a. Monad m => Int -> a -> Stream (Of a) m ()
loop Int
n a
a
    where
      loop :: Control.Monad m => Int -> a -> Stream (Of a) m ()
      loop :: forall (m :: * -> *) a. Monad m => Int -> a -> Stream (Of a) m ()
loop Int
n a
a
        | Int
n Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 = () -> Stream (Of a) m ()
forall r (f :: * -> *) (m :: * -> *). r -> Stream f m r
Return ()
        | Bool
otherwise = m (Stream (Of a) m ()) %1 -> Stream (Of a) m ()
forall (m :: * -> *) (f :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect (m (Stream (Of a) m ()) %1 -> Stream (Of a) m ())
%1 -> m (Stream (Of a) m ()) %1 -> Stream (Of a) m ()
forall a b. (a %1 -> b) %1 -> a %1 -> b
$ Stream (Of a) m () %1 -> m (Stream (Of a) m ())
forall (m :: * -> *) a. Monad m => a %1 -> m a
Control.return (Stream (Of a) m () %1 -> m (Stream (Of a) m ()))
%1 -> Stream (Of a) m () %1 -> m (Stream (Of a) m ())
forall a b. (a %1 -> b) %1 -> a %1 -> b
$ Of a (Stream (Of a) m ()) %1 -> Stream (Of a) m ()
forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step (Of a (Stream (Of a) m ()) %1 -> Stream (Of a) m ())
%1 -> Of a (Stream (Of a) m ()) %1 -> Stream (Of a) m ()
forall a b. (a %1 -> b) %1 -> a %1 -> b
$ a
a a -> Stream (Of a) m () -> Of a (Stream (Of a) m ())
forall a b. a -> b -> Of a b
:> Int -> a -> Stream (Of a) m ()
forall (m :: * -> *) a. Monad m => Int -> a -> Stream (Of a) m ()
loop (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1) a
a
{-# INLINABLE replicate #-}

{-| Repeat an action several times, streaming its results.

@
\>\>\> import qualified Unsafe.Linear as Unsafe
\>\>\> import qualified Data.Time as Time
\>\>\> let getCurrentTime = fromSystemIO (Unsafe.coerce Time.getCurrentTime)
\>\>\> S.print $ S.replicateM 2 getCurrentTime
2015-08-18 00:57:36.124508 UTC
2015-08-18 00:57:36.124785 UTC
@
-}
replicateM :: Control.Monad m =>
  Int -> m (Ur a) -> Stream (Of a) m ()
replicateM :: forall (m :: * -> *) a.
Monad m =>
Int -> m (Ur a) -> Stream (Of a) m ()
replicateM Int
n m (Ur a)
ma
  | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0 = FilePath -> Stream (Of a) m ()
forall a. HasCallStack => FilePath -> a
Prelude.error FilePath
"Cannot replicate a stream of negative length"
  | Bool
otherwise = Int -> m (Ur a) -> Stream (Of a) m ()
forall (m :: * -> *) a.
Monad m =>
Int -> m (Ur a) -> Stream (Of a) m ()
loop Int
n m (Ur a)
ma
    where
      loop :: Control.Monad m => Int -> m (Ur a) -> Stream (Of a) m ()
      loop :: forall (m :: * -> *) a.
Monad m =>
Int -> m (Ur a) -> Stream (Of a) m ()
loop Int
n m (Ur a)
ma
        | Int
n Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 = () -> Stream (Of a) m ()
forall r (f :: * -> *) (m :: * -> *). r -> Stream f m r
Return ()
        | Bool
otherwise = m (Stream (Of a) m ()) %1 -> Stream (Of a) m ()
forall (m :: * -> *) (f :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect (m (Stream (Of a) m ()) %1 -> Stream (Of a) m ())
%1 -> m (Stream (Of a) m ()) %1 -> Stream (Of a) m ()
forall a b. (a %1 -> b) %1 -> a %1 -> b
$ Control.do
          Ur a
a <- m (Ur a)
ma
          Stream (Of a) m () %1 -> m (Stream (Of a) m ())
forall (m :: * -> *) a. Monad m => a %1 -> m a
Control.return (Stream (Of a) m () %1 -> m (Stream (Of a) m ()))
%1 -> Stream (Of a) m () %1 -> m (Stream (Of a) m ())
forall a b. (a %1 -> b) %1 -> a %1 -> b
$ Of a (Stream (Of a) m ()) %1 -> Stream (Of a) m ()
forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step (Of a (Stream (Of a) m ()) %1 -> Stream (Of a) m ())
%1 -> Of a (Stream (Of a) m ()) %1 -> Stream (Of a) m ()
forall a b. (a %1 -> b) %1 -> a %1 -> b
$ a
a a -> Stream (Of a) m () -> Of a (Stream (Of a) m ())
forall a b. a -> b -> Of a b
:> (Int -> m (Ur a) -> Stream (Of a) m ()
forall (m :: * -> *) a.
Monad m =>
Int -> m (Ur a) -> Stream (Of a) m ()
replicateM (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1) m (Ur a)
ma)

-- | Replicate a constant element and zip it with the finite stream which
-- is the first argument.
replicateZip :: Control.Monad m =>
  Stream (Of x) m r -> a -> Stream (Of (a,x)) m r
replicateZip :: forall (m :: * -> *) x r a.
Monad m =>
Stream (Of x) m r -> a -> Stream (Of (a, x)) m r
replicateZip Stream (Of x) m r
stream a
a = (x -> (a, x)) -> Stream (Of x) m r %1 -> Stream (Of (a, x)) m r
forall (m :: * -> *) a b r.
Monad m =>
(a -> b) -> Stream (Of a) m r %1 -> Stream (Of b) m r
map ((,) a
a) Stream (Of x) m r
stream
{-# INLINABLE replicateZip #-}

untilRight :: forall m a r . Control.Monad m =>
  m (Either (Ur a) r) -> Stream (Of a) m r
untilRight :: forall (m :: * -> *) a r.
Monad m =>
m (Either (Ur a) r) -> Stream (Of a) m r
untilRight m (Either (Ur a) r)
mEither = m (Stream (Of a) m r) -> Stream (Of a) m r
forall (m :: * -> *) (f :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect m (Stream (Of a) m r)
loop
  where
    loop :: m (Stream (Of a) m r)
    loop :: m (Stream (Of a) m r)
loop = Control.do
      Either (Ur a) r
either <- m (Either (Ur a) r)
mEither
      Either (Ur a) r
either Either (Ur a) r
%1 -> (Either (Ur a) r %1 -> m (Stream (Of a) m r))
%1 -> m (Stream (Of a) m r)
forall a b. a %1 -> (a %1 -> b) %1 -> b
& \case
        Left (Ur a
a) ->
          Stream (Of a) m r %1 -> m (Stream (Of a) m r)
forall (m :: * -> *) a. Monad m => a %1 -> m a
Control.return (Stream (Of a) m r %1 -> m (Stream (Of a) m r))
%1 -> Stream (Of a) m r %1 -> m (Stream (Of a) m r)
forall a b. (a %1 -> b) %1 -> a %1 -> b
$ Of a (Stream (Of a) m r) %1 -> Stream (Of a) m r
forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step (Of a (Stream (Of a) m r) %1 -> Stream (Of a) m r)
%1 -> Of a (Stream (Of a) m r) %1 -> Stream (Of a) m r
forall a b. (a %1 -> b) %1 -> a %1 -> b
$ a
a a -> Stream (Of a) m r -> Of a (Stream (Of a) m r)
forall a b. a -> b -> Of a b
:> (m (Either (Ur a) r) -> Stream (Of a) m r
forall (m :: * -> *) a r.
Monad m =>
m (Either (Ur a) r) -> Stream (Of a) m r
untilRight m (Either (Ur a) r)
mEither)
        Right r
r -> Stream (Of a) m r %1 -> m (Stream (Of a) m r)
forall (m :: * -> *) a. Monad m => a %1 -> m a
Control.return (Stream (Of a) m r %1 -> m (Stream (Of a) m r))
%1 -> Stream (Of a) m r %1 -> m (Stream (Of a) m r)
forall a b. (a %1 -> b) %1 -> a %1 -> b
$ r %1 -> Stream (Of a) m r
forall r (f :: * -> *) (m :: * -> *). r -> Stream f m r
Return r
r
{-# INLINABLE untilRight #-}


-- # The \"Affine\" 'Stream'
-------------------------------------------------------------------------------

-- | An *affine stream is represented with a state of type @x@,
-- a possibly terminating step function of type @(x %1-> m (Either (f x) r))@,
-- and a stop-short function @(x %1-> m r)@.
--
-- This mirrors the unfold of a normal stream:
--
-- > data Stream f m r where
-- >   Stream :: x %1-> (x %1-> m (Either (f x) r)) -> Stream f m r
--
-- *Though referred to as an \"affine stream\" this might not be the correct
-- definition for affine streams. Sorting this out requires a bit more
-- careful thought.
data AffineStream f m r where
  AffineStream ::
    x %1->
    (x %1-> m (Either (f x) r)) ->
    (x %1-> m r) ->
    AffineStream f m r

-- | Take @n@ number of elements from the affine stream, for non-negative
-- @n@. (Negative @n@ is treated as 0.)
take :: forall f m r. (Control.Monad m, Control.Functor f) =>
  Int -> AffineStream f m r %1-> Stream f m r
take :: forall (f :: * -> *) (m :: * -> *) r.
(Monad m, Functor f) =>
Int -> AffineStream f m r %1 -> Stream f m r
take = Int -> AffineStream f m r %1 -> Stream f m r
loop where
  loop :: Int -> AffineStream f m r %1-> Stream f m r
  loop :: Int -> AffineStream f m r %1 -> Stream f m r
loop Int
n (AffineStream x
s x %1 -> m (Either (f x) r)
step x %1 -> m r
end)
    | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = m (Stream f m r) %1 -> Stream f m r
forall (m :: * -> *) (f :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect (m (Stream f m r) %1 -> Stream f m r)
%1 -> m (Stream f m r) %1 -> Stream f m r
forall a b. (a %1 -> b) %1 -> a %1 -> b
$ (r %1 -> Stream f m r) %1 -> m r %1 -> m (Stream f m r)
forall (f :: * -> *) a b.
Functor f =>
(a %1 -> b) %1 -> f a %1 -> f b
Control.fmap r %1 -> Stream f m r
forall (m :: * -> *) a. Monad m => a %1 -> m a
Control.return (m r %1 -> m (Stream f m r)) %1 -> m r %1 -> m (Stream f m r)
forall a b. (a %1 -> b) %1 -> a %1 -> b
$ x %1 -> m r
end x
s
    | Bool
otherwise = m (Stream f m r) %1 -> Stream f m r
forall (m :: * -> *) (f :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect (m (Stream f m r) %1 -> Stream f m r)
%1 -> m (Stream f m r) %1 -> Stream f m r
forall a b. (a %1 -> b) %1 -> a %1 -> b
$ Control.do
        Either (f x) r
next <- x %1 -> m (Either (f x) r)
step x
s
        Either (f x) r
next Either (f x) r
%1 -> (Either (f x) r %1 -> m (Stream f m r))
%1 -> m (Stream f m r)
forall a b. a %1 -> (a %1 -> b) %1 -> b
& \case
          Right r
r -> Stream f m r %1 -> m (Stream f m r)
forall (m :: * -> *) a. Monad m => a %1 -> m a
Control.return (r %1 -> Stream f m r
forall r (f :: * -> *) (m :: * -> *). r -> Stream f m r
Return r
r)
          Left f x
fx -> Stream f m r %1 -> m (Stream f m r)
forall (m :: * -> *) a. Monad m => a %1 -> m a
Control.return (Stream f m r %1 -> m (Stream f m r))
%1 -> Stream f m r %1 -> m (Stream f m r)
forall a b. (a %1 -> b) %1 -> a %1 -> b
$ f (Stream f m r) %1 -> Stream f m r
forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step (f (Stream f m r) %1 -> Stream f m r)
%1 -> f (Stream f m r) %1 -> Stream f m r
forall a b. (a %1 -> b) %1 -> a %1 -> b
$
            (x %1 -> Stream f m r) %1 -> f x %1 -> f (Stream f m r)
forall (f :: * -> *) a b.
Functor f =>
(a %1 -> b) %1 -> f a %1 -> f b
Control.fmap (\x
x -> Int -> AffineStream f m r %1 -> Stream f m r
loop (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1) (x
%1 -> (x %1 -> m (Either (f x) r))
-> (x %1 -> m r)
-> AffineStream f m r
forall x (m :: * -> *) (f :: * -> *) r.
x
-> (x %1 -> m (Either (f x) r))
-> (x %1 -> m r)
-> AffineStream f m r
AffineStream x
x x %1 -> m (Either (f x) r)
step x %1 -> m r
end)) f x
fx
{-# INLINABLE take #-}

-- | Run an affine stream until it ends or a monadic test succeeds.
-- Drop the element it succeeds on.
untilM :: forall a m r. Control.Monad m =>
  (a -> m Bool) -> AffineStream (Of a) m r %1-> Stream (Of a) m r
untilM :: forall a (m :: * -> *) r.
Monad m =>
(a -> m Bool) -> AffineStream (Of a) m r %1 -> Stream (Of a) m r
untilM = (a -> m Bool) -> AffineStream (Of a) m r %1 -> Stream (Of a) m r
loop where
  loop :: (a -> m Bool) -> AffineStream (Of a) m r %1-> Stream (Of a) m r
  loop :: (a -> m Bool) -> AffineStream (Of a) m r %1 -> Stream (Of a) m r
loop a -> m Bool
test (AffineStream x
s x %1 -> m (Either (Of a x) r)
step x %1 -> m r
end) = m (Stream (Of a) m r) %1 -> Stream (Of a) m r
forall (m :: * -> *) (f :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect (m (Stream (Of a) m r) %1 -> Stream (Of a) m r)
%1 -> m (Stream (Of a) m r) %1 -> Stream (Of a) m r
forall a b. (a %1 -> b) %1 -> a %1 -> b
$ Control.do
    Either (Of a x) r
next <- x %1 -> m (Either (Of a x) r)
step x
s
    Either (Of a x) r
next Either (Of a x) r
%1 -> (Either (Of a x) r %1 -> m (Stream (Of a) m r))
%1 -> m (Stream (Of a) m r)
forall a b. a %1 -> (a %1 -> b) %1 -> b
& \case
      Right r
r -> Stream (Of a) m r %1 -> m (Stream (Of a) m r)
forall (m :: * -> *) a. Monad m => a %1 -> m a
Control.return (r %1 -> Stream (Of a) m r
forall r (f :: * -> *) (m :: * -> *). r -> Stream f m r
Return r
r)
      Left (a
a :> x
next) -> Control.do
        Bool
testResult <- a -> m Bool
test a
a
        Bool
testResult Bool
%1 -> (Bool %1 -> m (Stream (Of a) m r))
%1 -> m (Stream (Of a) m r)
forall a b. a %1 -> (a %1 -> b) %1 -> b
& \case
          Bool
False -> Stream (Of a) m r %1 -> m (Stream (Of a) m r)
forall (m :: * -> *) a. Monad m => a %1 -> m a
Control.return (Stream (Of a) m r %1 -> m (Stream (Of a) m r))
%1 -> Stream (Of a) m r %1 -> m (Stream (Of a) m r)
forall a b. (a %1 -> b) %1 -> a %1 -> b
$
            Of a (Stream (Of a) m r) %1 -> Stream (Of a) m r
forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step (Of a (Stream (Of a) m r) %1 -> Stream (Of a) m r)
%1 -> Of a (Stream (Of a) m r) %1 -> Stream (Of a) m r
forall a b. (a %1 -> b) %1 -> a %1 -> b
$ a
a a -> Stream (Of a) m r %1 -> Of a (Stream (Of a) m r)
forall a b. a -> b -> Of a b
:> (a -> m Bool) -> AffineStream (Of a) m r %1 -> Stream (Of a) m r
loop a -> m Bool
test (x
%1 -> (x %1 -> m (Either (Of a x) r))
-> (x %1 -> m r)
-> AffineStream (Of a) m r
forall x (m :: * -> *) (f :: * -> *) r.
x
-> (x %1 -> m (Either (f x) r))
-> (x %1 -> m r)
-> AffineStream f m r
AffineStream x
next x %1 -> m (Either (Of a x) r)
step x %1 -> m r
end)
          Bool
True -> (r %1 -> Stream (Of a) m r) %1 -> m r %1 -> m (Stream (Of a) m r)
forall (f :: * -> *) a b.
Functor f =>
(a %1 -> b) %1 -> f a %1 -> f b
Control.fmap r %1 -> Stream (Of a) m r
forall (m :: * -> *) a. Monad m => a %1 -> m a
Control.return (m r %1 -> m (Stream (Of a) m r))
%1 -> m r %1 -> m (Stream (Of a) m r)
forall a b. (a %1 -> b) %1 -> a %1 -> b
$ x %1 -> m r
end x
next
{-# INLINABLE untilM #-}

-- | Like 'untilM' but without the monadic test.
until :: forall a m r. Control.Monad m =>
  (a -> Bool) -> AffineStream (Of a) m r %1-> Stream (Of a) m r
until :: forall a (m :: * -> *) r.
Monad m =>
(a -> Bool) -> AffineStream (Of a) m r %1 -> Stream (Of a) m r
until = (a -> Bool) -> AffineStream (Of a) m r %1 -> Stream (Of a) m r
loop where
  loop :: (a -> Bool) -> AffineStream (Of a) m r %1-> Stream (Of a) m r
  loop :: (a -> Bool) -> AffineStream (Of a) m r %1 -> Stream (Of a) m r
loop a -> Bool
test (AffineStream x
s x %1 -> m (Either (Of a x) r)
step x %1 -> m r
end) = m (Stream (Of a) m r) %1 -> Stream (Of a) m r
forall (m :: * -> *) (f :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect (m (Stream (Of a) m r) %1 -> Stream (Of a) m r)
%1 -> m (Stream (Of a) m r) %1 -> Stream (Of a) m r
forall a b. (a %1 -> b) %1 -> a %1 -> b
$ Control.do
    Either (Of a x) r
next <- x %1 -> m (Either (Of a x) r)
step x
s
    Either (Of a x) r
next Either (Of a x) r
%1 -> (Either (Of a x) r %1 -> m (Stream (Of a) m r))
%1 -> m (Stream (Of a) m r)
forall a b. a %1 -> (a %1 -> b) %1 -> b
& \case
      Right r
r -> Stream (Of a) m r %1 -> m (Stream (Of a) m r)
forall (m :: * -> *) a. Monad m => a %1 -> m a
Control.return (r %1 -> Stream (Of a) m r
forall r (f :: * -> *) (m :: * -> *). r -> Stream f m r
Return r
r)
      Left (a
a :> x
next) -> case a -> Bool
test a
a of
        Bool
True -> (r %1 -> Stream (Of a) m r) %1 -> m r %1 -> m (Stream (Of a) m r)
forall (f :: * -> *) a b.
Functor f =>
(a %1 -> b) %1 -> f a %1 -> f b
Control.fmap r %1 -> Stream (Of a) m r
forall (m :: * -> *) a. Monad m => a %1 -> m a
Control.return (m r %1 -> m (Stream (Of a) m r))
%1 -> m r %1 -> m (Stream (Of a) m r)
forall a b. (a %1 -> b) %1 -> a %1 -> b
$ x %1 -> m r
end x
next
        Bool
False -> Stream (Of a) m r %1 -> m (Stream (Of a) m r)
forall (m :: * -> *) a. Monad m => a %1 -> m a
Control.return (Stream (Of a) m r %1 -> m (Stream (Of a) m r))
%1 -> Stream (Of a) m r %1 -> m (Stream (Of a) m r)
forall a b. (a %1 -> b) %1 -> a %1 -> b
$ Of a (Stream (Of a) m r) %1 -> Stream (Of a) m r
forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step (Of a (Stream (Of a) m r) %1 -> Stream (Of a) m r)
%1 -> Of a (Stream (Of a) m r) %1 -> Stream (Of a) m r
forall a b. (a %1 -> b) %1 -> a %1 -> b
$
          a
a a -> Stream (Of a) m r %1 -> Of a (Stream (Of a) m r)
forall a b. a -> b -> Of a b
:> (a -> Bool) -> AffineStream (Of a) m r %1 -> Stream (Of a) m r
loop a -> Bool
test (x
%1 -> (x %1 -> m (Either (Of a x) r))
-> (x %1 -> m r)
-> AffineStream (Of a) m r
forall x (m :: * -> *) (f :: * -> *) r.
x
-> (x %1 -> m (Either (f x) r))
-> (x %1 -> m r)
-> AffineStream f m r
AffineStream x
next x %1 -> m (Either (Of a x) r)
step x %1 -> m r
end)
{-# INLINABLE until #-}

-- | Zip a finite stream with an affine stream.
zip :: forall a x m r1 r2. Control.Monad m =>
  Stream (Of x) m r1 %1->
  AffineStream (Of a) m r2 %1->
  Stream (Of (x,a)) m (r1,r2)
zip :: forall a x (m :: * -> *) r1 r2.
Monad m =>
Stream (Of x) m r1
%1 -> AffineStream (Of a) m r2 %1 -> Stream (Of (x, a)) m (r1, r2)
zip = Stream (Of x) m r1
%1 -> AffineStream (Of a) m r2 %1 -> Stream (Of (x, a)) m (r1, r2)
loop where
  loop ::
    Stream (Of x) m r1 %1->
    AffineStream (Of a) m r2 %1->
    Stream (Of (x,a)) m (r1,r2)
  loop :: Stream (Of x) m r1
%1 -> AffineStream (Of a) m r2 %1 -> Stream (Of (x, a)) m (r1, r2)
loop Stream (Of x) m r1
stream (AffineStream x
s x %1 -> m (Either (Of a x) r2)
step x %1 -> m r2
end) = Stream (Of x) m r1
stream Stream (Of x) m r1
%1 -> (Stream (Of x) m r1 %1 -> Stream (Of (x, a)) m (r1, r2))
%1 -> Stream (Of (x, a)) m (r1, r2)
forall a b. a %1 -> (a %1 -> b) %1 -> b
& \case
    Return r1
r1 -> m (Stream (Of (x, a)) m (r1, r2))
%1 -> Stream (Of (x, a)) m (r1, r2)
forall (m :: * -> *) (f :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect (m (Stream (Of (x, a)) m (r1, r2))
 %1 -> Stream (Of (x, a)) m (r1, r2))
%1 -> m (Stream (Of (x, a)) m (r1, r2))
%1 -> Stream (Of (x, a)) m (r1, r2)
forall a b. (a %1 -> b) %1 -> a %1 -> b
$
      (r2 %1 -> Stream (Of (x, a)) m (r1, r2))
%1 -> m r2 %1 -> m (Stream (Of (x, a)) m (r1, r2))
forall (f :: * -> *) a b.
Functor f =>
(a %1 -> b) %1 -> f a %1 -> f b
Control.fmap (\r2
r2 -> (r1, r2) %1 -> Stream (Of (x, a)) m (r1, r2)
forall (m :: * -> *) a. Monad m => a %1 -> m a
Control.return ((r1, r2) %1 -> Stream (Of (x, a)) m (r1, r2))
%1 -> (r1, r2) %1 -> Stream (Of (x, a)) m (r1, r2)
forall a b. (a %1 -> b) %1 -> a %1 -> b
$ (r1
r1,r2
r2)) (m r2 %1 -> m (Stream (Of (x, a)) m (r1, r2)))
%1 -> m r2 %1 -> m (Stream (Of (x, a)) m (r1, r2))
forall a b. (a %1 -> b) %1 -> a %1 -> b
$ x %1 -> m r2
end x
s
    Effect m (Stream (Of x) m r1)
m -> m (Stream (Of (x, a)) m (r1, r2))
%1 -> Stream (Of (x, a)) m (r1, r2)
forall (m :: * -> *) (f :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect (m (Stream (Of (x, a)) m (r1, r2))
 %1 -> Stream (Of (x, a)) m (r1, r2))
%1 -> m (Stream (Of (x, a)) m (r1, r2))
%1 -> Stream (Of (x, a)) m (r1, r2)
forall a b. (a %1 -> b) %1 -> a %1 -> b
$
      (Stream (Of x) m r1 %1 -> Stream (Of (x, a)) m (r1, r2))
%1 -> m (Stream (Of x) m r1)
%1 -> m (Stream (Of (x, a)) m (r1, r2))
forall (f :: * -> *) a b.
Functor f =>
(a %1 -> b) %1 -> f a %1 -> f b
Control.fmap (\Stream (Of x) m r1
str -> Stream (Of x) m r1
%1 -> AffineStream (Of a) m r2 %1 -> Stream (Of (x, a)) m (r1, r2)
loop Stream (Of x) m r1
str (x
%1 -> (x %1 -> m (Either (Of a x) r2))
-> (x %1 -> m r2)
-> AffineStream (Of a) m r2
forall x (m :: * -> *) (f :: * -> *) r.
x
-> (x %1 -> m (Either (f x) r))
-> (x %1 -> m r)
-> AffineStream f m r
AffineStream x
s x %1 -> m (Either (Of a x) r2)
step x %1 -> m r2
end)) m (Stream (Of x) m r1)
m
    Step (x
x :> Stream (Of x) m r1
rest) -> m (Stream (Of (x, a)) m (r1, r2))
%1 -> Stream (Of (x, a)) m (r1, r2)
forall (m :: * -> *) (f :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect (m (Stream (Of (x, a)) m (r1, r2))
 %1 -> Stream (Of (x, a)) m (r1, r2))
%1 -> m (Stream (Of (x, a)) m (r1, r2))
%1 -> Stream (Of (x, a)) m (r1, r2)
forall a b. (a %1 -> b) %1 -> a %1 -> b
$ Control.do
      Either (Of a x) r2
next <- x %1 -> m (Either (Of a x) r2)
step x
s
      Either (Of a x) r2
next Either (Of a x) r2
%1 -> (Either (Of a x) r2 %1 -> m (Stream (Of (x, a)) m (r1, r2)))
%1 -> m (Stream (Of (x, a)) m (r1, r2))
forall a b. a %1 -> (a %1 -> b) %1 -> b
& \case
        Right r2
r2 -> Control.do
          r1
r1 <- Stream (Of x) m r1 %1 -> m r1
forall a (m :: * -> *) r. Monad m => Stream (Of a) m r %1 -> m r
effects Stream (Of x) m r1
rest
          Stream (Of (x, a)) m (r1, r2)
%1 -> m (Stream (Of (x, a)) m (r1, r2))
forall (m :: * -> *) a. Monad m => a %1 -> m a
Control.return ((r1, r2) %1 -> Stream (Of (x, a)) m (r1, r2)
forall r (f :: * -> *) (m :: * -> *). r -> Stream f m r
Return (r1
r1,r2
r2))
        Left (a
a :> x
rest') -> Stream (Of (x, a)) m (r1, r2)
%1 -> m (Stream (Of (x, a)) m (r1, r2))
forall (m :: * -> *) a. Monad m => a %1 -> m a
Control.return (Stream (Of (x, a)) m (r1, r2)
 %1 -> m (Stream (Of (x, a)) m (r1, r2)))
%1 -> Stream (Of (x, a)) m (r1, r2)
%1 -> m (Stream (Of (x, a)) m (r1, r2))
forall a b. (a %1 -> b) %1 -> a %1 -> b
$ Of (x, a) (Stream (Of (x, a)) m (r1, r2))
%1 -> Stream (Of (x, a)) m (r1, r2)
forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step (Of (x, a) (Stream (Of (x, a)) m (r1, r2))
 %1 -> Stream (Of (x, a)) m (r1, r2))
%1 -> Of (x, a) (Stream (Of (x, a)) m (r1, r2))
%1 -> Stream (Of (x, a)) m (r1, r2)
forall a b. (a %1 -> b) %1 -> a %1 -> b
$
          (x
x,a
a) (x, a)
-> Stream (Of (x, a)) m (r1, r2)
%1 -> Of (x, a) (Stream (Of (x, a)) m (r1, r2))
forall a b. a -> b -> Of a b
:> Stream (Of x) m r1
%1 -> AffineStream (Of a) m r2 %1 -> Stream (Of (x, a)) m (r1, r2)
loop Stream (Of x) m r1
rest (x
%1 -> (x %1 -> m (Either (Of a x) r2))
-> (x %1 -> m r2)
-> AffineStream (Of a) m r2
forall x (m :: * -> *) (f :: * -> *) r.
x
-> (x %1 -> m (Either (f x) r))
-> (x %1 -> m r)
-> AffineStream f m r
AffineStream x
rest' x %1 -> m (Either (Of a x) r2)
step x %1 -> m r2
end)
{-# INLINABLE zip #-}

-- | An affine stream of standard input lines.
stdinLn :: AffineStream (Of Text) IO ()
stdinLn :: AffineStream (Of Text) IO ()
stdinLn = ()
-> (() %1 -> IO (Either (Of Text ()) ()))
-> (() %1 -> IO ())
-> AffineStream (Of Text) IO ()
forall x (m :: * -> *) (f :: * -> *) r.
x
-> (x %1 -> m (Either (f x) r))
-> (x %1 -> m r)
-> AffineStream f m r
AffineStream () () %1 -> IO (Either (Of Text ()) ())
getALine () %1 -> IO ()
forall (f :: * -> *) a. Applicative f => a %1 -> f a
Control.pure where
  getALine :: () %1-> IO (Either (Of Text ()) ())
  getALine :: () %1 -> IO (Either (Of Text ()) ())
getALine () = Control.do
    Ur FilePath
line <- IO FilePath -> IO (Ur FilePath)
forall a. IO a -> IO (Ur a)
fromSystemIOU IO FilePath
System.getLine
    Either (Of Text ()) () %1 -> IO (Either (Of Text ()) ())
forall (m :: * -> *) a. Monad m => a %1 -> m a
Control.return (Either (Of Text ()) () %1 -> IO (Either (Of Text ()) ()))
%1 -> Either (Of Text ()) () %1 -> IO (Either (Of Text ()) ())
forall a b. (a %1 -> b) %1 -> a %1 -> b
$ Of Text () -> Either (Of Text ()) ()
forall a b. a -> Either a b
Left (FilePath -> Text
Text.pack FilePath
line Text -> () -> Of Text ()
forall a b. a -> b -> Of a b
:> ())

-- | An affine stream of reading lines, crashing on failed parse.
readLn :: Read a => AffineStream (Of a) IO ()
readLn :: forall a. Read a => AffineStream (Of a) IO ()
readLn = ()
-> (() %1 -> IO (Either (Of a ()) ()))
-> (() %1 -> IO ())
-> AffineStream (Of a) IO ()
forall x (m :: * -> *) (f :: * -> *) r.
x
-> (x %1 -> m (Either (f x) r))
-> (x %1 -> m r)
-> AffineStream f m r
AffineStream () () %1 -> IO (Either (Of a ()) ())
forall a. Read a => () %1 -> IO (Either (Of a ()) ())
readALine () %1 -> IO ()
forall (f :: * -> *) a. Applicative f => a %1 -> f a
Control.pure where
  readALine :: Read a => () %1-> IO (Either (Of a ()) ())
  readALine :: forall a. Read a => () %1 -> IO (Either (Of a ()) ())
readALine () = Control.do
    Ur FilePath
line <- IO FilePath -> IO (Ur FilePath)
forall a. IO a -> IO (Ur a)
fromSystemIOU IO FilePath
System.getLine
    Either (Of a ()) () %1 -> IO (Either (Of a ()) ())
forall (m :: * -> *) a. Monad m => a %1 -> m a
Control.return (Either (Of a ()) () %1 -> IO (Either (Of a ()) ()))
%1 -> Either (Of a ()) () %1 -> IO (Either (Of a ()) ())
forall a b. (a %1 -> b) %1 -> a %1 -> b
$ Of a () -> Either (Of a ()) ()
forall a b. a -> Either a b
Left (FilePath -> a
forall a. Read a => FilePath -> a
Prelude.read FilePath
line a -> () -> Of a ()
forall a b. a -> b -> Of a b
:> ())

-- | An affine stream iterating an initial state forever.
iterate :: forall a m.
  Control.Monad m => a -> (a -> a) -> AffineStream (Of a) m ()
iterate :: forall a (m :: * -> *).
Monad m =>
a -> (a -> a) -> AffineStream (Of a) m ()
iterate a
a a -> a
step =
  Ur a
-> (Ur a %1 -> m (Either (Of a (Ur a)) ()))
-> (Ur a %1 -> m ())
-> AffineStream (Of a) m ()
forall x (m :: * -> *) (f :: * -> *) r.
x
-> (x %1 -> m (Either (f x) r))
-> (x %1 -> m r)
-> AffineStream f m r
AffineStream (a -> Ur a
forall a. a -> Ur a
Ur a
a) Ur a %1 -> m (Either (Of a (Ur a)) ())
stepper (\Ur a
x -> () %1 -> m ()
forall (m :: * -> *) a. Monad m => a %1 -> m a
Control.return (() %1 -> m ()) %1 -> () %1 -> m ()
forall a b. (a %1 -> b) %1 -> a %1 -> b
$ Ur a %1 -> ()
forall a. Consumable a => a %1 -> ()
consume Ur a
x)
  where
    stepper :: Ur a %1-> m (Either (Of a (Ur a)) ())
    stepper :: Ur a %1 -> m (Either (Of a (Ur a)) ())
stepper (Ur a
a) = Either (Of a (Ur a)) () %1 -> m (Either (Of a (Ur a)) ())
forall (m :: * -> *) a. Monad m => a %1 -> m a
Control.return (Either (Of a (Ur a)) () %1 -> m (Either (Of a (Ur a)) ()))
%1 -> Either (Of a (Ur a)) () %1 -> m (Either (Of a (Ur a)) ())
forall a b. (a %1 -> b) %1 -> a %1 -> b
$
      Of a (Ur a) %1 -> Either (Of a (Ur a)) ()
forall a b. a -> Either a b
Left (Of a (Ur a) %1 -> Either (Of a (Ur a)) ())
%1 -> Of a (Ur a) %1 -> Either (Of a (Ur a)) ()
forall a b. (a %1 -> b) %1 -> a %1 -> b
$ a
a a -> Ur a -> Of a (Ur a)
forall a b. a -> b -> Of a b
:> a -> Ur a
forall a. a -> Ur a
Ur (a -> a
step a
a)

-- | An affine stream monadically iterating an initial state forever.
iterateM :: forall a m. Control.Monad m =>
  m (Ur a) -> (a -> m (Ur a)) -> AffineStream (Of a) m ()
iterateM :: forall a (m :: * -> *).
Monad m =>
m (Ur a) -> (a -> m (Ur a)) -> AffineStream (Of a) m ()
iterateM m (Ur a)
ma a -> m (Ur a)
step =
  m (Ur a)
-> (m (Ur a) %1 -> m (Either (Of a (m (Ur a))) ()))
-> (m (Ur a) %1 -> m ())
-> AffineStream (Of a) m ()
forall x (m :: * -> *) (f :: * -> *) r.
x
-> (x %1 -> m (Either (f x) r))
-> (x %1 -> m r)
-> AffineStream f m r
AffineStream m (Ur a)
ma m (Ur a) %1 -> m (Either (Of a (m (Ur a))) ())
stepper ((Ur a %1 -> ()) %1 -> m (Ur a) %1 -> m ()
forall (f :: * -> *) a b.
Functor f =>
(a %1 -> b) %1 -> f a %1 -> f b
Control.fmap Ur a %1 -> ()
forall a. Consumable a => a %1 -> ()
consume)
  where
    stepper :: m (Ur a) %1-> m (Either (Of a (m (Ur a))) ())
    stepper :: m (Ur a) %1 -> m (Either (Of a (m (Ur a))) ())
stepper m (Ur a)
ma = Control.do
      Ur a
a <- m (Ur a)
ma
      Either (Of a (m (Ur a))) () %1 -> m (Either (Of a (m (Ur a))) ())
forall (m :: * -> *) a. Monad m => a %1 -> m a
Control.return (Either (Of a (m (Ur a))) () %1 -> m (Either (Of a (m (Ur a))) ()))
%1 -> Either (Of a (m (Ur a))) ()
%1 -> m (Either (Of a (m (Ur a))) ())
forall a b. (a %1 -> b) %1 -> a %1 -> b
$ Of a (m (Ur a)) %1 -> Either (Of a (m (Ur a))) ()
forall a b. a -> Either a b
Left (Of a (m (Ur a)) %1 -> Either (Of a (m (Ur a))) ())
%1 -> Of a (m (Ur a)) %1 -> Either (Of a (m (Ur a))) ()
forall a b. (a %1 -> b) %1 -> a %1 -> b
$ a
a a -> m (Ur a) -> Of a (m (Ur a))
forall a b. a -> b -> Of a b
:> (a -> m (Ur a)
step a
a)

-- Remark. In order to implement the affine break function, which is the third
-- argument of the constructor, we need to specify the functor as @Of@.
-- Approaches to keeping it functor general seem messy.

-- | An affine stream cycling through a given finite stream forever.
cycle :: forall a m r. (Control.Monad m, Consumable r) =>
  Stream (Of a) m r -> AffineStream (Of a) m r
cycle :: forall a (m :: * -> *) r.
(Monad m, Consumable r) =>
Stream (Of a) m r -> AffineStream (Of a) m r
cycle Stream (Of a) m r
stream =
  -- Note. The state is (original stream, stream_in_current_cycle)
  (Ur (Stream (Of a) m r), Stream (Of a) m r)
-> ((Ur (Stream (Of a) m r), Stream (Of a) m r)
    %1 -> m (Either
               (Of a (Ur (Stream (Of a) m r), Stream (Of a) m r)) r))
-> ((Ur (Stream (Of a) m r), Stream (Of a) m r) %1 -> m r)
-> AffineStream (Of a) m r
forall x (m :: * -> *) (f :: * -> *) r.
x
-> (x %1 -> m (Either (f x) r))
-> (x %1 -> m r)
-> AffineStream f m r
AffineStream (Stream (Of a) m r -> Ur (Stream (Of a) m r)
forall a. a -> Ur a
Ur Stream (Of a) m r
stream, Stream (Of a) m r
stream) (Ur (Stream (Of a) m r), Stream (Of a) m r)
%1 -> m (Either
           (Of a (Ur (Stream (Of a) m r), Stream (Of a) m r)) r)
forall (f :: * -> *).
Functor f =>
(Ur (Stream f m r), Stream f m r)
%1 -> m (Either (f (Ur (Stream f m r), Stream f m r)) r)
stepStream (Ur (Stream (Of a) m r), Stream (Of a) m r) %1 -> m r
leftoverEffects
  where
    leftoverEffects ::
      (Ur (Stream (Of a) m r), Stream (Of a) m r) %1-> m r
    leftoverEffects :: (Ur (Stream (Of a) m r), Stream (Of a) m r) %1 -> m r
leftoverEffects (Ur Stream (Of a) m r
_, Stream (Of a) m r
str) = Stream (Of a) m r %1 -> m r
forall a (m :: * -> *) r. Monad m => Stream (Of a) m r %1 -> m r
effects Stream (Of a) m r
str

    stepStream :: Control.Functor f =>
      (Ur (Stream f m r), Stream f m r) %1->
      m (Either (f (Ur (Stream f m r), Stream f m r)) r)
    stepStream :: forall (f :: * -> *).
Functor f =>
(Ur (Stream f m r), Stream f m r)
%1 -> m (Either (f (Ur (Stream f m r), Stream f m r)) r)
stepStream (Ur Stream f m r
s, Stream f m r
str) = Stream f m r
str Stream f m r
%1 -> (Stream f m r
       %1 -> m (Either (f (Ur (Stream f m r), Stream f m r)) r))
%1 -> m (Either (f (Ur (Stream f m r), Stream f m r)) r)
forall a b. a %1 -> (a %1 -> b) %1 -> b
& \case
      Return r
r -> r
%1 -> m (Either (f (Ur (Stream f m r), Stream f m r)) r)
%1 -> m (Either (f (Ur (Stream f m r), Stream f m r)) r)
forall a b. Consumable a => a %1 -> b %1 -> b
lseq r
r (m (Either (f (Ur (Stream f m r), Stream f m r)) r)
 %1 -> m (Either (f (Ur (Stream f m r), Stream f m r)) r))
%1 -> m (Either (f (Ur (Stream f m r), Stream f m r)) r)
%1 -> m (Either (f (Ur (Stream f m r), Stream f m r)) r)
forall a b. (a %1 -> b) %1 -> a %1 -> b
$ (Ur (Stream f m r), Stream f m r)
%1 -> m (Either (f (Ur (Stream f m r), Stream f m r)) r)
forall (f :: * -> *).
Functor f =>
(Ur (Stream f m r), Stream f m r)
%1 -> m (Either (f (Ur (Stream f m r), Stream f m r)) r)
stepStream (Stream f m r -> Ur (Stream f m r)
forall a. a -> Ur a
Ur Stream f m r
s, Stream f m r
s)
      Effect m (Stream f m r)
m ->
        m (Stream f m r)
m m (Stream f m r)
%1 -> (Stream f m r
       %1 -> m (Either (f (Ur (Stream f m r), Stream f m r)) r))
%1 -> m (Either (f (Ur (Stream f m r), Stream f m r)) r)
forall (m :: * -> *) a b.
Monad m =>
m a %1 -> (a %1 -> m b) %1 -> m b
Control.>>= (\Stream f m r
stream -> (Ur (Stream f m r), Stream f m r)
%1 -> m (Either (f (Ur (Stream f m r), Stream f m r)) r)
forall (f :: * -> *).
Functor f =>
(Ur (Stream f m r), Stream f m r)
%1 -> m (Either (f (Ur (Stream f m r), Stream f m r)) r)
stepStream (Stream f m r -> Ur (Stream f m r)
forall a. a -> Ur a
Ur Stream f m r
s, Stream f m r
stream))
      Step f (Stream f m r)
f -> Either (f (Ur (Stream f m r), Stream f m r)) r
%1 -> m (Either (f (Ur (Stream f m r), Stream f m r)) r)
forall (m :: * -> *) a. Monad m => a %1 -> m a
Control.return (Either (f (Ur (Stream f m r), Stream f m r)) r
 %1 -> m (Either (f (Ur (Stream f m r), Stream f m r)) r))
%1 -> Either (f (Ur (Stream f m r), Stream f m r)) r
%1 -> m (Either (f (Ur (Stream f m r), Stream f m r)) r)
forall a b. (a %1 -> b) %1 -> a %1 -> b
$
        f (Ur (Stream f m r), Stream f m r)
%1 -> Either (f (Ur (Stream f m r), Stream f m r)) r
forall a b. a -> Either a b
Left (f (Ur (Stream f m r), Stream f m r)
 %1 -> Either (f (Ur (Stream f m r), Stream f m r)) r)
%1 -> f (Ur (Stream f m r), Stream f m r)
%1 -> Either (f (Ur (Stream f m r), Stream f m r)) r
forall a b. (a %1 -> b) %1 -> a %1 -> b
$ (Stream f m r %1 -> (Ur (Stream f m r), Stream f m r))
%1 -> f (Stream f m r) %1 -> f (Ur (Stream f m r), Stream f m r)
forall (f :: * -> *) a b.
Functor f =>
(a %1 -> b) %1 -> f a %1 -> f b
Control.fmap ((,) (Stream f m r -> Ur (Stream f m r)
forall a. a -> Ur a
Ur Stream f m r
s)) f (Stream f m r)
f

-- | An affine stream iterating an enumerated stream forever.
enumFrom :: (Control.Monad m, Enum e) => e -> AffineStream (Of e) m ()
enumFrom :: forall (m :: * -> *) e.
(Monad m, Enum e) =>
e -> AffineStream (Of e) m ()
enumFrom e
e = e -> (e -> e) -> AffineStream (Of e) m ()
forall a (m :: * -> *).
Monad m =>
a -> (a -> a) -> AffineStream (Of a) m ()
iterate e
e e -> e
forall a. Enum a => a -> a
Prelude.succ

-- | An affine stream iterating an enumerated stream forever, using the
-- first two elements to determine the gap to skip by.
-- E.g., @enumFromThen  3 5@ is like @[3,5..]@.
enumFromThen :: forall e m. (Control.Monad m, Enum e) =>
  e -> e -> AffineStream (Of e) m ()
enumFromThen :: forall e (m :: * -> *).
(Monad m, Enum e) =>
e -> e -> AffineStream (Of e) m ()
enumFromThen e
e e
e' = e -> (e -> e) -> AffineStream (Of e) m ()
forall a (m :: * -> *).
Monad m =>
a -> (a -> a) -> AffineStream (Of a) m ()
iterate e
e e -> e
enumStep where
  enumStep :: e -> e
  enumStep :: e -> e
enumStep e
enum = Int -> e
forall a. Enum a => Int -> a
toEnum (Int -> e) -> Int -> e
forall a b. (a -> b) -> a -> b
Prelude.$
    (e -> Int
forall a. Enum a => a -> Int
fromEnum e
enum) Int -> Int -> Int
forall a. Num a => a -> a -> a
+ ((e -> Int
forall a. Enum a => a -> Int
fromEnum e
e') Int -> Int -> Int
forall a. Num a => a -> a -> a
- (e -> Int
forall a. Enum a => a -> Int
fromEnum e
e))
    -- Think:  \enum -> enum + stepSize where stepSize = (e1 - e0)


-- # Working with infinite 'Stream's
-------------------------------------------------------------------------------

-- | @stdinLnN n@ is a stream of @n@ lines from standard input
stdinLnN :: Int -> Stream (Of Text) IO ()
stdinLnN :: Int -> Stream (Of Text) IO ()
stdinLnN Int
n = Int -> AffineStream (Of Text) IO () %1 -> Stream (Of Text) IO ()
forall (f :: * -> *) (m :: * -> *) r.
(Monad m, Functor f) =>
Int -> AffineStream f m r %1 -> Stream f m r
take Int
n AffineStream (Of Text) IO ()
stdinLn
{-# INLINE stdinLnN #-}

-- | Provides a stream of standard input and omits the first line
-- that satisfies the predicate, possibly requiring IO
stdinLnUntilM :: (Text -> IO Bool) -> Stream (Of Text) IO ()
stdinLnUntilM :: (Text -> IO Bool) -> Stream (Of Text) IO ()
stdinLnUntilM Text -> IO Bool
test = (Text -> IO Bool)
-> AffineStream (Of Text) IO () %1 -> Stream (Of Text) IO ()
forall a (m :: * -> *) r.
Monad m =>
(a -> m Bool) -> AffineStream (Of a) m r %1 -> Stream (Of a) m r
untilM Text -> IO Bool
test AffineStream (Of Text) IO ()
stdinLn
{-# INLINE stdinLnUntilM #-}

-- | Provides a stream of standard input and omits the first line
-- that satisfies the predicate
stdinLnUntil :: (Text -> Bool) -> Stream (Of Text) IO ()
stdinLnUntil :: (Text -> Bool) -> Stream (Of Text) IO ()
stdinLnUntil Text -> Bool
test = (Text -> Bool)
-> AffineStream (Of Text) IO () %1 -> Stream (Of Text) IO ()
forall a (m :: * -> *) r.
Monad m =>
(a -> Bool) -> AffineStream (Of a) m r %1 -> Stream (Of a) m r
until Text -> Bool
test AffineStream (Of Text) IO ()
stdinLn
{-# INLINE stdinLnUntil #-}

-- | Given a finite stream, provide a stream of lines of standard input
-- zipped with that finite stream
stdinLnZip :: Stream (Of x) IO r %1-> Stream (Of (x, Text)) IO r
stdinLnZip :: forall x r. Stream (Of x) IO r %1 -> Stream (Of (x, Text)) IO r
stdinLnZip Stream (Of x) IO r
stream = ((r, ()) %1 -> r)
%1 -> Stream (Of (x, Text)) IO (r, ())
%1 -> Stream (Of (x, Text)) IO r
forall (f :: * -> *) a b.
Functor f =>
(a %1 -> b) %1 -> f a %1 -> f b
Control.fmap (\(r
r,()) -> r
r) (Stream (Of (x, Text)) IO (r, ()) %1 -> Stream (Of (x, Text)) IO r)
%1 -> Stream (Of (x, Text)) IO (r, ())
%1 -> Stream (Of (x, Text)) IO r
forall a b. (a %1 -> b) %1 -> a %1 -> b
$ Stream (Of x) IO r
%1 -> AffineStream (Of Text) IO ()
%1 -> Stream (Of (x, Text)) IO (r, ())
forall a x (m :: * -> *) r1 r2.
Monad m =>
Stream (Of x) m r1
%1 -> AffineStream (Of a) m r2 %1 -> Stream (Of (x, a)) m (r1, r2)
zip Stream (Of x) IO r
stream AffineStream (Of Text) IO ()
stdinLn
{-# INLINE stdinLnZip #-}

readLnN :: Read a => Int -> Stream (Of a) IO ()
readLnN :: forall a. Read a => Int -> Stream (Of a) IO ()
readLnN Int
n = Int -> AffineStream (Of a) IO () %1 -> Stream (Of a) IO ()
forall (f :: * -> *) (m :: * -> *) r.
(Monad m, Functor f) =>
Int -> AffineStream f m r %1 -> Stream f m r
take Int
n AffineStream (Of a) IO ()
forall a. Read a => AffineStream (Of a) IO ()
readLn
{-# INLINE readLnN #-}

readLnUntilM :: Read a => (a -> IO Bool) -> Stream (Of a) IO ()
readLnUntilM :: forall a. Read a => (a -> IO Bool) -> Stream (Of a) IO ()
readLnUntilM a -> IO Bool
test = (a -> IO Bool)
-> AffineStream (Of a) IO () %1 -> Stream (Of a) IO ()
forall a (m :: * -> *) r.
Monad m =>
(a -> m Bool) -> AffineStream (Of a) m r %1 -> Stream (Of a) m r
untilM a -> IO Bool
test AffineStream (Of a) IO ()
forall a. Read a => AffineStream (Of a) IO ()
readLn
{-# INLINE readLnUntilM #-}

readLnUntil :: Read a => (a -> Bool) -> Stream (Of a) IO ()
readLnUntil :: forall a. Read a => (a -> Bool) -> Stream (Of a) IO ()
readLnUntil a -> Bool
test = (a -> Bool) -> AffineStream (Of a) IO () %1 -> Stream (Of a) IO ()
forall a (m :: * -> *) r.
Monad m =>
(a -> Bool) -> AffineStream (Of a) m r %1 -> Stream (Of a) m r
until a -> Bool
test AffineStream (Of a) IO ()
forall a. Read a => AffineStream (Of a) IO ()
readLn
{-# INLINE readLnUntil #-}

readLnZip :: Read a => Stream (Of x) IO r %1-> Stream (Of (x, a)) IO r
readLnZip :: forall a x r.
Read a =>
Stream (Of x) IO r %1 -> Stream (Of (x, a)) IO r
readLnZip Stream (Of x) IO r
stream = ((r, ()) %1 -> r)
%1 -> Stream (Of (x, a)) IO (r, ()) %1 -> Stream (Of (x, a)) IO r
forall (f :: * -> *) a b.
Functor f =>
(a %1 -> b) %1 -> f a %1 -> f b
Control.fmap (\(r
r,()) -> r
r) (Stream (Of (x, a)) IO (r, ()) %1 -> Stream (Of (x, a)) IO r)
%1 -> Stream (Of (x, a)) IO (r, ()) %1 -> Stream (Of (x, a)) IO r
forall a b. (a %1 -> b) %1 -> a %1 -> b
$ Stream (Of x) IO r
%1 -> AffineStream (Of a) IO () %1 -> Stream (Of (x, a)) IO (r, ())
forall a x (m :: * -> *) r1 r2.
Monad m =>
Stream (Of x) m r1
%1 -> AffineStream (Of a) m r2 %1 -> Stream (Of (x, a)) m (r1, r2)
zip Stream (Of x) IO r
stream AffineStream (Of a) IO ()
forall a. Read a => AffineStream (Of a) IO ()
readLn
{-# INLINE readLnZip #-}

-- | Iterate a pure function from a seed value,
-- streaming the results forever.
iterateN :: Control.Monad m => Int -> (a -> a) -> a -> Stream (Of a) m ()
iterateN :: forall (m :: * -> *) a.
Monad m =>
Int -> (a -> a) -> a -> Stream (Of a) m ()
iterateN Int
n a -> a
step a
a = Int -> AffineStream (Of a) m () %1 -> Stream (Of a) m ()
forall (f :: * -> *) (m :: * -> *) r.
(Monad m, Functor f) =>
Int -> AffineStream f m r %1 -> Stream f m r
take Int
n (AffineStream (Of a) m () %1 -> Stream (Of a) m ())
%1 -> AffineStream (Of a) m () %1 -> Stream (Of a) m ()
forall a b. (a %1 -> b) %1 -> a %1 -> b
$ a -> (a -> a) -> AffineStream (Of a) m ()
forall a (m :: * -> *).
Monad m =>
a -> (a -> a) -> AffineStream (Of a) m ()
iterate a
a a -> a
step
{-# INLINE iterateN #-}

iterateZip :: Control.Monad m => Stream (Of x) m r ->
  (a -> a) -> a -> Stream (Of (x,a)) m r
iterateZip :: forall (m :: * -> *) x r a.
Monad m =>
Stream (Of x) m r -> (a -> a) -> a -> Stream (Of (x, a)) m r
iterateZip Stream (Of x) m r
stream a -> a
step a
a =
  ((r, ()) %1 -> r)
%1 -> Stream (Of (x, a)) m (r, ()) %1 -> Stream (Of (x, a)) m r
forall (f :: * -> *) a b.
Functor f =>
(a %1 -> b) %1 -> f a %1 -> f b
Control.fmap (\(r
r,()) -> r
r) (Stream (Of (x, a)) m (r, ()) %1 -> Stream (Of (x, a)) m r)
%1 -> Stream (Of (x, a)) m (r, ()) %1 -> Stream (Of (x, a)) m r
forall a b. (a %1 -> b) %1 -> a %1 -> b
$ Stream (Of x) m r
%1 -> AffineStream (Of a) m () %1 -> Stream (Of (x, a)) m (r, ())
forall a x (m :: * -> *) r1 r2.
Monad m =>
Stream (Of x) m r1
%1 -> AffineStream (Of a) m r2 %1 -> Stream (Of (x, a)) m (r1, r2)
zip Stream (Of x) m r
stream (AffineStream (Of a) m () %1 -> Stream (Of (x, a)) m (r, ()))
%1 -> AffineStream (Of a) m () %1 -> Stream (Of (x, a)) m (r, ())
forall a b. (a %1 -> b) %1 -> a %1 -> b
$ a -> (a -> a) -> AffineStream (Of a) m ()
forall a (m :: * -> *).
Monad m =>
a -> (a -> a) -> AffineStream (Of a) m ()
iterate a
a a -> a
step
{-# INLINE iterateZip #-}

-- | Iterate a monadic function from a seed value,
-- streaming the results forever.
iterateMN :: Control.Monad m =>
  Int -> (a -> m (Ur a)) -> m (Ur a) -> Stream (Of a) m ()
iterateMN :: forall (m :: * -> *) a.
Monad m =>
Int -> (a -> m (Ur a)) -> m (Ur a) -> Stream (Of a) m ()
iterateMN Int
n a -> m (Ur a)
step m (Ur a)
ma = Int -> AffineStream (Of a) m () %1 -> Stream (Of a) m ()
forall (f :: * -> *) (m :: * -> *) r.
(Monad m, Functor f) =>
Int -> AffineStream f m r %1 -> Stream f m r
take Int
n (AffineStream (Of a) m () %1 -> Stream (Of a) m ())
%1 -> AffineStream (Of a) m () %1 -> Stream (Of a) m ()
forall a b. (a %1 -> b) %1 -> a %1 -> b
$ m (Ur a) -> (a -> m (Ur a)) -> AffineStream (Of a) m ()
forall a (m :: * -> *).
Monad m =>
m (Ur a) -> (a -> m (Ur a)) -> AffineStream (Of a) m ()
iterateM m (Ur a)
ma a -> m (Ur a)
step
{-# INLINE iterateMN #-}

iterateMZip :: Control.Monad m =>
  Stream (Of x) m r %1->
  (a -> m (Ur a)) -> m (Ur a) -> Stream (Of (x,a)) m r
iterateMZip :: forall (m :: * -> *) x r a.
Monad m =>
Stream (Of x) m r
%1 -> (a -> m (Ur a)) -> m (Ur a) -> Stream (Of (x, a)) m r
iterateMZip Stream (Of x) m r
stream a -> m (Ur a)
step m (Ur a)
ma =
  ((r, ()) %1 -> r)
%1 -> Stream (Of (x, a)) m (r, ()) %1 -> Stream (Of (x, a)) m r
forall (f :: * -> *) a b.
Functor f =>
(a %1 -> b) %1 -> f a %1 -> f b
Control.fmap (\(r
r,()) -> r
r) (Stream (Of (x, a)) m (r, ()) %1 -> Stream (Of (x, a)) m r)
%1 -> Stream (Of (x, a)) m (r, ()) %1 -> Stream (Of (x, a)) m r
forall a b. (a %1 -> b) %1 -> a %1 -> b
$ Stream (Of x) m r
%1 -> AffineStream (Of a) m () %1 -> Stream (Of (x, a)) m (r, ())
forall a x (m :: * -> *) r1 r2.
Monad m =>
Stream (Of x) m r1
%1 -> AffineStream (Of a) m r2 %1 -> Stream (Of (x, a)) m (r1, r2)
zip Stream (Of x) m r
stream (AffineStream (Of a) m () %1 -> Stream (Of (x, a)) m (r, ()))
%1 -> AffineStream (Of a) m () %1 -> Stream (Of (x, a)) m (r, ())
forall a b. (a %1 -> b) %1 -> a %1 -> b
$ m (Ur a) -> (a -> m (Ur a)) -> AffineStream (Of a) m ()
forall a (m :: * -> *).
Monad m =>
m (Ur a) -> (a -> m (Ur a)) -> AffineStream (Of a) m ()
iterateM m (Ur a)
ma a -> m (Ur a)
step
{-# INLINE iterateMZip #-}

-- | Cycle a stream a finite number of times
cycleN :: (Control.Monad m, Consumable r) =>
  Int -> Stream (Of a) m r -> Stream (Of a) m r
cycleN :: forall (m :: * -> *) r a.
(Monad m, Consumable r) =>
Int -> Stream (Of a) m r -> Stream (Of a) m r
cycleN Int
n Stream (Of a) m r
stream = Int -> AffineStream (Of a) m r %1 -> Stream (Of a) m r
forall (f :: * -> *) (m :: * -> *) r.
(Monad m, Functor f) =>
Int -> AffineStream f m r %1 -> Stream f m r
take Int
n (AffineStream (Of a) m r %1 -> Stream (Of a) m r)
%1 -> AffineStream (Of a) m r %1 -> Stream (Of a) m r
forall a b. (a %1 -> b) %1 -> a %1 -> b
$ Stream (Of a) m r -> AffineStream (Of a) m r
forall a (m :: * -> *) r.
(Monad m, Consumable r) =>
Stream (Of a) m r -> AffineStream (Of a) m r
cycle Stream (Of a) m r
stream
{-# INLINE cycleN #-}

-- | @cycleZip s1 s2@ will cycle @s2@ just enough to zip with the given finite
-- stream @s1@. Note that we consume all the effects of the remainder of the
-- cycled stream @s2@. That is, we consume @s2@ the smallest natural number of
-- times we need to zip.
cycleZip :: (Control.Monad m, Consumable s) =>
  Stream (Of a) m r %1-> Stream (Of b) m s -> Stream (Of (a,b)) m (r,s)
cycleZip :: forall (m :: * -> *) s a r b.
(Monad m, Consumable s) =>
Stream (Of a) m r
%1 -> Stream (Of b) m s -> Stream (Of (a, b)) m (r, s)
cycleZip Stream (Of a) m r
str Stream (Of b) m s
stream = Stream (Of a) m r
%1 -> AffineStream (Of b) m s %1 -> Stream (Of (a, b)) m (r, s)
forall a x (m :: * -> *) r1 r2.
Monad m =>
Stream (Of x) m r1
%1 -> AffineStream (Of a) m r2 %1 -> Stream (Of (x, a)) m (r1, r2)
zip Stream (Of a) m r
str (AffineStream (Of b) m s %1 -> Stream (Of (a, b)) m (r, s))
%1 -> AffineStream (Of b) m s %1 -> Stream (Of (a, b)) m (r, s)
forall a b. (a %1 -> b) %1 -> a %1 -> b
$ Stream (Of b) m s -> AffineStream (Of b) m s
forall a (m :: * -> *) r.
(Monad m, Consumable r) =>
Stream (Of a) m r -> AffineStream (Of a) m r
cycle Stream (Of b) m s
stream
{-# INLINE cycleZip #-}

{-| An finite sequence of enumerable values at a fixed distance, determined
   by the first and second values.

@
\>\>\> S.print $ S.enumFromThenN 3 100 200
100
200
300
@
-}
enumFromThenN :: (Control.Monad m, Enum e) => Int -> e -> e -> Stream (Of e) m ()
enumFromThenN :: forall (m :: * -> *) e.
(Monad m, Enum e) =>
Int -> e -> e -> Stream (Of e) m ()
enumFromThenN Int
n e
e e
e' = Int -> AffineStream (Of e) m () %1 -> Stream (Of e) m ()
forall (f :: * -> *) (m :: * -> *) r.
(Monad m, Functor f) =>
Int -> AffineStream f m r %1 -> Stream f m r
take Int
n (AffineStream (Of e) m () %1 -> Stream (Of e) m ())
%1 -> AffineStream (Of e) m () %1 -> Stream (Of e) m ()
forall a b. (a %1 -> b) %1 -> a %1 -> b
$ e -> e -> AffineStream (Of e) m ()
forall e (m :: * -> *).
(Monad m, Enum e) =>
e -> e -> AffineStream (Of e) m ()
enumFromThen e
e e
e'
{-# INLINE enumFromThenN #-}

-- | A finite sequence of enumerable values at a fixed distance determined
-- by the first and second values. The length is limited by zipping
-- with a given finite stream, i.e., the first argument.
enumFromThenZip :: (Control.Monad m, Enum e) =>
  Stream (Of a) m r %1-> e -> e -> Stream (Of (a,e)) m r
enumFromThenZip :: forall (m :: * -> *) e a r.
(Monad m, Enum e) =>
Stream (Of a) m r %1 -> e -> e -> Stream (Of (a, e)) m r
enumFromThenZip Stream (Of a) m r
stream e
e e
e'=
  ((r, ()) %1 -> r)
%1 -> Stream (Of (a, e)) m (r, ()) %1 -> Stream (Of (a, e)) m r
forall (f :: * -> *) a b.
Functor f =>
(a %1 -> b) %1 -> f a %1 -> f b
Control.fmap (\(r
r,()) -> r
r) (Stream (Of (a, e)) m (r, ()) %1 -> Stream (Of (a, e)) m r)
%1 -> Stream (Of (a, e)) m (r, ()) %1 -> Stream (Of (a, e)) m r
forall a b. (a %1 -> b) %1 -> a %1 -> b
$ Stream (Of a) m r
%1 -> AffineStream (Of e) m () %1 -> Stream (Of (a, e)) m (r, ())
forall a x (m :: * -> *) r1 r2.
Monad m =>
Stream (Of x) m r1
%1 -> AffineStream (Of a) m r2 %1 -> Stream (Of (x, a)) m (r1, r2)
zip Stream (Of a) m r
stream (AffineStream (Of e) m () %1 -> Stream (Of (a, e)) m (r, ()))
%1 -> AffineStream (Of e) m () %1 -> Stream (Of (a, e)) m (r, ())
forall a b. (a %1 -> b) %1 -> a %1 -> b
$ e -> e -> AffineStream (Of e) m ()
forall e (m :: * -> *).
(Monad m, Enum e) =>
e -> e -> AffineStream (Of e) m ()
enumFromThen e
e e
e'
{-# INLINE enumFromThenZip #-}

-- | Like 'enumFromThenN' but where the next element in the enumeration is just
-- the successor @succ n@ for a given enum @n@.
enumFromN :: (Control.Monad m, Enum e) => Int -> e -> Stream (Of e) m ()
enumFromN :: forall (m :: * -> *) e.
(Monad m, Enum e) =>
Int -> e -> Stream (Of e) m ()
enumFromN Int
n e
e = Int -> AffineStream (Of e) m () %1 -> Stream (Of e) m ()
forall (f :: * -> *) (m :: * -> *) r.
(Monad m, Functor f) =>
Int -> AffineStream f m r %1 -> Stream f m r
take Int
n (AffineStream (Of e) m () %1 -> Stream (Of e) m ())
%1 -> AffineStream (Of e) m () %1 -> Stream (Of e) m ()
forall a b. (a %1 -> b) %1 -> a %1 -> b
$ e -> AffineStream (Of e) m ()
forall (m :: * -> *) e.
(Monad m, Enum e) =>
e -> AffineStream (Of e) m ()
enumFrom e
e
{-# INLINE enumFromN #-}

-- | Like 'enumFromThenZip' but where the next element in the enumeration is just
-- the successor @succ n@ for a given enum @n@.
enumFromZip :: (Control.Monad m, Enum e) =>
  Stream (Of a) m r %1-> e -> Stream (Of (a,e)) m r
enumFromZip :: forall (m :: * -> *) e a r.
(Monad m, Enum e) =>
Stream (Of a) m r %1 -> e -> Stream (Of (a, e)) m r
enumFromZip Stream (Of a) m r
str e
e =
  ((r, ()) %1 -> r)
%1 -> Stream (Of (a, e)) m (r, ()) %1 -> Stream (Of (a, e)) m r
forall (f :: * -> *) a b.
Functor f =>
(a %1 -> b) %1 -> f a %1 -> f b
Control.fmap (\(r
r,()) -> r
r) (Stream (Of (a, e)) m (r, ()) %1 -> Stream (Of (a, e)) m r)
%1 -> Stream (Of (a, e)) m (r, ()) %1 -> Stream (Of (a, e)) m r
forall a b. (a %1 -> b) %1 -> a %1 -> b
$ Stream (Of a) m r
%1 -> AffineStream (Of e) m () %1 -> Stream (Of (a, e)) m (r, ())
forall a x (m :: * -> *) r1 r2.
Monad m =>
Stream (Of x) m r1
%1 -> AffineStream (Of a) m r2 %1 -> Stream (Of (x, a)) m (r1, r2)
zip Stream (Of a) m r
str (AffineStream (Of e) m () %1 -> Stream (Of (a, e)) m (r, ()))
%1 -> AffineStream (Of e) m () %1 -> Stream (Of (a, e)) m (r, ())
forall a b. (a %1 -> b) %1 -> a %1 -> b
$ e -> AffineStream (Of e) m ()
forall (m :: * -> *) e.
(Monad m, Enum e) =>
e -> AffineStream (Of e) m ()
enumFrom e
e
{-# INLINE enumFromZip #-}