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.Core

Core types and functions for the io-streams library.

Contents
  1. Stream types
  2. Creating streams
  3. Primitive stream operations
  4. Connecting streams together
  5. Thread safety / concurrency
  6. Utility streams
  7. Generator monad
Synopsis

Stream types

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)
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.)

Instances6BufferedIO, RawIO, IODevice

Creating 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.)

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.

Connecting streams together

Thread safety / concurrency

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