Given the program:
import Debug.Trace
main = print $ trace "hit" 1 + trace "hit" 1
If I compile with ghc -O
(7.0.1 or higher) I get the output:
hit
2
i.e. GHC has used common sub-expression elimination (CSE) to rewrite my program as:
main = print $ let x = trace "hit" 1 in x + x
If I compile with -fno-cse
then I see hit
appearing twice.
Is it possible to avoid CSE by modifying the program? Is there any sub-expression e
for which I can guarantee e + e
will not be CSE'd? I know about lazy
, but can't find anything designed to inhibit CSE.
The background of this question is the cmdargs library, where CSE breaks the library (due to impurity in the library). One solution is to ask users of the library to specify -fno-cse
, but I'd prefer to modify the library.