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

-- | This module provides all functions which produce a
-- 'Stream (Of a) m r' from some given non-stream inputs.
module Streaming.Linear.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 qualified Control.Functor.Linear as Control
import Data.Text (Text)
import qualified Data.Text as Text
import Data.Unrestricted.Linear
import GHC.Stack
import Prelude.Linear (($), (&))
import Streaming.Linear.Internal.Consume (effects)
import Streaming.Linear.Internal.Process
import Streaming.Linear.Internal.Type
import qualified System.IO as System
import System.IO.Linear
import System.IO.Resource.Linear
import Prelude
  ( Bool (..),
    Either (..),
    Enum,
    Eq (..),
    FilePath,
    Int,
    Num (..),
    Ord (..),
    Read,
    fromEnum,
    otherwise,
    toEnum,
  )
import qualified Prelude

-- # 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 = forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ a
x forall a b. a -> b -> Of a b
:> 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 = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
Prelude.foldr (\a
a Stream (Of a) m ()
stream -> forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ a
a forall a b. a -> b -> Of a b
:> Stream (Of a) m ()
stream) (forall r (f :: * -> *) (m :: * -> *). r -> Stream f m r
Return ()) [a]
xs
{-# INLINEABLE 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 = 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 =
      forall (m :: * -> *) (f :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$
        s %1 -> m (Either r (Ur a, s))
step s
s forall (m :: * -> *) a b.
Monad m =>
m a %1 -> (a %1 -> m b) %1 -> m b
Control.>>= \case
          Left r
r -> forall (m :: * -> *) a. Monad m => a %1 -> m a
Control.return forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ forall r (f :: * -> *) (m :: * -> *). r -> Stream f m r
Return r
r
          Right (Ur a
a, s
s') ->
            forall (m :: * -> *) a. Monad m => a %1 -> m a
Control.return forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ a
a forall a b. a -> b -> Of a b
:> 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'
{-# INLINEABLE 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') <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a %1 -> t m a
Control.lift forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ Handle %1 -> RIO (Ur Bool, Handle)
hIsEOF Handle
h
      case Bool
isEOF of
        Bool
True -> Control.do
          forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a %1 -> t m a
Control.lift forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ Handle %1 -> RIO ()
hClose Handle
h'
          forall (m :: * -> *) a. Monad m => a %1 -> m a
Control.return ()
        Bool
False -> Control.do
          (Ur Text
text, Handle
h'') <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a %1 -> t m a
Control.lift forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ Handle %1 -> RIO (Ur Text, Handle)
hGetLine Handle
h'
          forall (m :: * -> *) a. Monad m => a -> Stream (Of a) m ()
yield Text
text
          Handle %1 -> Stream (Of Text) RIO ()
fromHandle Handle
h''
{-# INLINEABLE 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 <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a %1 -> t m a
Control.lift forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> 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 forall a. Ord a => a -> a -> Bool
< Int
0 = forall a. HasCallStack => FilePath -> a
Prelude.error FilePath
"Cannot replicate a stream of negative length"
  | Bool
otherwise = 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 forall a. Eq a => a -> a -> Bool
== Int
0 = forall r (f :: * -> *) (m :: * -> *). r -> Stream f m r
Return ()
      | Bool
otherwise = forall (m :: * -> *) (f :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ forall (m :: * -> *) a. Monad m => a %1 -> m a
Control.return forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ a
a forall a b. a -> b -> Of a b
:> forall (m :: * -> *) a. Monad m => Int -> a -> Stream (Of a) m ()
loop (Int
n forall a. Num a => a -> a -> a
- Int
1) a
a
{-# INLINEABLE 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 forall a. Ord a => a -> a -> Bool
< Int
0 = forall a. HasCallStack => FilePath -> a
Prelude.error FilePath
"Cannot replicate a stream of negative length"
  | Bool
otherwise = 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 forall a. Eq a => a -> a -> Bool
== Int
0 = forall r (f :: * -> *) (m :: * -> *). r -> Stream f m r
Return ()
      | Bool
otherwise = forall (m :: * -> *) (f :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ Control.do
          Ur a
a <- m (Ur a)
ma
          forall (m :: * -> *) a. Monad m => a %1 -> m a
Control.return forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ a
a forall a b. a -> b -> Of a b
:> (forall (m :: * -> *) a.
Monad m =>
Int -> m (Ur a) -> Stream (Of a) m ()
replicateM (Int
n 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 = 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
{-# INLINEABLE 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 = 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 forall a b (p :: Multiplicity) (q :: Multiplicity).
a %p -> (a %p -> b) %q -> b
& \case
        Left (Ur a
a) ->
          forall (m :: * -> *) a. Monad m => a %1 -> m a
Control.return forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ a
a forall a b. a -> b -> Of a b
:> (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 -> forall (m :: * -> *) a. Monad m => a %1 -> m a
Control.return forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ forall r (f :: * -> *) (m :: * -> *). r -> Stream f m r
Return r
r
{-# INLINEABLE 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 forall a. Ord a => a -> a -> Bool
<= Int
0 = forall (m :: * -> *) (f :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ forall (f :: * -> *) a b.
Functor f =>
(a %1 -> b) %1 -> f a %1 -> f b
Control.fmap forall (m :: * -> *) a. Monad m => a %1 -> m a
Control.return forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ x %1 -> m r
end x
s
      | Bool
otherwise = forall (m :: * -> *) (f :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ Control.do
          Either (f x) r
next <- x %1 -> m (Either (f x) r)
step x
s
          Either (f x) r
next forall a b (p :: Multiplicity) (q :: Multiplicity).
a %p -> (a %p -> b) %q -> b
& \case
            Right r
r -> forall (m :: * -> *) a. Monad m => a %1 -> m a
Control.return (forall r (f :: * -> *) (m :: * -> *). r -> Stream f m r
Return r
r)
            Left f x
fx ->
              forall (m :: * -> *) a. Monad m => a %1 -> m a
Control.return forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$
                forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$
                  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
n forall a. Num a => a -> a -> a
- Int
1) (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
{-# INLINEABLE 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) = forall (m :: * -> *) (f :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> 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 forall a b (p :: Multiplicity) (q :: Multiplicity).
a %p -> (a %p -> b) %q -> b
& \case
        Right r
r -> forall (m :: * -> *) a. Monad m => a %1 -> m a
Control.return (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 forall a b (p :: Multiplicity) (q :: Multiplicity).
a %p -> (a %p -> b) %q -> b
& \case
            Bool
False ->
              forall (m :: * -> *) a. Monad m => a %1 -> m a
Control.return forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$
                forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$
                  a
a 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 (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 -> forall (f :: * -> *) a b.
Functor f =>
(a %1 -> b) %1 -> f a %1 -> f b
Control.fmap forall (m :: * -> *) a. Monad m => a %1 -> m a
Control.return forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ x %1 -> m r
end x
next
{-# INLINEABLE 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) = forall (m :: * -> *) (f :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> 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 forall a b (p :: Multiplicity) (q :: Multiplicity).
a %p -> (a %p -> b) %q -> b
& \case
        Right r
r -> forall (m :: * -> *) a. Monad m => a %1 -> m a
Control.return (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 -> forall (f :: * -> *) a b.
Functor f =>
(a %1 -> b) %1 -> f a %1 -> f b
Control.fmap forall (m :: * -> *) a. Monad m => a %1 -> m a
Control.return forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ x %1 -> m r
end x
next
          Bool
False ->
            forall (m :: * -> *) a. Monad m => a %1 -> m a
Control.return forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$
              forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$
                a
a 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 (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)
{-# INLINEABLE 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 forall a b (p :: Multiplicity) (q :: Multiplicity).
a %p -> (a %p -> b) %q -> b
& \case
        Return r1
r1 ->
          forall (m :: * -> *) (f :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$
            forall (f :: * -> *) a b.
Functor f =>
(a %1 -> b) %1 -> f a %1 -> f b
Control.fmap (\r2
r2 -> forall (m :: * -> *) a. Monad m => a %1 -> m a
Control.return forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ (r1
r1, r2
r2)) forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$
              x %1 -> m r2
end x
s
        Effect m (Stream (Of x) m r1)
m ->
          forall (m :: * -> *) (f :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$
            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 (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) -> forall (m :: * -> *) (f :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> 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 forall a b (p :: Multiplicity) (q :: Multiplicity).
a %p -> (a %p -> b) %q -> b
& \case
            Right r2
r2 -> Control.do
              r1
r1 <- forall a (m :: * -> *) r. Monad m => Stream (Of a) m r %1 -> m r
effects Stream (Of x) m r1
rest
              forall (m :: * -> *) a. Monad m => a %1 -> m a
Control.return (forall r (f :: * -> *) (m :: * -> *). r -> Stream f m r
Return (r1
r1, r2
r2))
            Left (a
a :> x
rest') ->
              forall (m :: * -> *) a. Monad m => a %1 -> m a
Control.return forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$
                forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$
                  (x
x, a
a) 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 (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)
{-# INLINEABLE zip #-}

-- | An affine stream of standard input lines.
stdinLn :: AffineStream (Of Text) IO ()
stdinLn :: AffineStream (Of Text) IO ()
stdinLn = 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 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 <- forall a. IO a -> IO (Ur a)
fromSystemIOU IO FilePath
System.getLine
      forall (m :: * -> *) a. Monad m => a %1 -> m a
Control.return forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ forall a b. a -> Either a b
Left (FilePath -> Text
Text.pack FilePath
line 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 = forall x (m :: * -> *) (f :: * -> *) r.
x
-> (x %1 -> m (Either (f x) r))
-> (x %1 -> m r)
-> AffineStream f m r
AffineStream () forall a. Read a => () %1 -> IO (Either (Of a ()) ())
readALine 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 <- forall a. IO a -> IO (Ur a)
fromSystemIOU IO FilePath
System.getLine
      forall (m :: * -> *) a. Monad m => a %1 -> m a
Control.return forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ forall a b. a -> Either a b
Left (forall a. Read a => FilePath -> a
Prelude.read FilePath
line 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 =
  forall x (m :: * -> *) (f :: * -> *) r.
x
-> (x %1 -> m (Either (f x) r))
-> (x %1 -> m r)
-> AffineStream f m r
AffineStream (forall a. a -> Ur a
Ur a
a) Ur a %1 -> m (Either (Of a (Ur a)) ())
stepper (\Ur a
x -> forall (m :: * -> *) a. Monad m => a %1 -> m a
Control.return forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ 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) =
      forall (m :: * -> *) a. Monad m => a %1 -> m a
Control.return forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$
        forall a b. a -> Either a b
Left forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$
          a
a forall a b. a -> b -> Of a b
:> 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 =
  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 (forall (f :: * -> *) a b.
Functor f =>
(a %1 -> b) %1 -> f a %1 -> f b
Control.fmap 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
      forall (m :: * -> *) a. Monad m => a %1 -> m a
Control.return forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ forall a b. a -> Either a b
Left forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ a
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)
  forall x (m :: * -> *) (f :: * -> *) r.
x
-> (x %1 -> m (Either (f x) r))
-> (x %1 -> m r)
-> AffineStream f m r
AffineStream (forall a. a -> Ur a
Ur Stream (Of a) m r
stream, Stream (Of a) m r
stream) 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) = 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 forall a b (p :: Multiplicity) (q :: Multiplicity).
a %p -> (a %p -> b) %q -> b
& \case
        Return r
r -> forall a b. Consumable a => a %1 -> b %1 -> b
lseq r
r forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ 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 (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 forall (m :: * -> *) a b.
Monad m =>
m a %1 -> (a %1 -> m b) %1 -> m b
Control.>>= (\Stream f m r
stream -> 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 (forall a. a -> Ur a
Ur Stream f m r
s, Stream f m r
stream))
        Step f (Stream f m r)
f ->
          forall (m :: * -> *) a. Monad m => a %1 -> m a
Control.return forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$
            forall a b. a -> Either a b
Left forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$
              forall (f :: * -> *) a b.
Functor f =>
(a %1 -> b) %1 -> f a %1 -> f b
Control.fmap ((,) (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 = forall a (m :: * -> *).
Monad m =>
a -> (a -> a) -> AffineStream (Of a) m ()
iterate 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' = 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 =
      forall a. Enum a => Int -> a
toEnum forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$
        (forall a. Enum a => a -> Int
fromEnum e
enum) forall a. Num a => a -> a -> a
+ ((forall a. Enum a => a -> Int
fromEnum e
e') forall a. Num a => a -> a -> a
- (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 = 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 = 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 = 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 = forall (f :: * -> *) a b.
Functor f =>
(a %1 -> b) %1 -> f a %1 -> f b
Control.fmap (\(r
r, ()) -> r
r) forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ 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 = forall (f :: * -> *) (m :: * -> *) r.
(Monad m, Functor f) =>
Int -> AffineStream f m r %1 -> Stream f m r
take Int
n 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 = 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 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 = forall a (m :: * -> *) r.
Monad m =>
(a -> Bool) -> AffineStream (Of a) m r %1 -> Stream (Of a) m r
until a -> Bool
test 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 = forall (f :: * -> *) a b.
Functor f =>
(a %1 -> b) %1 -> f a %1 -> f b
Control.fmap (\(r
r, ()) -> r
r) forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ 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 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 = forall (f :: * -> *) (m :: * -> *) r.
(Monad m, Functor f) =>
Int -> AffineStream f m r %1 -> Stream f m r
take Int
n forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ 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 =
  forall (f :: * -> *) a b.
Functor f =>
(a %1 -> b) %1 -> f a %1 -> f b
Control.fmap (\(r
r, ()) -> r
r) forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ 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 forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ 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 = forall (f :: * -> *) (m :: * -> *) r.
(Monad m, Functor f) =>
Int -> AffineStream f m r %1 -> Stream f m r
take Int
n forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ 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 =
  forall (f :: * -> *) a b.
Functor f =>
(a %1 -> b) %1 -> f a %1 -> f b
Control.fmap (\(r
r, ()) -> r
r) forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ 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 forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ 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 = forall (f :: * -> *) (m :: * -> *) r.
(Monad m, Functor f) =>
Int -> AffineStream f m r %1 -> Stream f m r
take Int
n forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ 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 = 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 forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ 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' = forall (f :: * -> *) (m :: * -> *) r.
(Monad m, Functor f) =>
Int -> AffineStream f m r %1 -> Stream f m r
take Int
n forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ 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' =
  forall (f :: * -> *) a b.
Functor f =>
(a %1 -> b) %1 -> f a %1 -> f b
Control.fmap (\(r
r, ()) -> r
r) forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ 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 forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ 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 = forall (f :: * -> *) (m :: * -> *) r.
(Monad m, Functor f) =>
Int -> AffineStream f m r %1 -> Stream f m r
take Int
n forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ 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 =
  forall (f :: * -> *) a b.
Functor f =>
(a %1 -> b) %1 -> f a %1 -> f b
Control.fmap (\(r
r, ()) -> r
r) forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ 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 forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ forall (m :: * -> *) e.
(Monad m, Enum e) =>
e -> AffineStream (Of e) m ()
enumFrom e
e
{-# INLINE enumFromZip #-}