Note [NON-BOTTOM-DICTS invariant]
It is a global invariant (not checkable by Lint) that
every non-newtype dictionary-typed expression is non-bottom.
These conditions are captured by GHC.Core.Type.isTerminatingType.
How are we so sure about this? Dictionaries are built by GHC in only two ways:
* A dictionary function (DFun), arising from an instance declaration.
DFuns do no computation: they always return a data constructor immediately.
See DFunUnfolding in GHC.Core. So the result of a call to a DFun is always
non-bottom.
Exception: newtype dictionaries.
Plus: see the Very Nasty Wrinkle in Note [Speculative evaluation]
in GHC.CoreToStg.Prep
* A superclass selection from some other dictionary. This is harder to guarantee:
see Note [Recursive superclasses] and Note [Solving superclass constraints]
in GHC.Tc.TyCl.Instance.
A bad Core-to-Core pass could invalidate this reasoning, but that's too bad.
It's still an invariant of Core programs generated by GHC from Haskell, and
Core-to-Core passes maintain it.
Why is it useful to know that dictionaries are non-bottom?
1. It justifies the use of `-XDictsStrict`;
see `GHC.Core.Types.Demand.strictifyDictDmd`
2. It means that (eq_sel d) is ok-for-speculation and thus
case (eq_sel d) of _ -> blah
can be discarded by the Simplifier. See these Notes:
Note [exprOkForSpeculation and type classes] in GHC.Core.Utils
Note[Speculative evaluation] in GHC.CoreToStg.Prep References 4
- exprOkForSpeculation and type classes GHC.Core.Utils
- Speculative evaluation GHC.CoreToStg.Prep
- Recursive superclasses GHC.Tc.TyCl.Instance
- Solving superclass constraints GHC.Tc.TyCl.Instance
Referenced by 4
- GHC.Core.Type call site
- exprOkForSpeculation and type classes GHC.Core.Utils
- Speculative evaluation GHC.CoreToStg.Prep
- GHC.Types.Demand call site