Community

Control.DeepSeq

stable

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).

NFData class

class NFData a

A class of types that can be fully evaluated.

rnf :: a -> ()

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 !_ = ()

Helper functions

deepseq :: NFData a => a -> b -> b

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.

force :: (NFData a) => a -> a

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

($!!) :: (NFData a) => (a -> b) -> a -> b

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.

(<$!!>) :: (Monad m, NFData b) => (a -> b) -> m a -> m b

Deeply strict version of <$>.

rwhnf :: a -> ()

Reduce to weak head normal form

Equivalent to \x -> seq x ().

Useful for defining NFData for types for which NF=WHNF holds.

data T = C1 | C2 | C3
instance NFData T where rnf = rwhnf

newtype Unit

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

A class of functors that can be fully evaluated.

In `deepseq-1.5.0.0` this class was updated to include superclasses.

liftRnf :: (a -> ()) -> f a -> ()

liftRnf should reduce its argument to normal form (that is, fully evaluate all sub-components), given an argument to reduce a arguments, and then return '()'.

See rnf for the generic deriving.

rnf1 :: (NFData1 f, NFData a) => f a -> ()

Lift the standard rnf function through the type constructor.

For binary constructors

class (forall a. NFData a => NFData1 (p a)) => NFData2 p

A class of bifunctors that can be fully evaluated.

In `deepseq-1.5.0.0` this class was updated to include superclasses.

liftRnf2 :: (a -> ()) -> (b -> ()) -> p a b -> ()

liftRnf2 should reduce its argument to normal form (that is, fully evaluate all sub-components), given functions to reduce a and b arguments respectively, and then return '()'.

Note: Unlike for the unary liftRnf, there is currently no support for generically deriving liftRnf2.

rnf2 :: (NFData2 p, NFData a, NFData b) => p a b -> ()

Lift the standard rnf function through the type constructor.

Instances

179
  • instance GNFData arity V1
  • instance GNFData arity U1
  • instance NFData a => GNFData arity (K1 i a)
  • instance GNFData arity a => GNFData arity (M1 i c a)
  • instance GNFData arity (URec a)
  • instance (GNFData arity a, GNFData arity b) => GNFData arity (a :*: b)
  • instance (GNFData arity a, GNFData arity b) => GNFData arity (a :+: b)
  • instance GNFData One Par1
  • instance NFData1 f => GNFData One (Rec1 f)
  • instance (NFData1 f, GNFData One g) => GNFData One (f :.: g)
  • instance Semigroup Unit
  • instance Monoid Unit
  • instance NFData Int
  • instance NFData Word
  • instance NFData Integer
  • instance NFData Float
  • instance NFData Double
  • instance NFData Char
  • instance NFData Bool
  • instance NFData Ordering
  • instance NFData ()
  • instance NFData Int8
  • instance NFData Int16
  • instance NFData Int32
  • instance NFData Int64
  • instance NFData Word8
  • instance NFData Word16
  • instance NFData Word32
  • instance NFData Word64
  • instance NFData MaskingState
  • instance NFData (Proxy a)
  • instance NFData1 Proxy
  • instance NFData (a :~: b)
  • instance NFData1 ((:~:) a)
  • instance NFData2 (:~:)
  • instance NFData (a :~~: b)
  • instance NFData1 ((:~~:) a)
  • instance NFData2 (:~~:)
  • instance NFData a => NFData (Identity a)
  • instance NFData1 Identity
  • instance NFData Void

    Defined as rnf = absurd.

  • instance NFData Natural
  • instance NFData (Fixed a)
  • instance NFData1 Fixed
  • instance NFData (a -> b)

    This instance is for convenience and consistency with seq. This assumes that WHNF is equivalent to NF for functions.

    @since 1.3.0.0 @deprecated 1.5.2.0

  • instance NFData1 Ratio

    Available on base >=4.9

  • instance (NFData1 f, NFData1 g) => NFData1 (Compose f g)
  • instance (NFData (f (g a))) => NFData (Compose f g a)

    Note: in deepseq-1.5.0.0 this instance's superclasses were changed.

  • instance (NFData1 f, NFData1 g) => NFData1 (Functor.Sum f g)
  • instance (NFData (f a), NFData (g a)) => NFData (Functor.Sum f g a)

    Note: in deepseq-1.5.0.0 this instance's superclasses were changed.

  • instance (NFData1 f, NFData1 g) => NFData1 (Functor.Product f g)
  • instance (NFData (f a), NFData (g a)) => NFData (Functor.Product f g a)

    Note: in deepseq-1.5.0.0 this instance's superclasses were changed.

  • instance NFData a => NFData (Ratio a)
  • instance (NFData a) => NFData (Complex a)
  • instance NFData a => NFData (Maybe a)
  • instance NFData1 Maybe
  • instance (NFData a, NFData b) => NFData (Either a b)
  • instance (NFData a) => NFData1 (Either a)
  • instance NFData2 Either
  • instance NFData Data.Version.Version
  • instance NFData a => NFData [a]
  • instance NFData1 []
  • instance NFData a => NFData (ZipList a)
  • instance NFData1 ZipList
  • instance NFData a => NFData (Const a b)
  • instance NFData a => NFData1 (Const a)
  • instance NFData2 Const
  • instance (NFData a, NFData b) => NFData (Array a b)
  • instance (NFData a) => NFData1 (Array a)
  • instance NFData2 Array
  • instance NFData a => NFData (Down a)
  • instance NFData1 Down
  • instance NFData a => NFData (Dual a)
  • instance NFData1 Dual
  • instance NFData a => NFData (Mon.First a)
  • instance NFData1 Mon.First
  • instance NFData a => NFData (Mon.Last a)
  • instance NFData1 Mon.Last
  • instance NFData Any
  • instance NFData All
  • instance NFData a => NFData (Sum a)
  • instance NFData1 Sum
  • instance NFData a => NFData (Product a)
  • instance NFData1 Product
  • instance NFData (StableName a)
  • instance NFData1 StableName
  • instance NFData ThreadId
  • instance NFData Unique
  • instance NFData TypeRep

    NOTE: Prior to deepseq-1.4.4.0 this instance was only defined for base-4.8.0.0 and later.

  • instance NFData TyCon

    NOTE: Prior to deepseq-1.4.4.0 this instance was only defined for base-4.8.0.0 and later.

  • instance NFData (Reflection.TypeRep a)
  • instance NFData Reflection.Module
  • instance NFData (IORef a)

    NOTE: Only strict in the reference and not the referenced value.

  • instance NFData1 IORef
  • instance NFData (STRef s a)

    NOTE: Only strict in the reference and not the referenced value.

  • instance NFData1 (STRef s)
  • instance NFData2 STRef
  • instance NFData (MVar a)

    NOTE: Only strict in the reference and not the referenced value.

  • instance NFData1 MVar
  • instance NFData Fingerprint
  • instance NFData (Ptr a)
  • instance NFData1 Ptr
  • instance NFData (FunPtr a)
  • instance NFData1 FunPtr
  • instance NFData CChar
  • instance NFData CSChar
  • instance NFData CUChar
  • instance NFData CShort
  • instance NFData CUShort
  • instance NFData CInt
  • instance NFData CUInt
  • instance NFData CLong
  • instance NFData CULong
  • instance NFData CPtrdiff
  • instance NFData CSize
  • instance NFData CWchar
  • instance NFData CSigAtomic
  • instance NFData CLLong
  • instance NFData CULLong
  • instance NFData CIntPtr
  • instance NFData CUIntPtr
  • instance NFData CIntMax
  • instance NFData CUIntMax
  • instance NFData CClock
  • instance NFData CTime
  • instance NFData CUSeconds
  • instance NFData CSUSeconds
  • instance NFData CFloat
  • instance NFData CDouble
  • instance NFData CFile
  • instance NFData CFpos
  • instance NFData CJmpBuf
  • instance NFData CBool
  • instance NFData ExitCode
  • instance NFData a => NFData (NonEmpty a)
  • instance NFData1 NonEmpty
  • instance NFData a => NFData (Min a)
  • instance NFData1 Min
  • instance NFData a => NFData (Max a)
  • instance NFData1 Max
  • instance (NFData a, NFData b) => NFData (Arg a b)
  • instance (NFData a) => NFData1 (Arg a)
  • instance NFData2 Arg
  • instance NFData a => NFData (Semi.First a)
  • instance NFData1 Semi.First
  • instance NFData a => NFData (Semi.Last a)
  • instance NFData1 Semi.Last
  • instance NFData m => NFData (WrappedMonoid m)
  • instance NFData1 WrappedMonoid
  • instance NFData SrcLoc
  • instance NFData CallStack
  • instance NFData a => NFData (Solo a)
  • instance NFData1 Solo
  • instance (NFData a, NFData b) => NFData (a, b)
  • instance (NFData a) => NFData1 ((,) a)
  • instance NFData2 (,)
  • instance (NFData a1, NFData a2, NFData a3) => NFData (a1, a2, a3)
  • instance (NFData a1, NFData a2) => NFData1 ((,,) a1 a2)
  • instance (NFData a1) => NFData2 ((,,) a1)
  • instance (NFData a1, NFData a2, NFData a3, NFData a4) => NFData (a1, a2, a3, a4)
  • instance (NFData a1, NFData a2, NFData a3) => NFData1 ((,,,) a1 a2 a3)
  • instance (NFData a1, NFData a2) => NFData2 ((,,,) a1 a2)
  • instance (NFData a1, NFData a2, NFData a3, NFData a4, NFData a5) => NFData (a1, a2, a3, a4, a5)
  • instance (NFData a1, NFData a2, NFData a3, NFData a4) => NFData1 ((,,,,) a1 a2 a3 a4)
  • instance (NFData a1, NFData a2, NFData a3) => NFData2 ((,,,,) a1 a2 a3)
  • instance (NFData a1, NFData a2, NFData a3, NFData a4, NFData a5, NFData a6) => NFData (a1, a2, a3, a4, a5, a6)
  • instance (NFData a1, NFData a2, NFData a3, NFData a4, NFData a5) => NFData1 ((,,,,,) a1 a2 a3 a4 a5)
  • instance (NFData a1, NFData a2, NFData a3, NFData a4) => NFData2 ((,,,,,) a1 a2 a3 a4)
  • instance (NFData a1, NFData a2, NFData a3, NFData a4, NFData a5, NFData a6, NFData a7) => NFData (a1, a2, a3, a4, a5, a6, a7)
  • instance (NFData a1, NFData a2, NFData a3, NFData a4, NFData a5, NFData a6) => NFData1 ((,,,,,,) a1 a2 a3 a4 a5 a6)
  • instance (NFData a1, NFData a2, NFData a3, NFData a4, NFData a5) => NFData2 ((,,,,,,) a1 a2 a3 a4 a5)
  • instance (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)
  • instance (NFData a1, NFData a2, NFData a3, NFData a4, NFData a5, NFData a6, NFData a7) => NFData1 ((,,,,,,,) a1 a2 a3 a4 a5 a6 a7)
  • instance (NFData a1, NFData a2, NFData a3, NFData a4, NFData a5, NFData a6) => NFData2 ((,,,,,,,) a1 a2 a3 a4 a5 a6)
  • instance (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)
  • instance (NFData a1, NFData a2, NFData a3, NFData a4, NFData a5, NFData a6, NFData a7, NFData a8) => NFData1 ((,,,,,,,,) a1 a2 a3 a4 a5 a6 a7 a8)
  • instance (NFData a1, NFData a2, NFData a3, NFData a4, NFData a5, NFData a6, NFData a7) => NFData2 ((,,,,,,,,) a1 a2 a3 a4 a5 a6 a7)
  • instance NFData ByteArray
  • instance NFData (MutableByteArray s)
Maintainer
libraries@haskell.org
Portability
portable
Copyright
(c) The University of Glasgow 2001-2009
License
BSD-style (see the file LICENSE)
Build record and source files

Community contribution. The service has not verified this documentation.

Target: Linux on x86-64. Some inferred types and resolved links are not available.

Generator: aihc-haddock-aa6debbd-web-2

Metadata snapshot: 1bd743fbfc06a3aa9f80a460c5c49f76c161b787a33bdf95b66a8bd2e8e4308b

Open the dependency plan · Download the documentation model

Import a package

Enter a package name and an exact version from Hackage. The site contains imported versions only.