Thursday, January 31, 2013

HASKELL INPUT AND OUTPUT (IO)

HASKELL INPUT AND OUTPUT (IO)




REVISED: Monday, October 21, 2024




Haskell Input and Output (IO).

Good Haskell style involves separating pure code from code that performs IO.

I.  HASKELL IMPORT

I am asking you to input the following imports so I can use them in examples "I. A." and "I. B." shown below. I also want to demonstrate the dangers of using imports without knowing their contents.

Prelude> import Control.Arrow
Prelude>

Prelude> import Control.Monad
Prelude>

Prelude> import Data.ByteString
Prelude>

Prelude> import Data.Char
Prelude>

Prelude> import Data.List 
Prelude>

Prelude> import System.IO  
Prelude>

Prelude> import System.Directory
Prelude>

Prelude> import System.Environment 
Prelude>

Prelude> import System.IO.Error
Prelude>

Prelude> import System.Random 
Prelude>

A. :SHOW IMPORTS

Prelude> :show imports
import Prelude -- implicit
import Control.Arrow
import Control.Monad
import Data.ByteString
import Data.Char
import Data.List
import System.IO
import System.Directory
import System.Environment
import System.IO.Error
import System.Random
:module +*Main -- added automatically
Prelude>

Notice Data.ByteString is in the above list of imports.

B. :MODULE -M

Where M is the name of the import you want to remove, imports can be removed from the current scope, using the syntax :module -M as shown below:

Prelude> :module -Data.ByteString
Prelude>

Prelude> :show imports
import Prelude -- implicit
import Control.Arrow
import Control.Monad
import Data.Char
import Data.List
import System.IO
import System.Directory
import System.Environment
import System.IO.Error
import System.Random
:module +*Main -- added automatically
Prelude>

Notice Data.ByteString is not in the above list of imports.

The full syntax of the :module command is: 

:module [+|-] [*]mod1 ... [*]modn

Using the + form of the module command adds modules to the current scope, and - removes them.

You cannot add a module to the scope if it is not loaded.

II.  HASKELL "IO ACTIONS"

If you did:

Prelude> import Data.ByteString
Prelude>

Prelude> import System.IO
Prelude>

and did not do:

Prelude> :module -Data.ByteString
Prelude>

as shown above in I., you will receive an "ambiguous occurrence" error message from GHCi when you do the examples shown below. GHCi will not be able to determine which import you are referring to. Therefore, you have to be very careful when using multiple imports. It is a good idea to know everything contained in an import before you use it.

The GHCi prompt operates as if the lines were being typed into a do block in the IO monad.

Prelude> :type putStrLn
putStrLn :: String -> IO ()
Prelude>

putStrLn gives back nothing, ().

Prelude>  :t getLine
getLine :: IO String
Prelude>

getLine gives back a String to use. 

Prelude> :type readFile
readFile :: FilePath -> IO String
Prelude>

IO is Haskell’s way of representing that putStrLn and readFile are not really functions, in the pure mathematical sense, they are “IO Actions”. Typical actions include reading and setting global variables, writing files, reading input, and opening windows.

The -> drawn from notation is a way to get the value out of an action. The (), pronounced “unit”, is an empty tuple indicating that there is no return value from putStrLn. This is similar to "void" in Java or C.

If we are taking data out of an IO action, we can only take it out when we are inside another IO action. In other words, to get the value out of an IO action, you have to perform it inside another IO action by binding it to a name with the -> drawn from arrow function. Variables bound by the -> drawn from arrow function are lambda bound and are thus monomorphic. IO actions will only be performed when they are given the name of main or when they are inside a bigger IO action that was composed with a do block. Therefore, when you want to bind the results of IO actions to names, use the -> drawn from arrow function.

The only way to use things with an IO type is to combine them with functions using do notation which can include if/then/else and case/of. In a do expression, every line is a monadic value. We code do and then we lay out a series of steps, like we would in an imperative program chaining computations. Each of these steps is an IO action, a control flow expression. A statement is either an action, a pattern bound to the result of an action using <-, or a set of local definitions introduced using letBy putting them together with do syntax, we glue them into one IO action. do works with any monad, the action that we get has a type of IO (), because that is the type of the last IO action inside the doWhen sequencing actions with do notation, each “bare” line, lines that do not have a <- in them, must have type IO ().

do can replace the  >> pronounced "then" operator.

Prelude> :type (>>)
(>>) :: Monad m => m a -> m b -> m b
Prelude>

We can use do syntax to glue together several IO actions into one.  The value from the last action in a do block is bound to the do block result.

You always do IO inside a do statement, using the IO monad. This way you are guaranteed that IO actions are executed in order.

$ is pronounced "apply." When a $ is encountered, the expression on its right is applied as the parameter to the function on its left. In other words the $ is the same as a ( and the closing ) is the end of the line. For example (1, 2, 3, 4) is the same as $ 1, 2, 3, 4

You cannot use $ if the closing ) would not appear at the end of the line.

A. REVIEW

First, we will review GHCi "ambiguous occurrence" error messages

Try doing the following examples. You will receive another GHCi "ambiguous occurrence" error message. The point I am repeatedly trying to make is you should understand the contents of imports before using them.

Exit out of GHCi and then reload GHCi. Use the following commands to see what you start out with in GHCi when it is first loaded.

Prelude>  :show
options currently set: +t   -- +t prints type after evaluation
base language is: Haskell2010
with the following modifiers:
  -XNoDatatypeContexts
  -XNondecreasingIndentation
GHCi-specific dynamic flag settings:
other dynamic, non-language, flag settings:
  -fimplicit-import-qualified
warning settings:
Prelude>

Prelude>  :show imports
import Prelude -- implicit
Prelude>  

Second, we will show a basic example of putStrLn:

Prelude> :{
Prelude| do {
Prelude| putStrLn "Hello, World!"
Prelude| }
Prelude| :}
Hello, World!
it :: ()
Prelude>

Third, we will show an example of using getLine and $ with putStrLn:

Prelude> :{
Prelude| let prRev = do {
Prelude|      inp <- getLine;
Prelude|      putStrLn $ reverse inp;
Prelude| }
Prelude| :}
Prelude| 
Prelude>

Prelude> :type prRev
prRev :: IO ()
Prelude>

Prelude> prRev
Hello, World!
!dlroW ,olleH
Prelude>

B. OUTPUT FUNCTIONS

These functions write to the standard output device, which is normally the user's terminal.

Prelude> import Data.Char
Prelude>

Prelude> :type print
print :: Show a => a -> IO ()
Prelude>

Prelude> :type putChar
putChar :: Char -> IO ()
Prelude>

The unit type (), which is similar to void in other languages, is used by actions which return no values.  return packs up a value into an IO box. <- extracts the value out of an IO box and is read as "comes from". In other words, return turns a value into an IO action and <- turns an IO action into a regular value.

"Copy paste" the following Haskell script into your text editor and "save as" TestReturn.hs  to your working directory:

-- TestReturn.hs

module TestReturn where

main = do putStrLn "Shall I quit the program? y/n"
          ans <- getLine
          if ans /= "y"
             then do putStrLn "Thank you! I enjoy working with you!"
                     main
             else do putStrLn "Nice working with you my friend! Happy trails, until we meet again!"
                     return ()  -- We created an IO that does not do anything so the program can exit.

As shown below, load the TestReturn program, then run the module TestReturn function main, in GHCi:

Prelude>  :load TestReturn
[1 of 1] Compiling TestReturn       ( TestReturn.hs, interpreted )
Ok, modules loaded: TestReturn.
Prelude>

Prelude>  main
Shall I quit the program? y/n
n
Thank you! I enjoy working with you!
Shall I quit the program? y/n
y
Nice working with you my friend! Happy trails, until we meet again!
it :: ()
Prelude>  

PS C:\Users\User\tinnel\Haskell 2024\newProjects> ghci
GHCi, version 9.4.8: https://www.haskell.org/ghc/  :? for help
ghci> :load TestReturn.hs
[1 of 1] Compiling TestReturn       ( TestReturn.hs, interpreted )
Ok, one module loaded.
ghci> main
Shall I quit the program? y/n
n
Thank you! I enjoy working with you!
Shall I quit the program? y/n
y
Nice working with you my friend! Happy trails, until we meet again!
ghci>

putStrLn is a function that takes a single argument of type String and produces a value of type IO (). The IO () signifies an executable action that returns a value of type (). When we supply putStrLn with a String argument we get an executable IO action which will print the supplied String.

Prelude> :type putStr
putStr :: String -> IO ()
Prelude>

Prelude> putStr "Hello"
Helloit :: ()   -- Notice that two lines combine without a \n newline escape sequence!
Prelude> 

Prelude> putStr "Hello\n"
Hello   -- The \n newline escape sequence does a carriage return.
it :: ()Prelude>

Prelude> :type putStrLn
putStrLn :: String -> IO ()
Prelude>

Prelude> :type putStrLn "abcdefghijklmnopqrstuvwxyz"
putStrLn "abcdefghijklmnopqrstuvwxyz" :: IO ()
Prelude>

C. INPUT FUNCTIONS

These functions read input from the standard input device, which is normally the user's keyboard. 

Prelude> :type getChar
getChar :: IO Char
Prelude>

When the function getChar is invoked, it will perform an action which returns a character.

Prelude> :type getLine
getLine :: IO String
Prelude>

Should be read as, "getLine is an action that, when performed, produces a string".

Prelude> :type getContents
getContents :: IO String
Prelude>

Prelude> :type interact
interact :: (String -> String) -> IO ()
Prelude>

Prelude> :type readIO
readIO :: Read a => String -> IO a
Prelude>

Prelude> :type readLn
readLn :: Read a => IO a
Prelude>

Use let bindings to bind pure expressions, normal non-IO expressions, to names. For example:

Prelude> let y = 12
y :: Num a => a
Prelude>

Prelude> y
12
it :: Num a => a
Prelude>

As shown above, let bindings do not automatically print the value bound.

As shown below, when we type out a monadic IO action in GHCi and press Enter, the IO action will be performed:

Prelude> putStrLn "Hello, World!" 
Hello, World!
it :: ()
Prelude>

You are always writing code in the IO monad when you are using GHCi.  GHCi evaluates the IO action, then calls show on the IO action, and prints the string to the terminal using putStrLn implicitly. 

D. IO EXAMPLE 1

"Copy, Paste" the following program into your text editor and "File, Save As" Point.hs to your working directory:

-- Point.hs

module Point where

point :: IO (Char,Char)
point = do {  x <- getChar
                    ; getChar
                   ; y <- getChar
                   ; return (x,y)
                 }

Load Point.hs into GHCi as follows:

Prelude>  :load Point.hs
[1 of 1] Compiling Main             ( point.hs, interpreted )
Ok, modules loaded: Main.
Prelude>

From GHCi call the Point.hs program as follows:

Prelude>  Point.hs
8  -- You will be prompted twice. At each prompt type an Integer then press Enter.
2
('8','2')  -- The output will return a tuple point :: IO (Char,Char).
Prelude>

PS C:\Users\User\tinnel\Haskell 2024\newProjects> ghci
GHCi, version 9.4.8: https://www.haskell.org/ghc/  :? for help
ghci> :load Point.hs
[1 of 1] Compiling Point            ( Point.hs, interpreted )
Ok, one module loaded.
ghci> point
8
2
('8','2')
ghci> :quit
Leaving GHCi.
PS C:\Users\User\tinnel\Haskell 2024\newProjects> 

E. IO EXAMPLE 2

"Copy, Paste" the following program into your text editor and "File, Save As" Name.hs to your working directory:

-- Name.hs

module Name where

import Data.List()
import System.IO

main :: IO ()
main = do {  putStrLn "What is your name?"
                    ; yn 
 <- getLine
<- getline="" span="">                     ; putStrLn ("Hello " ++ yn ++ "!" ) 
                  }

Load Name.hs into GHCi as follows:

Prelude>  : load Name.hs
[1 of 1] Compiling Main             ( name.hs, interpreted )
Ok, one module loaded.
*Main>

From GHCi call the Name program as follows:

*Main>  main
What is your name?
Ron
Hello Ron!
*Main> 

PS C:\Users\User\tinnel\Haskell 2024\newProjects> ghci
GHCi, version 9.4.8: https://www.haskell.org/ghc/  :? for help
ghci> :load Name.hs
[1 of 1] Compiling Name             ( Name.hs, interpreted )
Ok, one module loaded.
ghci> main
What is your name?
Ron
Hello Ron!
ghci> :quit
Leaving GHCi.
PS C:\Users\User\tinnel\Haskell 2024\newProjects> 

III. TEXT FORMAT

Keep lines to no more than 80 columns.

Do not use tabs.

Use CamelCase, do not use underscores.

Do not leave trailing white space in your lines of code.

Do not use braces and semicolons.

Use spaces for indenting.

Indent code blocks such as "let, where, do, and of" 4 spaces.

Indent "where" keyword 2 spaces.

Indent "where" definitions 2 spaces.

Indent "guards" 2 spaces.

IV. CONCLUSION

In this tutorial, you have received an introduction to Haskell input and output (IO).

V. REFERENCES

Bird, R. (2015). Thinking Functionally with Haskell. Cambridge, England: Cambridge University Press.

Davie, A. (1992). Introduction to Functional Programming Systems Using Haskell. Cambridge, England: Cambridge University Press.

Goerzen, J. & O'Sullivan, B. &  Stewart, D. (2008). Real World Haskell. Sebastopol, CA: O'Reilly Media, Inc.

Hutton, G. (2007). Programming in Haskell. New York: Cambridge University Press.

Lipovača, M. (2011). Learn You a Haskell for Great Good!: A Beginner's Guide. San Francisco, CA: No Starch Press, Inc.

Thompson, S. (2011). The Craft of Functional Programming. Edinburgh Gate, Harlow, England: Pearson Education Limited.