Document strict fields invariant
Suppose MkT :: !a -> T a
.
As far as I understand, strict fields are only a syntactic sugar for case
expressions in Core wrappers. In Core, it is possible form MkT undefined
, bypassing wrappers. While we don't expose workers directly in Haskell, there's no robust guarantee that will never happen. Wrappers are inlined; perhaps some kind of nefarious rewrite rule, an optimisation or a plugin could form the application of a worker MkT x
without forcing x
. This will pass Core Lint.
GHC relies on the fact that MkT
does not contain a thunk. Note [Adding evaluatedness info to pattern-bound variables]
means that we remove evaluation of fields known to be strict. I've also heard that the code generator can exploit strictness of fields, but I could not confirm. In this case things could really go wrong if there's an unexpected thunk.
This ticket is a proposal: let's add to the list of Core invariants that a datacon worker with a strict field must be called with an evaluated argument.
I guess a robust fix would be to track strictness in types as in the "Types are calling conventions" paper, but that's expensive.