Strict Haskell

Simon Peyton Jones simonpj at
Thu Feb 18 10:42:06 UTC 2016

Consider this with -XStrict

f y = let Just x = blah[y] in body[y,x]
Suppose that in a call to f,

·         blah returns Nothing

·         but body does not use x
Should f succeed?  For sure, blah will be evaluated to HNF before body is started, but is the match against Just done strictly too?
According to our current semantics, in the match against Just is not done strictly, so the call should succeed.  I think that’s unexpected and probably wrong.
Here’s the semantics
The translation for
            !(Just x) = blah

ð  (FORCE)   v = blah; Just x = v    (and add a seq on v)

ð  (SPLIT)     v = blah; x = case v of Just x -> x
So we finish up with

f y = let v = blah[y] in

      let x = case v of Just x -> x in

      v `seq` body[y,x]
I don’t think that’s what you intended.
If the pattern can fail, I think we want the FORCE step to say this:

Replace any binding !p = e with

v = case e of p -> (v1,..,vn); (v1,..,vn) = v

and replace e0 with v seq e0, where v is fresh and are the variable(s) bound by p
(Compare with the text at the above link.)
Do you agree?

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the ghc-devs mailing list