Note [Demand examples]

GHC/Types/Demand.hs:2653 compiler

Here are some examples of the demand notation, specified in Note [Demand notation],
in action. In each case we give the demand on the variable `x`.

Demand on x    Example            Explanation
  1!A           seq x y             Evaluates `x` exactly once (`1`), but not
                                    any deeper (`A`), and discards the box (`!`).
  S!A           seq x (seq x y)     Twice the previous demand; hence eval'd
                                    more than once (`S` for strict).
  1!P(1!L,A)    fst x               Evaluates pair `x` exactly once, first
                                    component exactly once. No info that (`L`).
                                    Second component is absent. Discards boxes (`!`).
  1P(1L,A)      opq_fst x           Like fst, but all boxes are retained.
  SP(1!L,A)     opq_seq x (fst x)   Two evals of x but exactly one of its first component.
                                    Box of x retained, but box of first component discarded.
  1!C(1,L)      x $ 3               Evals x exactly once ( 1 ) and calls it
                                    exactly once ( C(1,_) ). No info on how the
                                    result is evaluated ( L ).
  MC(M,L)       maybe y x           Evals x at most once ( 1 ) and calls it at
                                    most once ( C(1,_) ). No info on how the
                                    result is evaluated ( L ).
  LP(SL,A)      map (+ fst x)       Evals x lazily and multiple times ( L ),
                                    but when it is evaluated, the first
                                    component is evaluated (strictly) as well.

In the examples above, `opq_fst` is an opaque wrapper around `fst`, i.e.

  opq_fst = fst
  {-# OPAQUE opq_fst #

References 1

Referenced by 2