Hello Dan,

`IO Integer` is something that, when executed, returns and `Integer` and there is no instance of `Show` for `IO Integer` as the compiler says.

You have to run the computations that will return the numbers and then print them, like so:

main :: IO ()
main = do
    let filenames = ["/etc/services"]
    let ioSizes = map get_size filenames :: [IO Integer]
    sizes <- sequence ioSizes
    mapM_ print sizes

-- sequence :: Monad m => [m a] -> m [a]

One important part is the use of sequence which transforms (ioSizes :: [IO Integer]) to `IO [Integer]` that is run and the result bound to (sizes : [Integer]).

Hope that's clear enough to get the point :)

Petr

On Tue, Oct 20, 2015 at 5:58 PM, Dan Stromberg <strombrg@gmail.com> wrote:

Here's a small program that replicates the compilation issue I'm seeing:

import qualified System.Posix.Files

get_size :: String -> IO Integer
get_size filename = do
    file_status <- System.Posix.Files.getFileStatus filename
    let file_size = System.Posix.Files.fileSize file_status
    let integer_file_size = fromIntegral file_size
    return integer_file_size

main :: IO ()
main = do
    let filenames = ["/etc/services"]
    let sizes = map get_size filenames
    mapM_ print sizes

The compilation error I get is:

ghc -Wall --make -o stat2 stat2.hs
[1 of 1] Compiling Main             ( stat2.hs, stat2.o )

stat2.hs:15:11:
    No instance for (Show (IO Integer)) arising from a use of `print'
    Possible fix: add an instance declaration for (Show (IO Integer))
    In the first argument of `mapM_', namely `print'
    In a stmt of a 'do' block: mapM_ print sizes
    In the expression:
      do { let filenames = ...;
           let sizes = map get_size filenames;
           mapM_ print sizes }
make: *** [stat2] Error 1

I've googled quite a bit, and guessed quite a bit, and added type declarations some, but I'm still not converging on a solution.

Why can't I print an IO Integer?

Thanks!

--
Dan Stromberg

_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners