GHC picks overlappable instance too early
GHC HEAD seems to pick OVERLAPPABLE instances too early under some circumstances.
Example with optics-core:
unknown@electronics optics $ cabal repl optics-core --allow-newer
Build profile: -w ghc-9.1.20210206 -O1
In order, the following will be built (use -v for more details):
- optics-core-0.4 (lib) (file src/Optics/Internal/Utils.hs changed)
Preprocessing library for optics-core-0.4..
GHCi, version 9.1.20210206: https://www.haskell.org/ghc/ :? for help
Loaded GHCi configuration from /home/unknown/.ghci
...
>>> let o1 :: Optic k NoIx s t a b; o1 = undefined
>>> let o2 :: Optic k NoIx s t a b; o2 = undefined
>>> :t o1 % o2
o1 % o2
:: ((TypeError ...), (TypeError ...)) =>
Optic (Join k l) '[] s t a b
in optics-core/src/Optics/Internal/Optic/Subtyping.hs
there is an OVERLAPPABLE instance Is k l
for custom type error, if it's commented out then the type of o1 % o2
is as expected:
>>> :t o1 % o2
o1 % o2
:: (Is l (Join k l), Is k (Join k l)) =>
Optic (Join k l) '[] s t a b
More practical demonstration:
without the OVERLAPPABLE Is
instance:
>>> data X = X { name :: String } deriving (Show, Generic)
>>> data Y = Y { name :: String, x :: X } deriving (Show, Generic)
>>> Y "hi" (X "there") ^. #name
"hi"
>>> Y "hi" (X "there") ^. #x % #name
"there"
with the OVERLAPPABLE Is
instance:
>>> data X = X { name :: String } deriving (Show, Generic)
>>> data Y = Y { name :: String, x :: X } deriving (Show, Generic)
>>> Y "hi" (X "there") ^. #name
"hi"
>>> Y "hi" (X "there") ^. #x % #name
<interactive>:34:26: error:
• A_Lens cannot be used as A_Lens
Perhaps you meant one of these:
‘view’ (from Optics.Getter)
‘over’ (from Optics.Setter)
‘set’ (from Optics.Setter)
‘(^.)’ ‘(%~)’ ‘(.~)’ (from Optics.Operators)
• In the second argument of ‘(^.)’, namely ‘#x % #name’
In the expression: Y "hi" (X "there") ^. #x % #name
In an equation for ‘it’: it = Y "hi" (X "there") ^. #x % #name
Here the OVERLAPPABLE Is k l
instance is picked even though there is a Is k k
instance that should be picked instead.
EDIT: This is HEAD regression, GHC <= 9.0 works as expected.