This code leaks or doesn't depending on how I compile the same RWS implementation:
import Control.Monad (replicateM_)
import qualified Control.Monad.RWS.CPS as RWS
import Data.Monoid (Sum (..))
-- import qualified RWS
{-# ann module "HLint: ignore Use camelCase" #-}
count_a_lot :: Int -> RWS.RWS () (Sum Int) () ()
count_a_lot = flip replicateM_ count
count :: RWS.RWS () (Sum Int) () ()
count = RWS.tell . Sum $ 1
main :: IO ()
main = print . snd $ RWS.evalRWS (count_a_lot 10000000) () ()
I experimented with two ways of using the CPS version of RWS:
import qualified Control.Monad.RWS.CPS as RWSuses thewriter-cps-transformerspackage, from nixpkgs.import qualified RWSuses Internal.hs, copied verbatim asRWS.hsto the same local directory as the file pasted above.
If I compile the code above and run it, I can see that it runs in constant space.
If I change the imports to compile and link the copy of RWS.hs
import Control.Monad (replicateM_)
-- import qualified Control.Monad.RWS.CPS as RWS
import Data.Monoid (Sum (..))
import qualified RWS
I get a space leak. The black retainer there is (103)<*>.\, and the blue is SYSTEM
Why can this be? Or how should I best debug this?
In both cases, I am compiling with ghc (no cabal) with -Wall -O2 -prof -fprof-auto -rtsopts -fexternal-interpreter. I can post more detailed compiler invocation and output lines if it were relevant.
Using ghc-8.4.4, transformers-0.5.5.0 and writer-cps-transformers-0.1.1.4. I know these versions are not current, but I am interested in knowing what is going on, rather on solving the actual leak, so I assume the versions aren't that relevant.

