If I understand the discussion here correctly, seq
should not be evaluating a value twice, as in x `seq` x
should be evaluating x
once.
Then why do I have this behaviour?
λ> :set +s
λ> let fib x = if x <= 1 then x else fib (x - 1) + fib (x - 2)
(0.01 secs, 102,600 bytes)
λ> fib 30
832040
(2.49 secs, 638,088,448 bytes)
λ> let x = fib 30 in x
832040
(2.47 secs, 638,088,792 bytes)
λ> let x = fib 30 in x `seq` x
832040
(4.95 secs, 1,276,067,128 bytes)
which is clearly double evaluating? Am I misunderstanding something?
EDIT: As asked @danidiaz below, I also evaluated
λ> (\x -> x `seq` x) (fib 30)
832040
(2.51 secs, 638,087,888 bytes)
λ> let x = (fib 30) :: Int in x `seq` x
832040
(2.52 secs, 732,476,640 bytes)
which are even more surprising now.
EDIT 2: I see that this question has been marked as a duplicate of an earlier question that asks about the monomorphism restriction. When I encountered this problem, I had no idea that this was due to the restriction. So if someone finds him/herself in my position I guess the answer to this question would be helpful.