Enforcing Strict Evaluation

Bernd Holzmüller holzmueller@ics-ag.de
Fri, 17 Aug 2001 10:42:48 +0200


Hi all,

I wrote an application that successively reads the contents of several
(big) files, building an intermediate data structure, pretty printing
their contents into one accumulated output file and extracting some
information from the data structure for further processing. To avoid
needing huge heap space the program reads one input file, pretty prints
a representation of its contents (big) to the output file (via
appendFile), extracts the necessary information (small) and proceeds
with that information, so that the data structure representing the file
contents can be released. 

To achieve the latter, however, I need to force strict evaluation of the
extraction. My first approach was using $! as follows:

  ...
  info <- mapM (apply outfile) files
  ...

apply outfile infile = do
    result <- read infile -- parses infile and generates data structure
of its contents
    appendFile outfile (show result)
    return $! (extractInfos result) -- extractInfos returns a list of
some sort of information

but that did not work. I tried then printing the length of the extracted
list because this would require the strict evaluation, and this works:

  ...
  info <- mapM (apply outfile) files
  ...

apply outfile infile = do
    result <- read infile -- parses infile and generates data structure
of its contents
    appendFile outfile (show result)
    let result' = extractInfos result -- extracts necessary information
from result
    putStrLnFlushed (show (length result')) -- forces evaluation of
result'!
    return result'


Why does the first solution not work? How can I achieve the effects of
the latter solution without printing something?

Thanks,
Bernd