[Haskell-cafe] Lifting makes lazy

hjgtuyl at chello.nl hjgtuyl at chello.nl
Thu Sep 16 12:26:35 EDT 2004


In my enthusiasm to reduce imperative style coding to a minimum, I changed  
a program to something too lazy to do anything. The following is an  
extremely simplified version of the program:

> import Monad

> displayFile1 :: IO (IO ())displayFile1 =  liftM putStr contents --  
> Displays nothing
>     where
>       contents :: IO [Char]
>       contents = readFile "DisplayFile.lhs"

This should display the contents of a file, but nothing appears. The  
following function should be exactly the same, but this one does display  
the file:

> displayFile2 :: IO ()
> displayFile2 =  do
>     contents <- readFile "DisplayFile.lhs"
>     putStr contents

My conclusion is, that "putStr" is evaluated strictly, while "liftM  
putStr" is not.

I have the following questions:
  - Why is this difference?
  - Is there some method to predict whether my program is sufficiently  
strict to really do what it is supposed to do?
  - Did someone design a method to develop programs not too strict and not  
too lazy?
  - The manual "A gentle introduction to Haskell" states in section 6.3:  
"adding strictness flags may lead to hard to find infinite loops or have  
other unexpected consequences"; I would like to know when these problems  
arise; are these cases described somewhere?

Met vriendelijke groet,
Herzliche Grüße,
Best regards,
Henk-Jan van Tuyl

   Festina Lente
   Hasten Slowly
   Haast U langzaam
   Eile langsam
   Skynd dig langsomt
   Affrettati lentamente
   Spěchej pomalu
   Skynda långsamt
                      Desiderius Erasmus

More information about the Haskell-Cafe mailing list