Copyright | (c) The University of Glasgow 2001-2009 |
---|---|
License | BSD-style (see the file LICENSE) |
Maintainer | libraries@haskell.org |
Stability | stable |
Portability | portable |
Safe Haskell | Trustworthy |
Language | Haskell2010 |
This module provides overloaded functions, such as deepseq
and
rnf
, for fully evaluating data structures (that is, evaluating to
"Normal Form").
A typical use is to prevent resource leaks in lazy IO programs, by forcing all characters from a file to be read. For example:
import System.IO import Control.DeepSeq import Control.Exception (evaluate) readFile' :: FilePath -> IO String readFile' fn = do h <- openFile fn ReadMode s <- hGetContents h evaluate (rnf s) hClose h return s
Note: The example above should rather be written in terms of
bracket
to ensure releasing file-descriptors in
a timely matter (see the description of force
for an example).
deepseq
differs from seq
as it traverses data structures deeply,
for example, seq
will evaluate only to the first constructor in
the list:
> [1,2,undefined] `seq` 3 3
While deepseq
will force evaluation of all the list elements:
> [1,2,undefined] `deepseq` 3 *** Exception: Prelude.undefined
Another common use is to ensure any exceptions hidden within lazy fields of a data structure do not leak outside the scope of the exception handler, or to force evaluation of a data structure in one thread, before passing to another thread (preventing work moving to the wrong threads).
Since: 1.1.0.0
Synopsis
- class NFData a where
- rnf :: a -> ()
- deepseq :: NFData a => a -> b -> b
- force :: NFData a => a -> a
- ($!!) :: NFData a => (a -> b) -> a -> b
- (<$!!>) :: (Monad m, NFData b) => (a -> b) -> m a -> m b
- rwhnf :: a -> ()
- newtype Unit = Unit {
- runUnit :: ()
- class (forall a. NFData a => NFData (f a)) => NFData1 f where
- liftRnf :: (a -> ()) -> f a -> ()
- rnf1 :: (NFData1 f, NFData a) => f a -> ()
- class (forall a. NFData a => NFData1 (p a)) => NFData2 p where
- liftRnf2 :: (a -> ()) -> (b -> ()) -> p a b -> ()
- rnf2 :: (NFData2 p, NFData a, NFData b) => p a b -> ()
NFData
class
A class of types that can be fully evaluated.
Since: 1.1.0.0
Nothing
rnf
should reduce its argument to normal form (that is, fully
evaluate all sub-components), and then return ()
.
Generic
NFData
deriving
Starting with GHC 7.2, you can automatically derive instances
for types possessing a Generic
instance.
Note: Generic1
can be auto-derived starting with GHC 7.4
{-# LANGUAGE DeriveGeneric #-} import GHC.Generics (Generic, Generic1) import Control.DeepSeq data Foo a = Foo a String deriving (Eq, Generic, Generic1) instance NFData a => NFData (Foo a) instance NFData1 Foo data Colour = Red | Green | Blue deriving Generic instance NFData Colour
Starting with GHC 7.10, the example above can be written more
concisely by enabling the new DeriveAnyClass
extension:
{-# LANGUAGE DeriveGeneric, DeriveAnyClass #-} import GHC.Generics (Generic) import Control.DeepSeq data Foo a = Foo a String deriving (Eq, Generic, Generic1, NFData, NFData1) data Colour = Red | Green | Blue deriving (Generic, NFData)
Compatibility with previous deepseq
versions
Prior to version 1.4.0.0, the default implementation of the rnf
method was defined as
rnf
a =seq
a ()
However, starting with deepseq-1.4.0.0
, the default
implementation is based on DefaultSignatures
allowing for
more accurate auto-derived NFData
instances. If you need the
previously used exact default rnf
method implementation
semantics, use
instance NFData Colour where rnf x = seq x ()
or alternatively
instance NFData Colour where rnf = rwhnf
or
{-# LANGUAGE BangPatterns #-} instance NFData Colour where rnf !_ = ()
Instances
NFData ByteArray Source # | Since: 1.4.7.0 |
Defined in Control.DeepSeq | |
NFData All Source # | Since: 1.4.0.0 |
Defined in Control.DeepSeq | |
NFData Any Source # | Since: 1.4.0.0 |
Defined in Control.DeepSeq | |
NFData TypeRep Source # | NOTE: Prior to Since: 1.4.0.0 |
Defined in Control.DeepSeq | |
NFData Unique Source # | Since: 1.4.0.0 |
Defined in Control.DeepSeq | |
NFData Version Source # | Since: 1.3.0.0 |
Defined in Control.DeepSeq | |
NFData CBool Source # | Since: 1.4.3.0 |
Defined in Control.DeepSeq | |
NFData CChar Source # | Since: 1.4.0.0 |
Defined in Control.DeepSeq | |
NFData CClock Source # | Since: 1.4.0.0 |
Defined in Control.DeepSeq | |
NFData CDouble Source # | Since: 1.4.0.0 |
Defined in Control.DeepSeq | |
NFData CFile Source # | Since: 1.4.0.0 |
Defined in Control.DeepSeq | |
NFData CFloat Source # | Since: 1.4.0.0 |
Defined in Control.DeepSeq | |
NFData CFpos Source # | Since: 1.4.0.0 |
Defined in Control.DeepSeq | |
NFData CInt Source # | Since: 1.4.0.0 |
Defined in Control.DeepSeq | |
NFData CIntMax Source # | Since: 1.4.0.0 |
Defined in Control.DeepSeq | |
NFData CIntPtr Source # | Since: 1.4.0.0 |
Defined in Control.DeepSeq | |
NFData CJmpBuf Source # | Since: 1.4.0.0 |
Defined in Control.DeepSeq | |
NFData CLLong Source # | Since: 1.4.0.0 |
Defined in Control.DeepSeq | |
NFData CLong Source # | Since: 1.4.0.0 |
Defined in Control.DeepSeq | |
NFData CPtrdiff Source # | Since: 1.4.0.0 |
Defined in Control.DeepSeq | |
NFData CSChar Source # | Since: 1.4.0.0 |
Defined in Control.DeepSeq | |
NFData CSUSeconds Source # | Since: 1.4.0.0 |
Defined in Control.DeepSeq rnf :: CSUSeconds -> () Source # | |
NFData CShort Source # | Since: 1.4.0.0 |
Defined in Control.DeepSeq | |
NFData CSigAtomic Source # | Since: 1.4.0.0 |
Defined in Control.DeepSeq rnf :: CSigAtomic -> () Source # | |
NFData CSize Source # | Since: 1.4.0.0 |
Defined in Control.DeepSeq | |
NFData CTime Source # | Since: 1.4.0.0 |
Defined in Control.DeepSeq | |
NFData CUChar Source # | Since: 1.4.0.0 |
Defined in Control.DeepSeq | |
NFData CUInt Source # | Since: 1.4.0.0 |
Defined in Control.DeepSeq | |
NFData CUIntMax Source # | Since: 1.4.0.0 |
Defined in Control.DeepSeq | |
NFData CUIntPtr Source # | Since: 1.4.0.0 |
Defined in Control.DeepSeq | |
NFData CULLong Source # | Since: 1.4.0.0 |
Defined in Control.DeepSeq | |
NFData CULong Source # | Since: 1.4.0.0 |
Defined in Control.DeepSeq | |
NFData CUSeconds Source # | Since: 1.4.0.0 |
Defined in Control.DeepSeq | |
NFData CUShort Source # | Since: 1.4.0.0 |
Defined in Control.DeepSeq | |
NFData CWchar Source # | Since: 1.4.0.0 |
Defined in Control.DeepSeq | |
NFData Void Source # | Since: 1.4.0.0 |
Defined in Control.DeepSeq | |
NFData ThreadId Source # | Since: 1.4.0.0 |
Defined in Control.DeepSeq | |
NFData Fingerprint Source # | Since: 1.4.0.0 |
Defined in Control.DeepSeq rnf :: Fingerprint -> () Source # | |
NFData MaskingState Source # | Since: 1.4.4.0 |
Defined in Control.DeepSeq rnf :: MaskingState -> () Source # | |
NFData ExitCode Source # | Since: 1.4.2.0 |
Defined in Control.DeepSeq | |
NFData Int16 Source # | |
Defined in Control.DeepSeq | |
NFData Int32 Source # | |
Defined in Control.DeepSeq | |
NFData Int64 Source # | |
Defined in Control.DeepSeq | |
NFData Int8 Source # | |
Defined in Control.DeepSeq | |
NFData CallStack Source # | Since: 1.4.2.0 |
Defined in Control.DeepSeq | |
NFData SrcLoc Source # | Since: 1.4.2.0 |
Defined in Control.DeepSeq | |
NFData Word16 Source # | |
Defined in Control.DeepSeq | |
NFData Word32 Source # | |
Defined in Control.DeepSeq | |
NFData Word64 Source # | |
Defined in Control.DeepSeq | |
NFData Word8 Source # | |
Defined in Control.DeepSeq | |
NFData Module Source # | Since: 1.4.8.0 |
Defined in Control.DeepSeq | |
NFData Ordering Source # | |
Defined in Control.DeepSeq | |
NFData TyCon Source # | NOTE: Prior to Since: 1.4.0.0 |
Defined in Control.DeepSeq | |
NFData Integer Source # | |
Defined in Control.DeepSeq | |
NFData Natural Source # | Since: 1.4.0.0 |
Defined in Control.DeepSeq | |
NFData () Source # | |
Defined in Control.DeepSeq | |
NFData Bool Source # | |
Defined in Control.DeepSeq | |
NFData Char Source # | |
Defined in Control.DeepSeq | |
NFData Double Source # | |
Defined in Control.DeepSeq | |
NFData Float Source # | |
Defined in Control.DeepSeq | |
NFData Int Source # | |
Defined in Control.DeepSeq | |
NFData Word Source # | |
Defined in Control.DeepSeq | |
NFData a => NFData (ZipList a) Source # | Since: 1.4.0.0 |
Defined in Control.DeepSeq | |
NFData (MutableByteArray s) Source # | Since: 1.4.8.0 |
Defined in Control.DeepSeq rnf :: MutableByteArray s -> () Source # | |
NFData a => NFData (Complex a) Source # | |
Defined in Control.DeepSeq | |
NFData a => NFData (Identity a) Source # | Since: 1.4.0.0 |
Defined in Control.DeepSeq | |
NFData a => NFData (First a) Source # | Since: 1.4.0.0 |
Defined in Control.DeepSeq | |
NFData a => NFData (Last a) Source # | Since: 1.4.0.0 |
Defined in Control.DeepSeq | |
NFData a => NFData (Down a) Source # | Since: 1.4.0.0 |
Defined in Control.DeepSeq | |
NFData a => NFData (First a) Source # | Since: 1.4.2.0 |
Defined in Control.DeepSeq | |
NFData a => NFData (Last a) Source # | Since: 1.4.2.0 |
Defined in Control.DeepSeq | |
NFData a => NFData (Max a) Source # | Since: 1.4.2.0 |
Defined in Control.DeepSeq | |
NFData a => NFData (Min a) Source # | Since: 1.4.2.0 |
Defined in Control.DeepSeq | |
NFData m => NFData (WrappedMonoid m) Source # | Since: 1.4.2.0 |
Defined in Control.DeepSeq rnf :: WrappedMonoid m -> () Source # | |
NFData a => NFData (Dual a) Source # | Since: 1.4.0.0 |
Defined in Control.DeepSeq | |
NFData a => NFData (Product a) Source # | Since: 1.4.0.0 |
Defined in Control.DeepSeq | |
NFData a => NFData (Sum a) Source # | Since: 1.4.0.0 |
Defined in Control.DeepSeq | |
NFData a => NFData (NonEmpty a) Source # | Since: 1.4.2.0 |
Defined in Control.DeepSeq | |
NFData (IORef a) Source # | NOTE: Only strict in the reference and not the referenced value. Since: 1.4.2.0 |
Defined in Control.DeepSeq | |
NFData (MVar a) Source # | NOTE: Only strict in the reference and not the referenced value. Since: 1.4.2.0 |
Defined in Control.DeepSeq | |
NFData (FunPtr a) Source # | Since: 1.4.2.0 |
Defined in Control.DeepSeq | |
NFData (Ptr a) Source # | Since: 1.4.2.0 |
Defined in Control.DeepSeq | |
NFData a => NFData (Ratio a) Source # | |
Defined in Control.DeepSeq | |
NFData (StableName a) Source # | Since: 1.4.0.0 |
Defined in Control.DeepSeq rnf :: StableName a -> () Source # | |
NFData a => NFData (Maybe a) Source # | |
Defined in Control.DeepSeq | |
NFData a => NFData (a) Source # | Since: 1.4.6.0 |
Defined in Control.DeepSeq | |
NFData a => NFData [a] Source # | |
Defined in Control.DeepSeq | |
(NFData a, NFData b) => NFData (Either a b) Source # | |
Defined in Control.DeepSeq | |
NFData (Fixed a) Source # | Since: 1.3.0.0 |
Defined in Control.DeepSeq | |
NFData (Proxy a) Source # | Since: 1.4.0.0 |
Defined in Control.DeepSeq | |
(NFData a, NFData b) => NFData (Arg a b) Source # | Since: 1.4.2.0 |
Defined in Control.DeepSeq | |
NFData (TypeRep a) Source # | Since: 1.4.8.0 |
Defined in Control.DeepSeq | |
(NFData a, NFData b) => NFData (Array a b) Source # | |
Defined in Control.DeepSeq | |
NFData (STRef s a) Source # | NOTE: Only strict in the reference and not the referenced value. Since: 1.4.2.0 |
Defined in Control.DeepSeq | |
(NFData a, NFData b) => NFData (a, b) Source # | |
Defined in Control.DeepSeq | |
NFData (a -> b) Source # | This instance is for convenience and consistency with Since: 1.3.0.0 |
Defined in Control.DeepSeq | |
NFData a => NFData (Const a b) Source # | Since: 1.4.0.0 |
Defined in Control.DeepSeq | |
NFData (a :~: b) Source # | Since: 1.4.3.0 |
Defined in Control.DeepSeq | |
(NFData a1, NFData a2, NFData a3) => NFData (a1, a2, a3) Source # | |
Defined in Control.DeepSeq | |
(NFData (f a), NFData (g a)) => NFData (Product f g a) Source # | Note: in Since: 1.4.3.0 |
Defined in Control.DeepSeq | |
(NFData (f a), NFData (g a)) => NFData (Sum f g a) Source # | Note: in Since: 1.4.3.0 |
Defined in Control.DeepSeq | |
NFData (a :~~: b) Source # | Since: 1.4.3.0 |
Defined in Control.DeepSeq | |
(NFData a1, NFData a2, NFData a3, NFData a4) => NFData (a1, a2, a3, a4) Source # | |
Defined in Control.DeepSeq | |
NFData (f (g a)) => NFData (Compose f g a) Source # | Note: in Since: 1.4.3.0 |
Defined in Control.DeepSeq | |
(NFData a1, NFData a2, NFData a3, NFData a4, NFData a5) => NFData (a1, a2, a3, a4, a5) Source # | |
Defined in Control.DeepSeq | |
(NFData a1, NFData a2, NFData a3, NFData a4, NFData a5, NFData a6) => NFData (a1, a2, a3, a4, a5, a6) Source # | |
Defined in Control.DeepSeq | |
(NFData a1, NFData a2, NFData a3, NFData a4, NFData a5, NFData a6, NFData a7) => NFData (a1, a2, a3, a4, a5, a6, a7) Source # | |
Defined in Control.DeepSeq | |
(NFData a1, NFData a2, NFData a3, NFData a4, NFData a5, NFData a6, NFData a7, NFData a8) => NFData (a1, a2, a3, a4, a5, a6, a7, a8) Source # | |
Defined in Control.DeepSeq | |
(NFData a1, NFData a2, NFData a3, NFData a4, NFData a5, NFData a6, NFData a7, NFData a8, NFData a9) => NFData (a1, a2, a3, a4, a5, a6, a7, a8, a9) Source # | |
Defined in Control.DeepSeq |
Helper functions
deepseq :: NFData a => a -> b -> b infixr 0 Source #
deepseq
: fully evaluates the first argument, before returning the
second.
The name deepseq
is used to illustrate the relationship to seq
:
where seq
is shallow in the sense that it only evaluates the top
level of its argument, deepseq
traverses the entire data structure
evaluating it completely.
deepseq
can be useful for forcing pending exceptions,
eradicating space leaks, or forcing lazy I/O to happen. It is
also useful in conjunction with parallel Strategies (see the
parallel
package).
There is no guarantee about the ordering of evaluation. The
implementation may evaluate the components of the structure in
any order or in parallel. To impose an actual order on
evaluation, use pseq
from Control.Parallel in the
parallel
package.
Since: 1.1.0.0
force :: NFData a => a -> a Source #
A variant of deepseq
that is useful in some circumstances:
force x = x `deepseq` x
force x
fully evaluates x
, and then returns it. Note that
force x
only performs evaluation when the value of force x
itself is demanded, so essentially it turns shallow evaluation into
deep evaluation.
force
can be conveniently used in combination with ViewPatterns
:
{-# LANGUAGE BangPatterns, ViewPatterns #-} import Control.DeepSeq someFun :: ComplexData -> SomeResult someFun (force -> !arg) = {- 'arg' will be fully evaluated -}
Another useful application is to combine force
with
evaluate
in order to force deep evaluation
relative to other IO
operations:
import Control.Exception (evaluate) import Control.DeepSeq main = do result <- evaluate $ force $ pureComputation {- 'result' will be fully evaluated at this point -} return ()
Finally, here's an exception safe variant of the readFile'
example:
readFile' :: FilePath -> IO String readFile' fn = bracket (openFile fn ReadMode) hClose $ \h -> evaluate . force =<< hGetContents h
Since: 1.2.0.0
($!!) :: NFData a => (a -> b) -> a -> b infixr 0 Source #
The deep analogue of $!
. f $!! x
fully evaluates x
before returning f x
.
There is no guarantee about the ordering of evaluation.
f x
may be evaluated before x
is fully evaluated.
To impose an actual order on evaluation, use pseq
from
Control.Parallel in the parallel
package.
Since: 1.2.0.0
(<$!!>) :: (Monad m, NFData b) => (a -> b) -> m a -> m b infixl 4 Source #
Deeply strict version of <$>
.
Since: 1.4.3.0
A monoid with (<>) = seq
.
Its purpose is to allow reducing structures to normal form using
foldMap
-like functions.
Examples
data Three a = Three a a a instance Foldable Three where foldMap f (Three x1 x2 x3) = f x1 <> f x2 <> f x3 instance NFData a => NFData (Three a) where rnf = runUnit . foldMap (Unit . rnf)
data Tree a b = NodeA a [Tree a b] | NodeB b [Tree a b] foldMapTree :: Monoid m => (a -> m) -> (b -> m) -> Tree a b -> m foldMapTree f g = go where go (NodeA x ts) = f x <> foldMap go ts go (NodeB y ts) = g y <> foldMap go ts instance NFData2 Tree where liftRnf2 r r' = runUnit . foldMapTree (Unit . r) (Unit . r')
Liftings of the NFData
class
For unary constructors
class (forall a. NFData a => NFData (f a)) => NFData1 f where Source #
A class of functors that can be fully evaluated.
In `deepseq-1.5.0.0` this class was updated to include superclasses.
Since: 1.4.3.0
Nothing
Instances
rnf1 :: (NFData1 f, NFData a) => f a -> () Source #
Lift the standard rnf
function through the type constructor.
Since: 1.4.3.0
For binary constructors
class (forall a. NFData a => NFData1 (p a)) => NFData2 p where Source #
A class of bifunctors that can be fully evaluated.
In `deepseq-1.5.0.0` this class was updated to include superclasses.
Since: 1.4.3.0
Instances
NFData2 Either Source # | Since: 1.4.3.0 |
Defined in Control.DeepSeq | |
NFData2 Arg Source # | Since: 1.4.3.0 |
Defined in Control.DeepSeq | |
NFData2 Array Source # | Since: 1.4.3.0 |
Defined in Control.DeepSeq | |
NFData2 STRef Source # | Since: 1.4.3.0 |
Defined in Control.DeepSeq | |
NFData2 (,) Source # | Since: 1.4.3.0 |
Defined in Control.DeepSeq | |
NFData2 (Const :: Type -> Type -> Type) Source # | Since: 1.4.3.0 |
Defined in Control.DeepSeq | |
NFData2 ((:~:) :: Type -> Type -> Type) Source # | Since: 1.4.3.0 |
Defined in Control.DeepSeq | |
NFData a1 => NFData2 ((,,) a1) Source # | Since: 1.4.3.0 |
Defined in Control.DeepSeq | |
NFData2 ((:~~:) :: Type -> Type -> Type) Source # | Since: 1.4.3.0 |
Defined in Control.DeepSeq | |
(NFData a1, NFData a2) => NFData2 ((,,,) a1 a2) Source # | Since: 1.4.3.0 |
Defined in Control.DeepSeq | |
(NFData a1, NFData a2, NFData a3) => NFData2 ((,,,,) a1 a2 a3) Source # | Since: 1.4.3.0 |
Defined in Control.DeepSeq | |
(NFData a1, NFData a2, NFData a3, NFData a4) => NFData2 ((,,,,,) a1 a2 a3 a4) Source # | Since: 1.4.3.0 |
Defined in Control.DeepSeq | |
(NFData a1, NFData a2, NFData a3, NFData a4, NFData a5) => NFData2 ((,,,,,,) a1 a2 a3 a4 a5) Source # | Since: 1.4.3.0 |
Defined in Control.DeepSeq | |
(NFData a1, NFData a2, NFData a3, NFData a4, NFData a5, NFData a6) => NFData2 ((,,,,,,,) a1 a2 a3 a4 a5 a6) Source # | Since: 1.4.3.0 |
Defined in Control.DeepSeq | |
(NFData a1, NFData a2, NFData a3, NFData a4, NFData a5, NFData a6, NFData a7) => NFData2 ((,,,,,,,,) a1 a2 a3 a4 a5 a6 a7) Source # | Since: 1.4.3.0 |
Defined in Control.DeepSeq |