Admin message

Due to a large amount of spam we do not allow new users to create repositories, they are "external" users. If you are a new user and want to create a repository, for example for forking GHC, open a new issue on ghc/ghc using the "get-verified" issue template

Improve newtype unwrapping
The typechecker has a very delicate story for unwrapping newtypes. Consider ``` newtype Rec = MkRec Rec newtype Rec1 a = MkRec (Rec1 a) -- NB: `a` is inferred to have role Phantom newtype Id a = MkId a ``` When trying to solve `ty1 ~R# ty2`, where `ty1` and/or `ty2` are headed by a newtype. * We first have a reflexivity check; see `Note [Eager reflexivity check]`. This catches `[W] Rec ~R# Rec`. Pretty ad-hoc but useful enough to implement. **Problem 1**: we sadly fail solve `[W] Rec1 Int ~R# Rec1 Bool`. But there a perfectly good way to solve it: just decompose. (Remember `Rec1`'s argument has phantom role.) * Next we attempt to unwrap newtypes, recursively; using `tcTopNormaliseNewTypeTF_maybe`. This function has a depth counter; if we get a loop in unrwrapping we do a no-op. **Problem 2:** So we sadly fail to solve `Id Rec ~R# Rec`, even though one unwrapping of `Id` would nail it. * When we unwrap, we bump a depth counter attached to the constraint; see `bumpReductionDepth` in `can_eq_newtype_nc`. **Problem 3:** it seems un-satisfying to have a *two* depth counters: one in `tcTopNormaliseNewTypeTF_maybe` and one in `can_eq_newtype_nc`. The latter is essential though; see `Note [Newtypes can blow the stack]`. * If we fail to unwrap a newtype equality, we try to decompose, as a last resort. See `Note [Unwrap newtypes first]` and `Note [Decomposing newtype equalities]` and the code in `canTyConApp`. ## Proposal I think we can both *simplify* the code and make it *typecheck some more programs*, solving Problems 1, 2, and 3 as follows: * Make `tcTopNormaliseNewTypeTF_maybe` do just one layer of newtype unwrapping. The iteration goes through `can_eq_nc`, and we use only the depth checking in `can_eq_newtype_nc`. Simpler. * Before attempting unwrapping, attempt "benign decomposition". (This replaces the eager-reflexivity check.) Given `N s1 s2 ~R# N t1 t2`, check that, for each argument `i`, * Either `si` = `ti` * Or N's `i`'th role is `Phantom` or `Representational`. In that case decomposition (which, remember, is always sound) is complete. * One wrinkle is that when unwrapping, say, `Id ty1 ~ ty2` we should loop around with `ty2 ~ ty1`, and not `ty1 ~ ty2`. Reason: the unwrapping test looks at `ty1` first, but we want to give both a chance. E.g. `Rec ~R# Id Rec`. Bingo. Would this be worth it if the code was much more complicated? No. But it is actually simpler in one place (unwrapping one layer only)! An the benign decomposition is only 6 lines of code. I like that. Note: * Recursive newtypes are very delicate and we are pretty certain we can't make a complete algorithm. * This proposal makes it bit more complete, while losing nothing, I believe. * But it'll still be defeated by, say ``` newtype Z1 = MkZ1 Z2 newtype Z2 = MkZ2 Z3 newtype Z3 = MkZ3 Z1 [W] Z1 ~ Z3 ``` becasue the unwrapping never gets in sync. * (Surprisingly, if the involves only two constructors rather than three, we'll nail it.)
issue