Opened 10 years ago

Closed 10 years ago

#1276 closed bug (duplicate)

a sum-file program not working under Windows

Reported by: nanotitan@… Owned by:
Priority: normal Milestone:
Component: libraries/base Version: 6.6
Keywords: Cc:
Operating System: Windows Architecture: x86
Type of failure: None/Unknown Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:


This program just hangs under Windows, ghc-6-6.msi:

module Main where

main = print . sum . map read . lines =<< getContents

I compiled with:

ghc -O sumcol.hs -o sumcol_O.exe

and ran as:

sumcol_O.exe "sumcol-input.txt"

where sumcol-input.txt = sumcol-input.txt :: Text_File (KB 6, Lines 1000)

Replacing 'getContents' with 'readFile "sumcol-input.txt" ' works.

Similar observed behavior under ghci:

Prelude> :set args "sumcol-input.txt"
Prelude> print . sum . map read . lines =<< getContents


Prelude> print . sum . map read . lines =<< readFile "sumcol-input.txt"
Prelude> _

It might also be relevant that when compiling and running this version of the sum-file program:

{-# OPTIONS -fbang-patterns #-}
module Main where

import Data.List
import qualified Data.ByteString.Lazy.Char8 as L

main = print . foldl' (+) 0 . unfoldr parse =<< L.getContents

parse !s | Just (n,t) <- L.readInt s = Just (n, L.tail t)
         | otherwise                 = Nothing


ghc -O -fglasgow-exts sumcol.hs -o sumcol_O.exe

and then

sumcol_O.exe "sumcol-input.txt"

I get

sumcol_O.exe: <stdin>: hGetBufNonBlocking: resource exhausted (Not enough space)

System: ghc 6.6 under WinXP Pro SP2 Intel Celeron 2.53GHz, 512 RAM

I'll be happy to provide any other necessary information:

Fernando Gonzalez, nanotitan at google's mail service

Change History (1)

comment:1 Changed 10 years ago by duncan

Component: Compilerlibraries/base
Resolution: duplicate
Status: newclosed

The confusion here is over what getContents does. It reads from the standard input 'stdin'. It has nothing to do with parsing command line arguments.

So that's why the program appears to hang, because it is waiting for input on its stdin, which by default is the console/terminal.

So to run the program you should say:

sumcol_O.exe < "sumcol-input.txt"

to direct he file "sumcol-input.txt" to the standard input of sumcol_O.exe.

If you want to use the command line arguments you should use the getArgs function and readFile as you noted.

On the other hand, the ByteString example is indeed broken because hGetBufNonBlocking is broken on Windows. I'm marking this bug as a duplicate of the bug for that problem.

Note: See TracTickets for help on using tickets.