Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function Totality with IO

Tags:

haskell

After reading over Prof. Yorgey's lecture on IO, is the following function considered to be total?

Prelude> let f x = return $ error "44" :: IO Int

I understand that a total function terminates and returns a value for every input.

However, since Haskell separates the evaluation and execution of IO, I'm not sure how totality applies to f.

like image 690
Kevin Meredith Avatar asked Nov 23 '16 17:11

Kevin Meredith


1 Answers

The function f is indeed total. It returns, for any input, a value. Specifically, it produces every time the same value.

The value happens to be an IO action, that, when executed, will probably terminate the program, unless run in a context that can deal with the error. But this doesn't play a role, since merely applying f does not run the IO action.

Here is an example program that uses f :

main = putStrLn $ show $ length $ map (f$) [1,2,3]

and it should print

3
like image 114
Ingo Avatar answered Oct 12 '22 06:10

Ingo