HORIZON HASKELLDocslts/ghc-10.0.x8fef28d2026-11-02Search packagesCtrl K
Theme: |

GHC 10.0.0.20260917 · lts/ghc-10.0.x · 8fef28d · 2026-11-02

Moduleio-streams-1.5.2.2Haskell2010

System.IO.Streams.Internal

Internal implementation of the io-streams library, intended for library writers

Library users should use the interface provided by System.IO.Streams

Contents
  1. Types
  2. About pushback
  3. Input and output streams
  4. Primitive stream operations
  5. Building streams
  6. Connecting streams
  7. Thread safety
  8. Utility streams
  9. Generator monad
  10. Consumer monad
Synopsis

Types

About pushback

Users can push a value back into an input stream using the unRead function. Usually this will use the default pushback mechanism which provides a buffer for the stream. Some stream transformers, like takeBytes, produce streams that send pushed-back values back to the streams that they wrap. A function like map cannot do this because the types don't match up:

map :: (a -> b) -> InputStream a -> IO (InputStream b)

A function will usually document if its pushback behaviour differs from the default. No matter what the case, input streams should obey the following law:

Streams.unRead c stream >> Streams.read stream === return (Just c)

Input and output streams

datadata InputStream a

An InputStream generates values of type c in the IO monad.

Two primitive operations are defined on InputStream:

It is intended that InputStreams obey the following law:

unRead c stream >> read stream === return (Just c)

Constructors

Instances6BufferedIO, RawIO, IODevice
datadata OutputStream a

An OutputStream consumes values of type c in the IO monad. The only primitive operation defined on OutputStream is:

Values of type c are written in an OutputStream by wrapping them in Just, and the end of the stream is indicated by supplying Nothing.

If you supply a value after a Nothing, the behavior is defined by the implementer of the given OutputStream. (All OutputStream definitions in this library will simply discard the extra input.)

Constructors

Instances6BufferedIO, RawIO, IODevice

Primitive stream operations

valueunRead :: a -> InputStream a -> IO ()

Pushes a value back onto an input stream. read and unRead should satisfy the following law, with the possible exception of side effects:

Streams.unRead c stream >> Streams.read stream === return (Just c)

Note that this could be used to add values back to the stream that were not originally drawn from the stream.

Building streams

valuemakeInputStream :: IO (Maybe a) -> IO (InputStream a)

Creates an InputStream from a value-producing action.

(makeInputStream m) calls the action m each time you request a value from the InputStream. The given action is extended with the default pushback mechanism (see System.IO.Streams.Internal#pushback).

valuemakeOutputStream :: (Maybe a -> IO ()) -> IO (OutputStream a)

Creates an OutputStream from a value-consuming action.

(makeOutputStream f) runs the computation f on each value fed to it.

Since version 1.2.0.0, makeOutputStream also ensures that output streams no longer receive data once EOF is received (i.e. you can now assume that makeOutputStream will feed your function Nothing at most once.)

Connecting streams

Thread safety

Utility streams

Generator monad

newtypenewtype Generator r a

A Generator is a coroutine monad that can be used to define complex InputStreams. You can cause a value of type Just r to appear when the InputStream is read by calling yield:

g :: Generator Int ()
g = do
    Streams.yield 1
    Streams.yield 2
    Streams.yield 3

A Generator can be turned into an InputStream by calling fromGenerator:

m :: IO [Int]
m = Streams.fromGenerator g >>= Streams.System.IO.Streams.toList     -- value returned is [1,2,3]

You can perform IO by calling liftIO, and turn a Generator into an InputStream with fromGenerator.

As a general rule, you should not acquire resources that need to be freed from a Generator, because there is no guarantee the coroutine continuation will ever be called, nor can you catch an exception from within a Generator.

Instances4Monad, Functor, Applicative, MonadIO

Consumer monad

newtypenewtype Consumer c a
#
Instances4Monad, Functor, Applicative, MonadIO
  • Monad (Consumer c)Defined in io-streams-1.5.2.2 · System.IO.Streams.Internal
  • Functor (Consumer r)Defined in io-streams-1.5.2.2 · System.IO.Streams.Internal
  • Applicative (Consumer r)Defined in io-streams-1.5.2.2 · System.IO.Streams.Internal
  • MonadIO (Consumer c)Defined in io-streams-1.5.2.2 · System.IO.Streams.Internal