Implement "instance chains"
It would be useful to implement a version of "instance chains"  in GHC, which would eliminate the need for OVERLAPPING_INSTANCES in most (all practcial?) programs.
The idea is that programmers can explicitly group and order instances into an "instance chain". For example:
instance (Monad m) => StateM (StateT s m) s where ... else (MonadTrans t, StateM m s) => StateM (t m) s where ...
When GHC searches for instances, the instances in a chain are considered together and in order, starting with the first one:
- If the goal matches the current instance's head, then this instance is selected and the rest are ignored, as if they were not there;
- If the goal does not match the current instance's head, AND it does not unify with the current instance's head, then we skip the instance and proceed to the next member of the chain;
- If the goal does not match the current instance's head, but it does unify with it, then we cannot use this chain to solve the goal.
In summary: earlier instances in a chain "hide" later instances, and later instances can be reached only if we are sure that none of the previous instance will match.
|Component||Compiler (Type checker)|