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

Modulerelude-1.2.0.0Haskell2010

Relude.DeepSeq

SPDX-License-Identifier : MIT Maintainer : Kowainik xrom.xkov@gmail.com Stability : Stable Portability : Portable

This module contains useful functions to evaluate expressions to weak-head normal form (WHNF) or just normal form (NF). Useful to force traces or errors inside monadic computations or to remove space leaks.

  • 1 class
  • 7 values
  • Packagerelude-1.2.0.0
  • Exports8
  • LanguageHaskell2010
  • LicenceMIT
  • SourceDeepSeq.hs

Control.DeepSeq reexports

4 declarations
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 !_ = ()
Instances144NFData, …
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.

Evaluation

4 declarations
valueevaluateNF :: (NFData a, MonadIO m) => a -> m a
#

Alias for evaluateWHNF . force with a clearer name.

Example4 expressions
let list = [1..5] :: [Int]:sprint listlist = _() <$ evaluateNF list:sprint listlist = [1,2,3,4,5]
valueevaluateNF_ :: (NFData a, MonadIO m) => a -> m ()
#

Alias for evaluateWHNF . rnf. Similar to evaluateNF but discards the resulting value.

Example4 expressions
let list = [1..5] :: [Int]:sprint listlist = _evaluateNF_ list:sprint listlist = [1,2,3,4,5]
valueevaluateWHNF :: MonadIO m => a -> m a
#

Lifted alias for evaluate with a clearer name.

Example4 expressions
let list = [1..5] :: [Int]:sprint listlist = _() <$ evaluateWHNF list:sprint listlist = 1 : _
valueevaluateWHNF_ :: MonadIO m => a -> m ()
#

Like evaluateWHNF but discards value.

Example4 expressions
let list = [1..5] :: [Int]:sprint listlist = _evaluateWHNF_ list:sprint listlist = 1 : _