在以下示例中,我希望GHC能够将变量a
约束为A
类型,因为这是我在instance EAmbiguous A
中所说的内容。但是当把它加载到ghci时,我得到:
使用'Main。$ dmcount'产生的模糊类型变量'a0'
有问题的代码:
{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FlexibleContexts #-}
import GHC.Generics (Rep, Generic)
import GHC.Base (Type)
class EAmbiguous a where
count :: Int
default count :: (Generic a, GenericAmbiguous (Rep a)) => Int
count = genericCount @(Rep a)
class GenericAmbiguous (a :: Type -> Type) where
genericCount :: Int
instance GenericAmbiguous (f p) where
genericCount = 10
data A = A deriving Generic
-- The error happens when defining a body-less instance:
-- • Ambiguous type variable ‘a0’ arising from a use of ‘Main.$dmcount’
-- prevents the constraint ‘(Generic a0)’ from being solved.
instance EAmbiguous A
我一直在看这个问题并且我确信这应该有用,但我显然遗漏了一些东西。所以我的问题是:为什么GHC无法解决a
到A
?