Note [mkDmdSigForArity]
Consider
f x = if expensive x
then \y. blah1
else \y. blah2
We will analyse the body with demand C(1L), reflecting the single visible
argument x. But dmdAnal will return a DmdType looking like
DmdType fvs [x-dmd, y-dmd]
because it has seen two lambdas, \x and \y. Since the length of the argument
demands in a DmdSig gives the "threshold" for applying the signature
(see Note [DmdSig: demand signatures, and demand-sig arity] in GHC.Types.Demand)
we must trim that DmdType to just
DmdSig (DmdTypte fvs [x-dmd])
when making that DmdType into the DmdSig for f. This trimming is the job of
`mkDmdSigForArity`.
Alternative. An alternative would be be to ensure that if
(dmd_ty, e') = dmdAnal env subdmd e
then the length dmds in dmd_ty is always less than (or maybe equal to?) the
call-depth of subdmd. To do that we'd need to adjust the Lam case of dmdAnal.
Probably not hard, but a job for another day; see discussion on !12873, #23113,
and #21392. References 1
- DmdSig: demand signatures, and demand-sig arity GHC.Types.Demand
Referenced by 1
- GHC.Core.Opt.DmdAnal call site