Understanding strictness of ghc output

Simon Marlow simonmar at microsoft.com
Tue Jun 22 10:37:01 EDT 2004

On 22 June 2004 15:11, Duncan Coutts wrote:

> On Tue, 2004-06-22 at 14:17, Tomasz Zielonka wrote:
>> On Tue, Jun 22, 2004 at 01:52:44PM +0100, Malcolm Wallace wrote:
>>> Same again.  Try
>>>    addHeight h  E        = h `seq` h
>>> which, although it looks bizarre, actually forces the evaluation of
>>> h, whilst simply returning it does not.
>> That contradicts my intution for seq. I would read it as "h is forced
>> before h is forced", and I would think that (h `seq` h) is equivalent
>> to h.
> I think a better intuition is that "h is forced before h is
> *returned*". You can return a value without that value being forced
> to head normal form. In fact this is the ordinary case. Values are
> only 'forced' when you pattern match on them (or if you use seq), and
> even then only when the result of the pattern match is used.

Nope.  You can't return something without evaluating it to head normal
form in Haskell.  Every value that is "returned" is a value, never a
thunk.  If you want to return something unevaluated, you have to wrap it
in a constructor.

h `seq` h  is the same as h.

addHeight is strict in h, no seq is necessary.

If there's a Haskell implementation that compiles addHeight in such a
way that addHeight _|_ e  /=  _|_, then I'd say it was wrong (but we
don't have an official denotational semantics for Haskell, only an
informal agreement ;-).


More information about the Glasgow-haskell-users mailing list