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
Some C compilers, such as gcc and clang, can perform tail call optimization (TCO).
Go, however, does not implement tail-call optimization, and you will eventually run out of memory.
Because F# is a language that heavily uses recursion, its compiler employs a trick that is called "tail call optimization". With this trick, the last function a function calls (even when it is not herself) does not burden the stack. This allows tail recursion to be essentially a loop, in terms of stack pressure.
Tail-call optimization is a part of the ES2015-ES6 specification. Supporting it isn't a NodeJS thing, it's something the V8 engine that NodeJS uses needs to support.
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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With