[Haskell-beginners] profiling weirdness?

Thomas haskell at phirho.com
Tue Jun 8 19:23:56 EDT 2010


Hello all!

Currently this is more of a curiosity for me rather than a real obstacle 
to learn Haskell, but I'd appreciate any insight into this behaviour 
nevertheless.

Given the (dumb, I know, but it's for illustration purpose) program:
---
import Data.Char

type Position = (Char, Integer)

sameSquare :: Position -> Position -> Bool
sameSquare p1 p2
	= ssqHelper p1 p2

ssqHelper :: Position -> Position -> Bool
ssqHelper (r1, c1) (r2, c2)
         | (((c1-1) `div` 3) == ((c2-1) `div` 3) && ((((ord r1) - 65) 
`div` 3) == (((ord r2) - 65) `div` 3))) = True
         | otherwise = False

main :: IO ()
main = print (sameSquare ('A', 2) ('B', 2))
---

If I compile this with profiling info and run the profiler all seems 
well (independently of optimization settings). Now, if I comment the line
--ssqHelper :: Position -> Position -> Bool
and do the same (compile with profiling info) I can see rather 
surprising results. While I'd expect exactly one call to ssqHelper for 
every call to sameSquare this is actually only the case if compiled 
without optimizations. Using -O2 when compiling I get a full 5 calls of 
ssqHelper for every call to sameSquare.

I am unable to imagine an optimization that would be more efficient with 
five calls instead of one. And I do not understand either what this has 
to do with the type annotation. Within such a trivial program the 
inferred type should be equivalent, shouldn't it?
Actually ghci generalizes slightly: Integral instead of Integer. But 
that's no clue for me either...

BTW I'm using ghc 6.10.4. (In case it matters.)

Any hint would be appreciated.
Thanks,
Thomas









More information about the Beginners mailing list