deepseq-1.4.4.0: Deep evaluation of data structures

Copyright(c) The University of Glasgow 2001-2009
LicenseBSD-style (see the file LICENSE)
Maintainerlibraries@haskell.org
Stabilitystable
Portabilityportable
Safe HaskellSafe
LanguageHaskell2010

Control.DeepSeq

Contents

Description

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: deepseq-1.1.0.0

Synopsis

NFData class

class NFData a where Source #

A class of types that can be fully evaluated.

Since: deepseq-1.1.0.0

Methods

rnf :: a -> () Source #

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

rnf :: (Generic a, GNFData Zero (Rep a)) => a -> () Source #

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 Bool Source # 
Instance details

Defined in Control.DeepSeq

Methods

rnf :: Bool -> () Source #

NFData Char Source # 
Instance details

Defined in Control.DeepSeq

Methods

rnf :: Char -> () Source #

NFData Double Source # 
Instance details

Defined in Control.DeepSeq

Methods

rnf :: Double -> () Source #

NFData Float Source # 
Instance details

Defined in Control.DeepSeq

Methods

rnf :: Float -> () Source #

NFData Int Source # 
Instance details

Defined in Control.DeepSeq

Methods

rnf :: Int -> () Source #

NFData Int8 Source # 
Instance details

Defined in Control.DeepSeq

Methods

rnf :: Int8 -> () Source #

NFData Int16 Source # 
Instance details

Defined in Control.DeepSeq

Methods

rnf :: Int16 -> () Source #

NFData Int32 Source # 
Instance details

Defined in Control.DeepSeq

Methods

rnf :: Int32 -> () Source #

NFData Int64 Source # 
Instance details

Defined in Control.DeepSeq

Methods

rnf :: Int64 -> () Source #

NFData Integer Source # 
Instance details

Defined in Control.DeepSeq

Methods

rnf :: Integer -> () Source #

NFData Natural Source #

Since: deepseq-1.4.0.0

Instance details

Defined in Control.DeepSeq

Methods

rnf :: Natural -> () Source #

NFData Ordering Source # 
Instance details

Defined in Control.DeepSeq

Methods

rnf :: Ordering -> () Source #

NFData Word Source # 
Instance details

Defined in Control.DeepSeq

Methods

rnf :: Word -> () Source #

NFData Word8 Source # 
Instance details

Defined in Control.DeepSeq

Methods

rnf :: Word8 -> () Source #

NFData Word16 Source # 
Instance details

Defined in Control.DeepSeq

Methods

rnf :: Word16 -> () Source #

NFData Word32 Source # 
Instance details

Defined in Control.DeepSeq

Methods

rnf :: Word32 -> () Source #

NFData Word64 Source # 
Instance details

Defined in Control.DeepSeq

Methods

rnf :: Word64 -> () Source #

NFData CallStack Source #

Since: deepseq-1.4.2.0

Instance details

Defined in Control.DeepSeq

Methods

rnf :: CallStack -> () Source #

NFData () Source # 
Instance details

Defined in Control.DeepSeq

Methods

rnf :: () -> () Source #

NFData TyCon Source #

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

Since: deepseq-1.4.0.0

Instance details

Defined in Control.DeepSeq

Methods

rnf :: TyCon -> () Source #

NFData Void Source #

Defined as rnf = absurd.

Since: deepseq-1.4.0.0

Instance details

Defined in Control.DeepSeq

Methods

rnf :: Void -> () Source #

NFData Unique Source #

Since: deepseq-1.4.0.0

Instance details

Defined in Control.DeepSeq

Methods

rnf :: Unique -> () Source #

NFData Version Source #

Since: deepseq-1.3.0.0

Instance details

Defined in Control.DeepSeq

Methods

rnf :: Version -> () Source #

NFData ThreadId Source #

Since: deepseq-1.4.0.0

Instance details

Defined in Control.DeepSeq

Methods

rnf :: ThreadId -> () Source #

NFData ExitCode Source #

Since: deepseq-1.4.2.0

Instance details

Defined in Control.DeepSeq

Methods

rnf :: ExitCode -> () Source #

NFData MaskingState Source #

Since: deepseq-1.4.4.0

Instance details

Defined in Control.DeepSeq

Methods

rnf :: MaskingState -> () Source #

NFData TypeRep Source #

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

Since: deepseq-1.4.0.0

Instance details

Defined in Control.DeepSeq

Methods

rnf :: TypeRep -> () Source #

NFData All Source #

Since: deepseq-1.4.0.0

Instance details

Defined in Control.DeepSeq

Methods

rnf :: All -> () Source #

NFData Any Source #

Since: deepseq-1.4.0.0

Instance details

Defined in Control.DeepSeq

Methods

rnf :: Any -> () Source #

NFData CChar Source #

Since: deepseq-1.4.0.0

Instance details

Defined in Control.DeepSeq

Methods

rnf :: CChar -> () Source #

NFData CSChar Source #

Since: deepseq-1.4.0.0

Instance details

Defined in Control.DeepSeq

Methods

rnf :: CSChar -> () Source #

NFData CUChar Source #

Since: deepseq-1.4.0.0

Instance details

Defined in Control.DeepSeq

Methods

rnf :: CUChar -> () Source #

NFData CShort Source #

Since: deepseq-1.4.0.0

Instance details

Defined in Control.DeepSeq

Methods

rnf :: CShort -> () Source #

NFData CUShort Source #

Since: deepseq-1.4.0.0

Instance details

Defined in Control.DeepSeq

Methods

rnf :: CUShort -> () Source #

NFData CInt Source #

Since: deepseq-1.4.0.0

Instance details

Defined in Control.DeepSeq

Methods

rnf :: CInt -> () Source #

NFData CUInt Source #

Since: deepseq-1.4.0.0

Instance details

Defined in Control.DeepSeq

Methods

rnf :: CUInt -> () Source #

NFData CLong Source #

Since: deepseq-1.4.0.0

Instance details

Defined in Control.DeepSeq

Methods

rnf :: CLong -> () Source #

NFData CULong Source #

Since: deepseq-1.4.0.0

Instance details

Defined in Control.DeepSeq

Methods

rnf :: CULong -> () Source #

NFData CLLong Source #

Since: deepseq-1.4.0.0

Instance details

Defined in Control.DeepSeq

Methods

rnf :: CLLong -> () Source #

NFData CULLong Source #

Since: deepseq-1.4.0.0

Instance details

Defined in Control.DeepSeq

Methods

rnf :: CULLong -> () Source #

NFData CBool Source #

Since: deepseq-1.4.3.0

Instance details

Defined in Control.DeepSeq

Methods

rnf :: CBool -> () Source #

NFData CFloat Source #

Since: deepseq-1.4.0.0

Instance details

Defined in Control.DeepSeq

Methods

rnf :: CFloat -> () Source #

NFData CDouble Source #

Since: deepseq-1.4.0.0

Instance details

Defined in Control.DeepSeq

Methods

rnf :: CDouble -> () Source #

NFData CPtrdiff Source #

Since: deepseq-1.4.0.0

Instance details

Defined in Control.DeepSeq

Methods

rnf :: CPtrdiff -> () Source #

NFData CSize Source #

Since: deepseq-1.4.0.0

Instance details

Defined in Control.DeepSeq

Methods

rnf :: CSize -> () Source #

NFData CWchar Source #

Since: deepseq-1.4.0.0

Instance details

Defined in Control.DeepSeq

Methods

rnf :: CWchar -> () Source #

NFData CSigAtomic Source #

Since: deepseq-1.4.0.0

Instance details

Defined in Control.DeepSeq

Methods

rnf :: CSigAtomic -> () Source #

NFData CClock Source #

Since: deepseq-1.4.0.0

Instance details

Defined in Control.DeepSeq

Methods

rnf :: CClock -> () Source #

NFData CTime Source #

Since: deepseq-1.4.0.0

Instance details

Defined in Control.DeepSeq

Methods

rnf :: CTime -> () Source #

NFData CUSeconds Source #

Since: deepseq-1.4.0.0

Instance details

Defined in Control.DeepSeq

Methods

rnf :: CUSeconds -> () Source #

NFData CSUSeconds Source #

Since: deepseq-1.4.0.0

Instance details

Defined in Control.DeepSeq

Methods

rnf :: CSUSeconds -> () Source #

NFData CFile Source #

Since: deepseq-1.4.0.0

Instance details

Defined in Control.DeepSeq

Methods

rnf :: CFile -> () Source #

NFData CFpos Source #

Since: deepseq-1.4.0.0

Instance details

Defined in Control.DeepSeq

Methods

rnf :: CFpos -> () Source #

NFData CJmpBuf Source #

Since: deepseq-1.4.0.0

Instance details

Defined in Control.DeepSeq

Methods

rnf :: CJmpBuf -> () Source #

NFData CIntPtr Source #

Since: deepseq-1.4.0.0

Instance details

Defined in Control.DeepSeq

Methods

rnf :: CIntPtr -> () Source #

NFData CUIntPtr Source #

Since: deepseq-1.4.0.0

Instance details

Defined in Control.DeepSeq

Methods

rnf :: CUIntPtr -> () Source #

NFData CIntMax Source #

Since: deepseq-1.4.0.0

Instance details

Defined in Control.DeepSeq

Methods

rnf :: CIntMax -> () Source #

NFData CUIntMax Source #

Since: deepseq-1.4.0.0

Instance details

Defined in Control.DeepSeq

Methods

rnf :: CUIntMax -> () Source #

NFData Fingerprint Source #

Since: deepseq-1.4.0.0

Instance details

Defined in Control.DeepSeq

Methods

rnf :: Fingerprint -> () Source #

NFData SrcLoc Source #

Since: deepseq-1.4.2.0

Instance details

Defined in Control.DeepSeq

Methods

rnf :: SrcLoc -> () Source #

NFData a => NFData [a] Source # 
Instance details

Defined in Control.DeepSeq

Methods

rnf :: [a] -> () Source #

NFData a => NFData (Maybe a) Source # 
Instance details

Defined in Control.DeepSeq

Methods

rnf :: Maybe a -> () Source #

NFData a => NFData (Ratio a) Source # 
Instance details

Defined in Control.DeepSeq

Methods

rnf :: Ratio a -> () Source #

NFData (Ptr a) Source #

Since: deepseq-1.4.2.0

Instance details

Defined in Control.DeepSeq

Methods

rnf :: Ptr a -> () Source #

NFData (FunPtr a) Source #

Since: deepseq-1.4.2.0

Instance details

Defined in Control.DeepSeq

Methods

rnf :: FunPtr a -> () Source #

NFData a => NFData (Complex a) Source # 
Instance details

Defined in Control.DeepSeq

Methods

rnf :: Complex a -> () Source #

NFData (Fixed a) Source #

Since: deepseq-1.3.0.0

Instance details

Defined in Control.DeepSeq

Methods

rnf :: Fixed a -> () Source #

NFData a => NFData (Min a) Source #

Since: deepseq-1.4.2.0

Instance details

Defined in Control.DeepSeq

Methods

rnf :: Min a -> () Source #

NFData a => NFData (Max a) Source #

Since: deepseq-1.4.2.0

Instance details

Defined in Control.DeepSeq

Methods

rnf :: Max a -> () Source #

NFData a => NFData (First a) Source #

Since: deepseq-1.4.2.0

Instance details

Defined in Control.DeepSeq

Methods

rnf :: First a -> () Source #

NFData a => NFData (Last a) Source #

Since: deepseq-1.4.2.0

Instance details

Defined in Control.DeepSeq

Methods

rnf :: Last a -> () Source #

NFData m => NFData (WrappedMonoid m) Source #

Since: deepseq-1.4.2.0

Instance details

Defined in Control.DeepSeq

Methods

rnf :: WrappedMonoid m -> () Source #

NFData a => NFData (Option a) Source #

Since: deepseq-1.4.2.0

Instance details

Defined in Control.DeepSeq

Methods

rnf :: Option a -> () Source #

NFData (StableName a) Source #

Since: deepseq-1.4.0.0

Instance details

Defined in Control.DeepSeq

Methods

rnf :: StableName a -> () Source #

NFData a => NFData (ZipList a) Source #

Since: deepseq-1.4.0.0

Instance details

Defined in Control.DeepSeq

Methods

rnf :: ZipList a -> () Source #

NFData a => NFData (Identity a) Source #

Since: deepseq-1.4.0.0

Instance details

Defined in Control.DeepSeq

Methods

rnf :: Identity a -> () Source #

NFData (IORef a) Source #

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

Since: deepseq-1.4.2.0

Instance details

Defined in Control.DeepSeq

Methods

rnf :: IORef a -> () Source #

NFData a => NFData (First a) Source #

Since: deepseq-1.4.0.0

Instance details

Defined in Control.DeepSeq

Methods

rnf :: First a -> () Source #

NFData a => NFData (Last a) Source #

Since: deepseq-1.4.0.0

Instance details

Defined in Control.DeepSeq

Methods

rnf :: Last a -> () Source #

NFData a => NFData (Dual a) Source #

Since: deepseq-1.4.0.0

Instance details

Defined in Control.DeepSeq

Methods

rnf :: Dual a -> () Source #

NFData a => NFData (Sum a) Source #

Since: deepseq-1.4.0.0

Instance details

Defined in Control.DeepSeq

Methods

rnf :: Sum a -> () Source #

NFData a => NFData (Product a) Source #

Since: deepseq-1.4.0.0

Instance details

Defined in Control.DeepSeq

Methods

rnf :: Product a -> () Source #

NFData a => NFData (Down a) Source #

Since: deepseq-1.4.0.0

Instance details

Defined in Control.DeepSeq

Methods

rnf :: Down a -> () Source #

NFData (MVar a) Source #

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

Since: deepseq-1.4.2.0

Instance details

Defined in Control.DeepSeq

Methods

rnf :: MVar a -> () Source #

NFData a => NFData (NonEmpty a) Source #

Since: deepseq-1.4.2.0

Instance details

Defined in Control.DeepSeq

Methods

rnf :: NonEmpty a -> () Source #

NFData (a -> b) Source #

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

Since: deepseq-1.3.0.0

Instance details

Defined in Control.DeepSeq

Methods

rnf :: (a -> b) -> () Source #

(NFData a, NFData b) => NFData (Either a b) Source # 
Instance details

Defined in Control.DeepSeq

Methods

rnf :: Either a b -> () Source #

(NFData a, NFData b) => NFData (a, b) Source # 
Instance details

Defined in Control.DeepSeq

Methods

rnf :: (a, b) -> () Source #

(NFData a, NFData b) => NFData (Array a b) Source # 
Instance details

Defined in Control.DeepSeq

Methods