Note [Understanding DmdType and DmdSig]
Demand types are sound approximations of an expression's semantics relative to
the incoming demand we put the expression under. Consider the following
expression:
\x y -> x `seq` (y, 2*x)
Here is a table with demand types resulting from different incoming demands we
put that expression under. Note the monotonicity; a stronger incoming demand
yields a more precise demand type:
incoming sub-demand | demand type
P(A) | <L><L>{}
C(1,C(1,P(L))) | <1P(L)><L>{}
C(1,C(1,1P(1P(L),A))) | <1P(A)><A>{}
Note that in the first example, the depth of the demand type was *higher* than
the arity of the incoming call demand due to the anonymous lambda.
The converse is also possible and happens when we unleash demand signatures.
In @f x y@, the incoming call demand on f has arity 2. But if all we have is a
demand signature with depth 1 for @f@ (which we can safely unleash, see below),
the demand type of @f@ under a call demand of arity 2 has a *lower* depth of 1.
So: Demand types are elicited by putting an expression under an incoming (call)
demand, the arity of which can be lower or higher than the depth of the
resulting demand type.
In contrast, a demand signature summarises a function's semantics *without*
immediately specifying the incoming demand it was produced under. Despite StrSig
being a newtype wrapper around DmdType, it actually encodes two things:
* The threshold (i.e., minimum arity) to unleash the signature
* A demand type that is sound to unleash when the minimum arity requirement is
met.
Here comes the subtle part: The threshold is encoded in the demand-sig arity!
So in mkDmdSigForArity we make sure to trim the list of argument demands to the
given threshold arity. Call sites will make sure that this corresponds to the
arity of the call demand that elicited the wrapped demand type. See also
Note [DmdSig: demand signatures, and demand-sig arity] References 1
- DmdSig: demand signatures, and demand-sig arity GHC.Types.Demand
Referenced by 8
- GHC.Types.Demand call site ×3
- GHC.Types.Cpr call site ×2
- idArity varies independently of dmdTypeDepth GHC.Core.Opt.DmdAnal
- Demand type Divergence GHC.Types.Demand
- DmdSig: demand signatures, and demand-sig arity GHC.Types.Demand