RE: Understanding strictness of ghc output

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 ;-). Cheers, Simon

On Tue, Jun 22, 2004 at 03:37:01PM +0100, Simon Marlow wrote:
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 ;-).
As long as the implementation would not do anything very fishy to produce a non-monotonic function, if addHeight _|_ e = m for some integer m, then addHeight n e would have to be m for all n, and I would agree very much that this is wrong :-) Greetings, Carsten P.S.: Is there a special reason for the Simons on this list not to produce messages with proper References headers? *duck* -- Carsten Schultz (2:38, 33:47), FB Mathematik, FU Berlin http://carsten.codimi.de/ PGP/GPG key on the pgp.net key servers, fingerprint on my home page.

"Simon Marlow"
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.
Actually, there are two possible strategies for who evaluates a thunk to head normal form. The caller or the callee. GHC says that the callee always evaluates to HNF before returning the value. But the language does not force this implementation choice. It is equally possible for an implementation of the function to return a thunk and for the caller to evaluate it if necessary. Both strategies give the same result semantically, since if the value in question is demanded, it gets evaluated.
If there's a Haskell implementation that compiles addHeight in such a way that addHeight _|_ e /= _|_, then I'd say it was wrong
So would we all! Regards, Malcolm
participants (3)
-
Carsten Schultz
-
Malcolm Wallace
-
Simon Marlow