{-# LANGUAGE BangPatterns, DoAndIfThenElse #-}{-|
Module : GHC.AssertNF
Copyright : (c) 2013 Joachim Breitner
License : BSD3
Maintainer : Joachim Breitner <mail@joachim-breitner.de>
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.
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'.
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.
-}moduleGHC.AssertNF(assertNF,assertNFNamed,assertNFHere,disableAssertNF,)whereimportGHC.HeapViewimportDebug.TraceimportControl.MonadimportData.FunctorimportText.PrintfimportLanguage.Haskell.TH(Q,Exp(AppE,VarE,LitE),Lit(StringL),Loc,location,loc_filename,loc_start,mkName)importData.IORefimportSystem.IO.Unsafe(unsafePerformIO)enabledRef::IORefBoolenabledRef=unsafePerformIO$newIORefTrue{-# NOINLINE enabledRef #-}-- Everything is in normal form, unless it is a-- thunks explicitly marked as such.-- Indirection are also considered to be in HNFisHNF::Closure->IOBoolisHNFc=docasecofThunkClosure{}->returnFalseAPClosure{}->returnFalseSelectorClosure{}->returnFalseBCOClosure{}->returnFalse_->returnTrue-- | The function 'assertNF' checks whether its argument is fully evaluated and-- deeply evaluated. If this is not the case, a warning is printed to the standard output,-- giving the number of thunks found and printing the shape of the unevaluated object:---- >> let x = 1 + 2-- >> let y = (x,x)-- >> assertNF y-- >Parameter not in normal form: 2 thunks found:-- >let t1 = _bco-- >in (t1,t1)-- >> x-- >3-- >> assertNF y-- >>--assertNF::a->IO()assertNF=assertNF'"Parameter not in normal form"-- | 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:---- >> assertNFNamed "y" y-- >y not in normal form: 2 thunks found:-- >let t1 = _bco-- >in (t1,t1)--assertNFNamed::String->a->IO()assertNFNamedvalName=assertNF'(valName++" not in normal form")-- | 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:---- >Parameter at Test.hs:18:1 not in normal form: 2 thunks found:-- >let t1 = _bco-- >in (t1,t1)--assertNFHere::QExpassertNFHere=dolocStr<-formatLoc<$>location-- We don't use ''assertNF here, so that this module can be used on a-- compiler that does not support TH.return$AppE(VarE(mkName"GHC.AssertNF.assertNFNamed"))(LitE(StringLlocStr))whereformatLoc::Loc->StringformatLocloc=letfile=loc_filenameloc(line,col)=loc_startlocinprintf"parameter at %s:%d:%d"filelinecolassertNF'::String->a->IO()assertNF'strx=doen<-readIORefenabledRefwhenen$dodepths<-assertNFBoxed0(asBoxx)unless(nulldepths)$dog<-buildHeapGraph(maximumdepths+3)()(asBoxx)-- +3 for good mesure; applications don't look good otherwisetraceIO$str++": "++show(lengthdepths)++" thunks found:\n"++ppHeapGraphgassertNFBoxed::Int->Box->IO[Int]assertNFBoxed!db=doc<-getBoxedClosureDatabnf<-isHNFcifnfthendoc'<-getBoxedClosureDatabconcat<$>mapM(assertNFBoxed(d+1))(allPtrsc')elsedoreturn[d]-- | Invoke this function at the top of your 'main' method to turn every call-- to 'assertNF' and its variants to noops.disableAssertNF::IO()disableAssertNF=writeIORefenabledRefFalse