pure-io-0.2.1: Pure IO monad.

Safe HaskellNone




Pure IO monad, intended for educational use.


The IO monad and its machinery

runIO :: Input -> IO a -> (Either Interrupt a, Output)Source

Run the IO monad. This should be called in succession. Depending on the type of interrupt, this function should be re-run with the same action but with additional input.

data IO a Source

A pure IO monad.

data Input Source

User input.




data Output Source

IO monad output.



data Interrupt Source

Something that interrupts the flow of the IO monad.



When you receive this interrupt, you should get some standard input from somewhere and then provide it in the Input value next time you call runIO.

InterruptException !IOException

When you receive this interrupt, you should consider the computation as ended.

Library of actions

putStrLn :: String -> IO ()Source

The same as putStr, but adds a newline character.

putStr :: String -> IO ()Source

Write a string to the standard output device.

getLine :: IO StringSource

Read a line from standard input.

readLn :: Read a => IO aSource

The readLn function combines getLine and readIO.

print :: Show a => a -> IO ()Source

The print function outputs a value of any printable type to the standard output device. Printable types are those that are instances of class Show; print converts values to strings for output using the show operation and adds a newline.

For example, a program to print the first 20 integers and their powers of 2 could be written as:

 main = print ([(n, 2^n) | n <- [0..19]])

readIO :: Read a => String -> IO aSource

The readIO function is similar to read except that it signals parse failure to the IO monad instead of terminating the program.

throw :: IOException -> IO aSource

Throw an IO exception.

catch :: IO a -> (IOException -> IO a) -> IO aSource

Catch an IO exception.

readFile :: FilePath -> IO StringSource

The readFile function reads a file and returns the contents of the file as a string. The file is read lazily, on demand, as with getContents.

writeFile :: FilePath -> String -> IO ()Source

The computation writeFile file str function writes the string str, to the file file.

appendFile :: FilePath -> String -> IO ()Source

The computation appendFile file str function appends the string str, to the file file.

Note that writeFile and appendFile write a literal string to a file. To write a value of any printable type, as with print, use the show function to convert the value to a string first.

 main = appendFile "squares" (show [(x,x*x) | x <- [0,0.1..2]])

doesFileExist :: FilePath -> IO BoolSource

The operation doesFileExist returns True if the argument file exists, and False otherwise.

removeFile :: FilePath -> IO ()Source

removeFile file removes the directory entry for an existing file file.

getDirectoryContents :: FilePath -> IO [FilePath]Source

Get all files in the given directory.