dialogue-0.1.0: I/O in Haskell Report 1.2
Copyright(c) Alias Qli 2022
LicenseBSD-3-Clause
Maintainer2576814881@qq.com
Stabilityexperimental
PortabilityPOSIX
Safe HaskellSafe-Inferred
LanguageHaskell2010

System.IO.Dialogue

Description

This module implements stream-based I/O as described in Haskell Report 1.2.

As th resport says, "Haskell's I/O system is based on the view that a program communicates to the outside world via streams of messages: a program issues a stream of requests to the operating system and in return receives a stream of responses." And a stream in Haskell is only a lazy list.

Synopsis

The Program Type

type Dialogue = [Response] -> [Request] Source #

Type of a Haskell program. [Response] is an ordered list of responses and [Request] is an ordered list of requests; the nth response is the operating system's reply to the nth request.

Request Types

data Request Source #

Requests a program may issue.

Constructors

ReadFile Name

ReadFile name returns the contents of file name.

  • If successful, the response will be of the form Str s, where s is a string value;
  • If the file does not exist, the response Failure (SearchError string) is induced;
  • If it is unreachable for some other reason, the Failure (ReadError string) error is induced.
WriteFile Name String

WriteFile name string writes string to file name. If the file does not exist, it is created. If it already exists, it is overwritten.

This request is "hyperstrict" in its second argument: no response is returned until the entire list of values is completely evaluated.

AppendFile Name String

Identicle to WriteFile, except that

  1. the string argument is appended to the current contents of the file named name;
  2. If the file does not exist, the response Failure (SearchError string) is induced.

All other errors have form Failure (WriteError string), and the request is hyperstrict in its second argument.

ReadBinFile Name

Similar to ReadFile, except that if successful, the response will be of the form Bn b, where b is a binary value.

WriteBinFile Name Bin

Similar to WriteFile, except that it writes a binary value to file.

AppendBinFile Name Bin

Similar to AppendFile, except that it writes a binary value to file.

DeleteFile Name

DeleteFile name delete file name, with successful response Success.

  • If the file does not exist, the response Failure (SearchError string) is induced.
  • If it cannot be deleted for some other reason, a response of the form Failure (WriteError string) is induced.
StatusFile Name

StatusFile name induce Failure (SearchError string) if an object name does not exist, or Failure (OtherError string) if any other error should occur. Otherwise induce Str status where ststus is a string containing, in this order:

  1. Either 'f', 'd', or 'u' depending on whether the object is a file, directory, or something else, respectively;
  2. 'r' if the object is readable by this program, '-' if not;
  3. 'w' if the object is writable by this program, '-' if not;
  4. 'x' if the object is executable by this program, '-' if not.

For example, "dr--" denotes a directory that can be read but not written or executed.

ReadChan Name

ReadChan name opens channel name for input.

  • A successful response returns the contents of the channel as a lazy stream of characters.
  • If the channel does not exist, the response Failure (SearchError string) is induced;
  • All other errors have form Failure (ReadError string).

Unlike files, once a ReadChan or ReadBinChan request has been issued for a particular channel, it cannot be issued again for the same channel in that program, This reflects the ephemeral nature of its contents and prevents a serious space leak.

Known issue: This request would leave the handle behind the channel in semi-closed state, causing any other attempt to read from the channel to fail. This should be problematic if your program issued an request to read from stdin, and

  1. You called Haskell functions that read from stdin (e.g. getLine), or ran another program that issues such a request after the program finishes.
  2. You're running the program from ghci.
AppendChan Name String

AppendChan name string writes string to channel name. The sematics is as for AppendFile, except:

  1. The second argument is appended to whatever was previously written (if anything);
  2. If channel does not exist, the response Failure (SearchError string) is induced.

All other errors have form Failure (WriteError string). This request is hyperstrict in its second argument.

ReadBinChan Name

Similar to ReadChan, except that if successful, the response will be of the form Bn b, where b is a lazy binary value.

AppendBinChan Name Bin

Similar to AppendChan, except that it writes a binary value to the channel.

StatusChan Name

StatusChan name induces Failure (SearchError string) if an channel name does not exist, otherwise it always induces Str "0 0". The two "0"s indicate that there's no bound on the maximum line length and page length allowed on the channel, respectively.

Echo Bool

Echo True enables echoing of stdin on stdecho; Echo False disables it. Either Success or Failure (OtherError string) is induced.

The report requires that the echo mode can only be set once by a particular program, before any I/O operation involving stdin. However, the restriction is loosened, and echo mode may be set at any time by the proogram multiple times.

Known issue: It's currently implemented as hSetEcho, which is known not to work on Windows.

GetArgs

Induces the response StrList str_list, where str_list is a list of the program's explicit command line arguments.

GetProgName

Returns the short name of the current program, not including search path information. If successful, the response will be of the form Str s, where s is a string. If the operating system is unable to provbide the program name, Failure (OtherError string) is induced.

GetEnv Name

GetEnv name Returns the value of environment variable name. If successful, the response will be of the form Str s, where s is a string. If the environment variable does not exist, a SearchError is induced.

SetEnv Name String

SetEnv name string sets environment variable name to string, with response Success. If the environment variable does not exist, it is created.

Instances

Instances details
Read Request Source # 
Instance details

Defined in System.IO.Dialogue

Show Request Source # 
Instance details

Defined in System.IO.Dialogue

Eq Request Source # 
Instance details

Defined in System.IO.Dialogue

Methods

(==) :: Request -> Request -> Bool #

(/=) :: Request -> Request -> Bool #

Ord Request Source # 
Instance details

Defined in System.IO.Dialogue

The Binary Type

type Bin = ByteString Source #

Bin is a datatype for binary values, as required by the report, and is implemented as a lazy ByteString.

nullBin :: Bin Source #

The empty binary value.

appendBin :: Bin -> Bin -> Bin Source #

Append two Bins.

isNullBin :: Bin -> Bool Source #

Test whether a Bin is empty.

The Name Type

type Name = String Source #

This type synonym is described in Haskell Report 1.0, and exists for backward compatibility.

Channels

stdin :: Name Source #

The stdin channel. Readable.

stdout :: Name Source #

The stdout channel. Writable.

stderr :: Name Source #

The stderr channel. Writable.

stdecho :: Name Source #

The stdecho channel. Writable. Attached to stdout.

Response Types

data Response Source #

Responses a program may receive.

Instances

Instances details
Read Response Source # 
Instance details

Defined in System.IO.Dialogue

Show Response Source # 
Instance details

Defined in System.IO.Dialogue

Eq Response Source # 
Instance details

Defined in System.IO.Dialogue

Ord Response Source # 
Instance details

Defined in System.IO.Dialogue

data IOError Source #

Constructors

WriteError String 
ReadError String 
SearchError String 
FormatError String

Since we're using a modern device and the maximum line length and page length allowed on the channel have no bound, this error would never occur.

OtherError String 

Instances

Instances details
Read IOError Source # 
Instance details

Defined in System.IO.Dialogue

Show IOError Source # 
Instance details

Defined in System.IO.Dialogue

Eq IOError Source # 
Instance details

Defined in System.IO.Dialogue

Methods

(==) :: IOError -> IOError -> Bool #

(/=) :: IOError -> IOError -> Bool #

Ord IOError Source # 
Instance details

Defined in System.IO.Dialogue

Run the Program

runDialogue :: Dialogue -> IO () Source #

The central function to run a program.