hsExprTest-3.6.5: Expression Testing
Safe HaskellSafe
LanguageHaskell2010

Test.Testable.IO.NoMonad

Synopsis

IO replacements

data IO a #

IO is opaque, as in Prelude, but it does not interract with outer world therefore it is safe and escapable by means of runIOLines

Internally, we use ST to accumulate user output and line generator to represent input.

Instances

Instances details
MonadFail IO 
Instance details

Defined in Test.Testable.IO

Methods

fail :: String -> IO a

Applicative IO 
Instance details

Defined in Test.Testable.IO

Methods

pure :: a -> IO a

(<*>) :: IO (a -> b) -> IO a -> IO b

liftA2 :: (a -> b -> c) -> IO a -> IO b -> IO c

(*>) :: IO a -> IO b -> IO b

(<*) :: IO a -> IO b -> IO a

Functor IO 
Instance details

Defined in Test.Testable.IO

Methods

fmap :: (a -> b) -> IO a -> IO b

(<$) :: a -> IO b -> IO a

Monad IO 
Instance details

Defined in Test.Testable.IO

Methods

(>>=) :: IO a -> (a -> IO b) -> IO b

(>>) :: IO a -> IO b -> IO b

return :: a -> IO a

(Semigroup a, Monoid a) => Monoid (IO a) 
Instance details

Defined in Test.Testable.IO

Methods

mempty :: IO a

mappend :: IO a -> IO a -> IO a

mconcat :: [IO a] -> IO a

Semigroup a => Semigroup (IO a) 
Instance details

Defined in Test.Testable.IO

Methods

(<>) :: IO a -> IO a -> IO a

sconcat :: NonEmpty (IO a) -> IO a

stimes :: Integral b => b -> IO a -> IO a

getLine :: IO String #

Read a line from the standard input device.

getChar :: IO Char #

Read a character from the standard input device.

getContents :: IO String #

The getContents operation returns all user input as a single string, which is read lazily as it is needed

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

The readIO function is similar to read except that it signals parse failure to the IO monad instead of terminating the program. NOTE: as our IO has no exception support, this is pureIO . read

readLn :: Read a => IO a #

The readLn function combines getLine and readIO.

interact :: (String -> String) -> IO () #

The interact function takes a function of type String -> String as its argument. The entire input from the standard input device is passed to this function as its argument, and the resulting string is output on the standard output device.

putChar :: Char -> IO () #

Write a character to the standard output device.

putStr :: String -> IO () #

Write a string to the standard output device.

putStrLn :: String -> IO () #

The same as putStr, but adds a newline character.

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

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

readFile :: FilePath -> IO String #

The readFile function reads a file and returns the contents of the file as a string. While the standard readFile is lazy, this one is not. Furthermore it calls error if the file does not exist.

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

The computation writeFile file str function writes the string str, to the file file. The file is created if it did not exist and ovewritten otherwise (writeFile does not specify this).

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

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

running

runIOLines :: (Int -> String) -> IO a -> (a, [String]) #

Same as runIOLines', but does not allow finite input.

runIOLines' :: (Int -> Maybe String) -> IO a -> (a, [String]) #

Run virtual IO

This function accepts line genrator and (virtual) IO action to run

  • generator: a function which for given line number returns line of input. Lines are numbered from 0, each line must not contain newline ('n') character, not even at the end.
  • action: a virtual IO action
  • return value is tuple (output of IO action, list of lines produced by the IO action). If no lines are produced then [""] returned. Also, empty line at the end is to be expected if 'n' was last character on output (for example if putStrLn was last action).
>>> runIOLines show $ return 0
(0, [""])
>>> runIOLines show $ getLine >>= print . (+1) . read >> getLine >>= \x -> return (read x :: Int)
(1, ["1",""])

functor, applicative and monad replacements

fmap :: (a -> b) -> IO a -> IO b #

(<$>) :: (a -> b) -> IO a -> IO b infixl 4 #

(<*>) :: IO (a -> b) -> IO a -> IO b infixl 4 #

pure :: a -> IO a #

(>>=) :: IO a -> (a -> IO b) -> IO b infixl 1 #

(>>) :: IO a -> IO b -> IO b infixl 1 #

return :: a -> IO a #