Copyright | (c) 2014, Andrew G. Seniuk |
---|---|
License | BSD-style (see the file LICENSE) |
Maintainer | Andrew Seniuk <rasfar@gmail.com> |
Stability | provisional |
Portability | portable |
Safe Haskell | None |
Language | Haskell2010 |
This module provides an overloaded function, deepseqn
, for partially
(or fully) evaluating data structures to a given depth.
Depth-bounded analogues of deepseq
and force
deepseqn :: NFDataN a => Int -> a -> b -> b Source
evaluates deepseqn
n x yx
to depth n, before returning y
.
This is used when expression x
also appears in y
, but mere
evaluation of y
does not force the embedded x
subexpression
as deeply as we wish.
The name deepseqn
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,
traverses (evaluates) the entire
top deepseqn
nn
levels of the data structure.
A typical use is to ensure any exceptions hidden within lazy fields of a data structure do not leak outside the scope of the exception handler; another is to force evaluation of a data structure in one thread, before passing it to another thread (preventing work moving to the wrong threads). Unlike DeepSeq, potentially infinite coinductive data types are supported by principled bounding of deep evaluation.
It is also useful for diagnostic purposes when trying to understand
and manipulate space/time trade-offs in lazy code,
and as a less indiscriminate substitute for deepseq
.
Furthermore, deepseqn
can sometimes a better solution than
using stict fields in your data structures, because the
latter will behave strictly everywhere that its constructors
are used, instead of just where its laziness is problematic.
There may be possible applications to the prevention of resource leaks in lazy streaming, but I'm not certain.
forcen :: NFDataN a => Int -> a -> a Source
forcen
is a variant of deepseqn
that is useful in some circumstances.
forcen n x = deepseqn n x x
forcen n x
evaluates x
to depth n
, and then returns it.
Recall that, in common with all Haskell functions, forcen
only
performs evaluation when upstream demand actually occurs,
so essentially it turns shallow evaluation into depth-n
evaluation.
Class of things that can be evaluated to an arbitrary finite depth
A class of types that can be evaluated to arbitrary depth.
Nothing