1

Let's say I have a naively implemented function like this:

quadratic a b c = (ans1, ans2)
  where
    ans1 = ((-b) + sqrt (b * b - 4 * a * c)) / (2 * a)
    ans2 = ((-b) - sqrt (b * b - 4 * a * c)) / (2 * a)

There are multiple identical subexpressions. How can I tell without reading core that common subexpression elimination is happening or not and to which parts of this?

duplode
  • 33,731
  • 7
  • 79
  • 150
rityzmon
  • 1,945
  • 16
  • 26
  • I think "by reading core" is the answer. Why don't you want to do that? You may be able to guess by inserting functions from `Debug.Trace`, but I wouldn't rely on that. – jberryman Aug 29 '16 at 14:44
  • @jberryman Are there rules of thumb that indicate what code is likely or not likely to be optimized? – rityzmon Aug 29 '16 at 14:49
  • Unrelated, but nondeterminism is modeled with lists, not tuples. `quadratic a b c = [ans1, ans2] where ...` – chepner Aug 29 '16 at 15:17

1 Answers1

5

Using trace might tell you as demonstrated in this SO question.

import Debug.Trace

quadratic a b c = (ans1, ans2)
  where
    ans1 = ((-b) + tr1 (sqrt (b * b - 4 * a * c))) / (2 * a)
    ans2 = ((-b) - tr2 (sqrt (b * b - 4 * a * c))) / (2 * a)
    tr1 = trace "ans1"
    tr2 = trace "ans2"

main = print $ quadratic 1 10 3

Compiling this with -O2 or -O3 shows both ans1 and ans2 in the trace output indicating that GHC did not perform the CSE. You get similar results if you use tr1 in both places.

The Haskell Wiki mentions that GHC only performs CSE in limited circumstances - (link) - and that you are advised to perform it yourself if you want to make sure it happens.

Community
  • 1
  • 1
ErikR
  • 51,541
  • 9
  • 73
  • 124