[GHC] #15226: GHC doesn't know that seq# produces something in WHNF
GHC
ghc-devs at haskell.org
Tue Jul 24 12:39:55 UTC 2018
#15226: GHC doesn't know that seq# produces something in WHNF
-------------------------------------+-------------------------------------
Reporter: dfeuer | Owner: (none)
Type: bug | Status: closed
Priority: normal | Milestone: 8.6.1
Component: Compiler | Version: 8.4.3
Resolution: fixed | Keywords:
Operating System: Unknown/Multiple | Architecture:
| Unknown/Multiple
Type of failure: Runtime | Test Case:
performance bug | perf/should_run/T15226, 15226a
Blocked By: | Blocking:
Related Tickets: | Differential Rev(s): Phab:D4796
Wiki Page: |
-------------------------------------+-------------------------------------
Comment (by simonpj):
> Considering seq# strict can be rather bad, I believe.
I'm not sure about that. First, let's remember (I always forget this) that
`seq#` has type
{{{
seq# ::a -> State# s -> (# State# s, a #)
}}}
That is, it involves the IO monad. It's used to implement
{{{
evaluate :: a -> IO a
evaluate a = IO $ \s -> seq# a s -- NB. see #2273, #5129
}}}
See Trac #5129.
Now consider
{{{
\s. let x = blah in
let y = x+1 in
case seq# x s of
(# s', x' #) -> ...
}}}
It seems fine to me transform this to
{{{
\x. case blah of x ->
let y = x+1 in
case seq# x s of (# s', x' #) ->
...
}}}
What if the `seq#` is after some other IO operations thus:
{{{
\s. let x = blah in
case f s of (# s1, r #) ->
case seq# x s of (# s2, x' #) -> ......
}}}
Now you might worry that `x` might be evaluated (and throw an exception)
before
`f` gets a chance to run. But it doesn't: there's a hack in the
strictness analyser
(see `See Note [IO hack in the demand analyser]` in `DmdAnal`) that will
make
`x`'s binding lazy; in effect the strictness analyser treats the `case f s
of ...`
as if it had an extra invisible alternative not mentioning `x`.
It's not that important. But I think that `seq#` can safely be strict in
`x`.
--
Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/15226#comment:20>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler
More information about the ghc-tickets
mailing list