[Haskell-cafe] types and number of evaluation steps

Holger Siegel holgersiegel74 at yahoo.de
Sat Feb 18 12:52:08 CET 2012


Am 18.02.2012 um 11:56 schrieb Heinrich Hördegen:

> Hi,
> 
> this is true. The optimization only works with -O2. I'd like to have more details about what's going on. How can I make sure, that this optimization triggers?

You cannot. Common subexpression elimination is done by GHC very conservatively, because it can not only affect impure programs: it can also affects strictness/lazyness and worsen memory usage of pure code. Like the HaskellWiki says: "If you care about CSE, do it by hand."

Holger

> 
> Heinrich
> 
> On 18.02.2012 11:10, MigMit wrote:
>> Different kinds of optimization. I expect you'd have different results even if you use one type, but different -O flags.
>> 
>> On 18 Feb 2012, at 13:28, Heinrich Hördegen wrote:
>> 
>>> Dear all,
>>> 
>>> I have a question about evaluation with respect to types and currying. Consider this programm:
>>> 
>>> import Debug.Trace
>>> 
>>> -- add :: Integer ->  Integer ->  Integer
>>> add :: Int ->  Int ->  Int
>>> add x y = x + y
>>> 
>>> f a b c = trace "b" (add x c) where x = trace "a" (add a b)
>>> 
>>> main :: IO ()
>>> main = do
>>>  print (f 1 2 3)
>>>  print (f 1 2 4)
>>> 
>>> 
>>> Compiled with ghc-7.0.3:
>>> 
>>> $ ghc --make Main.hs -o main -O2
>>> 
>>> The function add has to types. When we use type Int ->  Int ->  Int, the programm produces "b a 6 b a 7" as output which shows that the x from the where clause in f is evaluated twice. However, when we use type Integer ->  Integer ->  Integer, this will give "b a 6 b 7" which shows that x is evaluated only once. This was rather unexpected to me.
>>> 
>>> Why does the number of evaluation steps depend on a type? Can anybody explain this or give a hint?
>>> 
>>> Thank you very much,
>>> Heinrich
>>> 
>>> 
>>> 
>>> -- 
>>> --
>>> 
>>> hoerdegen at funktional.info
>>> www.funktional.info
>>> 
>>> --
>>> 
>>> 
>>> _______________________________________________
>>> Haskell-Cafe mailing list
>>> Haskell-Cafe at haskell.org
>>> http://www.haskell.org/mailman/listinfo/haskell-cafe
>> 
> 
> 
> -- 
> --
> 
> Funktionale Programmierung Dr. Heinrich Hördegen
> Gutenbergstr. 26
> 80638 München
> 
> FON: +49 (89) 12 59 79 49
> FAX: +49 (89) 12 59 79 50
> 
> hoerdegen at funktional.info
> www.funktional.info
> 
> --
> 
> 
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe




More information about the Haskell-Cafe mailing list