1 {-# LANGUAGE BangPatterns, DoAndIfThenElse #-}
5 Copyright : (c) 2013 Joachim Breitner
7 Maintainer : Joachim Breitner <mail@joachim-breitner.de>
9 To avoid space leaks and unwanted evaluation behaviour, the programmer might want his data to be fully evaluated at certians positions in the code. This can be enforced, for example, by ample use of "Control.DeepSeq", but this comes at a cost.
11 Experienced users hence use 'Control.DeepSeq.deepseq' only to find out about the existance of space leaks and optimize their code to not create the thunks in the first place, until the code no longer shows better performance with 'deepseq'.
13 This module provides an alternative approach: An explicit assertion about the evaluation state. If the programmer expect a certain value to be fully evaluated at a specific point of the program (e.g. before a call to 'writeIORef'), he can state that, and as long as assertions are enabled, this statement will be checked. In the production code the assertions can be disabled, to avoid the run-time cost.
32 import Language.Haskell.TH (Q, Exp(AppE,VarE,LitE), Lit(StringL), Loc, location, loc_filename, loc_start, mkName)
34 import System.IO.Unsafe ( unsafePerformIO )
36 enabledRef :: IORef Bool
37 enabledRef = unsafePerformIO $ newIORef True
38 {-# NOINLINE enabledRef #-}
40 -- Everything is in normal form, unless it is a
41 -- thunks explicitly marked as such.
42 -- Indirection are also considered to be in HNF
43 isHNF :: Closure -> IO Bool
46 ThunkClosure {} -> return False
47 APClosure {} -> return False
48 SelectorClosure {} -> return False
49 BCOClosure {} -> return False
52 -- | The function 'assertNF' checks whether its argument is fully evaluated and
53 -- deeply evaluated. If this is not the case, a warning is printed to the standard output,
54 -- giving the number of thunks found and printing the shape of the unevaluated object:
59 -- >Parameter not in normal form: 2 thunks found:
67 assertNF :: a -> IO ()
68 assertNF = assertNF' "Parameter not in normal form"
70 -- | In order to better identify the source of error messages from 'assertNF', this variant allows you to include a name that is printed in the output:
72 -- >> assertNFNamed "y" y
73 -- >y not in normal form: 2 thunks found:
77 assertNFNamed :: String -> a -> IO ()
78 assertNFNamed valName = assertNF' (valName ++ " not in normal form")
80 -- | This function, when called as @$assertNFHere@ in a module with @-XTemplateHaskell@ enabled, will cause the current filename and position be included in the error message:
82 -- >Parameter at Test.hs:18:1 not in normal form: 2 thunks found:
88 locStr <- formatLoc <$> location
89 -- We don't use ''assertNF here, so that this module can be used on a
90 -- compiler that does not support TH.
91 return $ AppE (VarE (mkName "GHC.AssertNF.assertNFNamed"))
92 (LitE (StringL locStr))
93 where formatLoc :: Loc -> String
94 formatLoc loc = let file = loc_filename loc
95 (line, col) = loc_start loc
96 in printf "parameter at %s:%d:%d" file line col
98 assertNF' :: String -> a -> IO ()
100 en <- readIORef enabledRef
102 depths <- assertNFBoxed 0 (asBox x)
103 unless (null depths) $ do
104 g <- buildHeapGraph (maximum depths + 3) () (asBox x)
105 -- +3 for good mesure; applications don't look good otherwise
106 traceIO $ str ++ ": " ++ show (length depths) ++ " thunks found:\n" ++
110 assertNFBoxed :: Int -> Box -> IO [Int]
111 assertNFBoxed !d b = do
112 c <- getBoxedClosureData b
116 c' <- getBoxedClosureData b
117 concat <$> mapM (assertNFBoxed (d+1)) (allPtrs c')
121 -- | Invoke this function at the top of your 'main' method to turn every call
122 -- to 'assertNF' and its variants to noops.
123 disableAssertNF :: IO ()
124 disableAssertNF = writeIORef enabledRef False
126 -- | A variant of 'assertNF' that does not print anything and just returns
127 -- 'True' if the value is in normal form, or 'False' otherwise. This function
128 -- is not affected by 'disableAssertNF'.
130 isNF x = isNFBoxed (asBox x)
132 isNFBoxed :: Box -> IO Bool
134 c <- getBoxedClosureData b
138 c' <- getBoxedClosureData b
139 allM isNFBoxed (allPtrs c')
143 -- From Control.Monad.Loops in monad-loops, but I'd like to avoid too many
144 -- trivial dependencies
145 allM :: (Monad m) => (a -> m Bool) -> [a] -> m Bool
146 allM _ [] = return True