HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Modulelinear-base-0.4.0Haskell2010

System.IO.Resource.Linear

This module defines an IO monad for linearly working with system resources like files. It provides tools to take resources that are currently unsafely accessible from System.IO and use them in this monad.

Import this module qualified to avoid name clashes.

To use this RIO monad, create some RIO computation, run it to get a System.IO computation.

A simple example

Example9 expressions
:set -XLinearTypes:set -XQualifiedDo:set -XNoImplicitPreludeimport qualified System.IO.Resource.Linear as Linearimport qualified Control.Functor.Linear as Controlimport qualified Data.Text as Textimport Prelude.Linearimport qualified Prelude:{ linearWriteToFile :: IO () linearWriteToFile = Linear.run $ Control.do   handle1 <- Linear.openFile "/home/user/test.txt" Linear.WriteMode   handle2 <- Linear.hPutStrLn handle1 (Text.pack "hello there")   () <- Linear.hClose handle2   Control.return (Ur ()):}

To enable do notation, QualifiedDo extension is used. But since QualifiedDo only modifies the desugaring of binds, we still need to qualify Control.return.

  • 6 types
  • 17 values

The Resource I/O Monad

2 declarations
newtypenewtype RIO a
#

The resource-aware I/O monad. This monad guarantees that acquired resources are always released.

Instances7Monad, Functor, Applicative, Monoid, Semigroup, …
  • Applicative RIODefined in linear-base-0.4.0 · System.IO.Resource.Linear.Internal
  • Functor RIODefined in linear-base-0.4.0 · System.IO.Resource.Linear.Internal
  • Monad RIODefined in linear-base-0.4.0 · System.IO.Resource.Linear.Internal
  • Functor RIODefined in linear-base-0.4.0 · System.IO.Resource.Linear.Internal
  • Applicative RIODefined in linear-base-0.4.0 · System.IO.Resource.Linear.Internal
  • Monoid a => Monoid (RIO a)Defined in linear-base-0.4.0 · System.IO.Resource.Linear.Internal
  • Semigroup a => Semigroup (RIO a)Defined in linear-base-0.4.0 · System.IO.Resource.Linear.Internal
valuerun :: RIO (Ur a) -> IO a
#

Take a RIO computation with a value a that is not linearly bound and make it a System.IO computation.

Using Resource Handles

1 declaration

File I/O

datadata IOMode
#

See GHC.Internal.System.IO.openFile

Instances6Enum, Eq, Ord, Read, Show, Ix
  • Enum IOModeDefined in ghc-internal-9.1003.0 · GHC.Internal.IO.IOMode
  • Eq IOModeDefined in ghc-internal-9.1003.0 · GHC.Internal.IO.IOMode
  • Ord IOModeDefined in ghc-internal-9.1003.0 · GHC.Internal.IO.IOMode
  • Read IOModeDefined in ghc-internal-9.1003.0 · GHC.Internal.IO.IOMode
  • Show IOModeDefined in ghc-internal-9.1003.0 · GHC.Internal.IO.IOMode
  • Ix IOModeDefined in ghc-internal-9.1003.0 · GHC.Internal.IO.IOMode

Working with Handles

datadata SeekMode
#

A mode that determines the effect of GHC.Internal.System.IO.hSeek hdl mode i.

Constructors

  • AbsoluteSeek

    the position of hdl is set to i.

  • RelativeSeek

    the position of hdl is set to offset i from the current position.

  • SeekFromEnd

    the position of hdl is set to offset i from the end of the file.

Instances6Enum, Eq, Ord, Read, Show, Ix
  • Enum SeekModeDefined in ghc-internal-9.1003.0 · GHC.Internal.IO.Device
  • Eq SeekModeDefined in ghc-internal-9.1003.0 · GHC.Internal.IO.Device
  • Ord SeekModeDefined in ghc-internal-9.1003.0 · GHC.Internal.IO.Device
  • Read SeekModeDefined in ghc-internal-9.1003.0 · GHC.Internal.IO.Device
  • Show SeekModeDefined in ghc-internal-9.1003.0 · GHC.Internal.IO.Device
  • Ix SeekModeDefined in ghc-internal-9.1003.0 · GHC.Internal.IO.Device

Creating new types of resources

5 declarations
datadata Resource a where
#

The type of system resources. To create and use resources, you need to use the API since the constructor is not released.

valueunsafeAcquire :: IO (Ur a) -> (a -> IO ()) -> RIO (Resource a)
#

Given a resource in the System.IO.Linear.IO monad, and given a function to release that resource, provides that resource in the RIO monad. For example, releasing a Handle from System.IO would be done with fromSystemIO hClose. Because this release function is an input, and could be wrong, this function is unsafe.

valueunsafeFromSystemIOResource
  1. :: a -> IO b
  2. -> Resource a
  3. -> RIO (Ur b, Resource a)
#

Given a System.IO computation on an unsafe resource, lift it to RIO computaton on the acquired resource. That is function of type a -> IO b turns into a function of type Resource a %1-> RIO (Ur b) along with threading the Resource a.

unsafeFromSystemIOResource is only safe to use on actions which do not release the resource.

Note that the result b can be used non-linearly.

Deprecated symbols

2 declarations