GHC 没有正确解析类型类方法中的约束(ghc 错误/限制?)

GHC do not resolve correctly constraint in typeclass methods (ghc bug / limitation ?)

GHC 在我的一些 class 成员的默认实现中不解析/传播约束。这种行为真的很奇怪,在我看来这是一个错误。

有人可以帮助我/向我解释哪里出了问题吗?

  1. 我如何告诉 GHC 统一 class 声明中的 a 和 class 方法中的 a 以便定义不歧义
  2. 有什么我不明白的地方吗?

代码:

module Foo where
import Data.Proxy

data Stuff a = Stuff
  {content :: String}

class HasStuff a where
  stuff :: Stuff a

  -- This works
  useStuffOK :: Proxy a -> (Stuff a)
  useStuffOK _ = (stuff)

  -- those don't work, 
  -- (but I think ghc has all the information necessary to figure it out)
  useStuffBAD :: Proxy a -> (Stuff a, String)
  useStuffBAD _ = (stuff, content (stuff :: Stuff a))
  -- Could not deduce (HasStuff a1) arising from a use of ‘stuff’
  -- from the context (HasStuff a)
  --   bound by the class declaration for ‘HasStuff’
  --   at Test.hs:(7,1)-(17,45)

  useStuffBAD2 :: Proxy a -> String
  useStuffBAD2 _ = content (stuff :: Stuff a)
  -- Could not deduce (HasStuff a1) arising from a use of ‘stuff’
  -- from the context (HasStuff a)
  --   bound by the class declaration for ‘HasStuff’
  --   at Test.hs:(7,1)-(17,45)

instance HasStuff Int where
  stuff = Stuff "ok"

-- inference works here
x :: Stuff Int
x = stuff

-- works here too
x :: String
x = content (stuff :: Stuff Int)

谢谢

正如@epsilonhalbe 和@user2407038 所指出的,我缺少 ScopedTypeVariables 扩展