program-0.1.0.0: Programs with Environments and Managed Resources
Copyright(c) Michael Szvetits 2021
LicenseBSD3 (see the file LICENSE)
Maintainertypedbyte@qualified.name
Stabilitystable
Portabilityportable
Safe HaskellSafe-Inferred
LanguageHaskell2010

Control.Program

Description

Types and functions for representing programs which run in a specific environment and are able to integrate bracket-like operations.

Synopsis

Program Representation

data Program e a Source #

Represents a program that produces a value of type a when running in an environment e. The required content of the environment is usually described by declaring Has constraints on e.

Turning an IO action into a Program is usually done by using liftIO.

Instances

Instances details
Monad (Program e) Source # 
Instance details

Defined in Control.Program

Methods

(>>=) :: Program e a -> (a -> Program e b) -> Program e b #

(>>) :: Program e a -> Program e b -> Program e b #

return :: a -> Program e a #

Functor (Program e) Source # 
Instance details

Defined in Control.Program

Methods

fmap :: (a -> b) -> Program e a -> Program e b #

(<$) :: a -> Program e b -> Program e a #

MonadFail (Program e) Source # 
Instance details

Defined in Control.Program

Methods

fail :: String -> Program e a #

Applicative (Program e) Source # 
Instance details

Defined in Control.Program

Methods

pure :: a -> Program e a #

(<*>) :: Program e (a -> b) -> Program e a -> Program e b #

liftA2 :: (a -> b -> c) -> Program e a -> Program e b -> Program e c #

(*>) :: Program e a -> Program e b -> Program e b #

(<*) :: Program e a -> Program e b -> Program e a #

MonadIO (Program e) Source # 
Instance details

Defined in Control.Program

Methods

liftIO :: IO a -> Program e a #

runProgram :: e -> Program e a -> IO a Source #

Runs a program in a given environment e.

Resource Handling

bracket Source #

Arguments

:: IO a

The computation which acquires the resource.

-> (a -> IO b)

The computation which releases the resource.

-> Program e a

The computation which uses the resource.

Acquire a resource, use it, and then release the resource automatically after the program ends.

bracketE :: (e -> IO a) -> (e -> a -> IO b) -> Program e a Source #

A version of bracket where the acquisition and release actions may consult the environment e.

manage :: (forall b. (a -> IO b) -> IO b) -> Program e a Source #

Integrates a continuation into a Program, which is useful for integrating existing bracket-like continuations (often named with...).

local :: Program e a -> Program e a Source #

Runs a sub-program within a program, which is useful for fine-grained resource handling (i.e., resources acquired by the sub-program are released after the sub-program ends, not at the end of the whole program).

Environment Handling

class Has e t where Source #

Demands that a specific value of type t must be present in the environment e.

Methods

from :: e -> t Source #

Extracts a value of type t from the environment e.

Instances

Instances details
Has e e Source # 
Instance details

Defined in Control.Program

Methods

from :: e -> e Source #

ask :: Program e e Source #

Gets the environment.

pull :: e `Has` t => Program e t Source #

Extracts a specific value of type t from the environment.

pullWith :: e `Has` t => (t -> IO a) -> Program e a Source #

Extracts a specific value of type t from the environment and extracts some IO action from it. This is useful if the environment contains a record of IO functions (e.g., a function which returns a handle).