[Haskell-beginners] A first try

Heinrich Apfelmus apfelmus at quantentunnel.de
Thu Jun 30 11:04:53 CEST 2011

David Place wrote:
> On Jun 29, 2011, at 4:30 AM, Heinrich Apfelmus wrote:
>> It is still possible to write programs that return unexpected _|_,
>> but they violate a clear conceptual guideline ("only fully
>> evaluated values may escape the scope of  withFile ").
> I wonder. If that is the behavior you desire, why not just call
> deepseq on the value before you return it to guarantee it.

Hm. The idea was that while evaluating the return value in full is a 
sufficient requirement, it is not a necessary requirement. The return 
value may contain unevaluated expressions as long as the input stream is 
being forced. Example:

     s <- hGetContents h
     let  x = take 10 s `deepseq` [reverse . drop 3 $ take 7 s]
     evaluate x
     hClose h

Evaluating  x  to weak head normal form will force the first 10 
characters of the input, but  x  will still contain unevaluated 
expressions. (This is what Iteratees do: they force the input without 
forcing the return value.)

But you are probably right, it's better to give guarantees. The behavior 
above can still be simulated with a broken  deepseq  instance.

     data Lazy a = Lazy a
     data NFData (Lazy a) where
         rnf _ = ()               -- broken on purpose

This way, breaking the guarantee takes more effort than not breaking it, 
as it should be.

>> Granted, Iteratees make it impossible to write such programs, but
>> they come with the terrible price of code duplication.
> I don't really understand this objection, though.  Won't any iteratee
> library provide all those functions?  The user shouldn't have to
> write them.  In this way, it is certainly no worse than the
> duplication brought about by ByteStrings.  Also, isn't that the point
> of the ListLike class?

I think the  ListLike  class demonstrates very well that there will 
always be useful list functions that are not provided by an existing 
API. :) Also note that the  ListLike  class from the ListLike package 
doesn't work for Iteratees, they have to provide their own functions and 
so on. At some point, the count of different implementations for, holy 
lambda, *lazy lists* simply becomes ridiculous. ;)

Best regards,
Heinrich Apfelmus


More information about the Beginners mailing list