I understand the following type family shouldn't and perhaps can't be implemented in GHC:
type family MatchesConstraint c a :: Bool where
MatchesConstraint c a is True if (c a)
MatchesConstraint c a is False otherwise
This is problematic because classes are open, so MatchesConstraint c a
could evaluate to True
for some parts of the program and False
in others depending on what instances are in scope, which I imagine could be potentially quite disastrous.
But consider the following:
type family MatchesConstraint c a :: Bool where
MatchesConstraint c a is True if (c a)
MatchesConstraint c a doesn't reduce otherwise
This seems quite safe. In some parts of our program we might fail to reduce if an instance is not in scope, but we'll never have an inconsistency.
Can I make something like this work in GHC?
The reason I'm asking for this, is because one could perhaps select instances based on not just type directly but class. Which could be a useful thing in some contexts I believe.