[Haskell-cafe] types and number of evaluation steps

Heinrich Hördegen hoerdegen at funktional.info
Sat Feb 18 13:59:44 CET 2012


In case of +, the reason might be that it's cheap, but the function add 
could do something else than + (It was just a small example).

Ok, thank you for your useful comments. I will read about cse.
Heinrich


On 18.02.2012 13:42, Victor Gorokgov wrote:
> + on Int is extremely cheap. It is always faster to add again rather 
> than store the value.
> But Integer is a different story. Addition time on this type can grow 
> to several minutes.
>
> 18.02.2012 13:28, Heinrich Hördegen пишет:
>>
>> 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
>>
>>
>>
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe


-- 
--

hoerdegen at funktional.info
www.funktional.info

--




More information about the Haskell-Cafe mailing list