HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Modulerebase-1.21.2Haskell2010

Rebase.Control.DeepSeq

  • 3 classes
  • 7 values
  • Packagerebase-1.21.2
  • Exports10
  • LanguageHaskell2010
  • LicenceMIT
  • SourceDeepSeq.hs
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.

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 !_ = ()
Instances186NFData, …
valuernf1 :: (NFData1 f, NFData a) => f a -> ()
#

Lift the standard rnf function through the type constructor.

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.

Instances57NFData1, …
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.

Instances17NFData2, …
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