Is GHC able to tail-call optimize IO actions?
-
16-09-2019 - |
Question
Will GHC perform tail-call optimization on the following function by default? The only weird thing about it is that it is recursively defining an IO action, but I don't see why this couldn't be TCO'd.
import Control.Concurrent.MVar
consume :: MVar a -> [a] -> IO ()
consume _ [] = return ()
consume store (x:xs) = do putMVar store x
consume store xs
Solution
Since your code is equivalent to
consume store (x:xs) = putMVar store >> consume store xs
the call does not actually occur in tail position. But if you run ghc -O
and turn on the optimizer, the -ddump-simpl
option will show you the output of GHC's intermediate code, and it does indeed optimize into a tail-recursive function, which will compile into a loop.
So the answer is GHC won't optimize this by default; you need the -O
option.
(Experiments done with GHC version 6.10.1.)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow