HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Moduledeepseq-1.5.0.0Haskell2010

Control.DeepSeq

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

  • 3 classes
  • 7 values
  • Packagedeepseq-1.5.0.0
  • Exports10
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceDeepSeq.hs

NFData class

1 declaration
classclass NFData a where
#

A class of types that can be fully evaluated.

Methods

  • 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 !_ = ()
Instances109NFData, …

Helper functions

5 declarations
valuedeepseq :: 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.

valueforce :: 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
value($!!) :: NFData a => (a -> b) -> a -> b
#

the deep analogue of $!. In the expression f $!! x, x is fully evaluated before the function f is applied to it.

valuerwhnf :: 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

Liftings of the NFData class

0 declarations

For unary constructors

classclass (forall a. NFData a => NFData (f a)) => NFData1 (f :: Type -> Type) where
#

A class of functors that can be fully evaluated.

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

Methods

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

Instances43NFData1, …
valuernf1 :: (NFData1 f, NFData a) => f a -> ()
#

Lift the standard rnf function through the type constructor.

For binary constructors

classclass (forall a. NFData a => NFData1 (p a)) => NFData2 (p :: Type -> Type -> Type) where
#

A class of bifunctors that can be fully evaluated.

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

Methods

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

Instances15NFData2, …