假设我有一个类型类、两个实例和一个定义如下的函数。
class C c where
newC :: String -> c
showC :: c -> String
data C1 = C1 String
instance C C1 where
newC string = C1 string
showC (C1 string) = string
data C2 = C2 String
instance C C2 where
newC string = C2 string
showC (C2 string) = string
topFn :: String -> String
topFn = showC . newC
加载时,GHC 会产生"不明确类型"错误,
Ambiguous type variable ‘c0’ arising from a use of ‘showC’
prevents the constraint ‘(C c0)’ from being solved.
Probable fix: use a type annotation to specify what ‘c0’ should be.
These potential instances exist:
instance C C1 -- Defined at ambigType.hs:8:10
instance C C2 -- Defined at ambigType.hs:14:10
我知道中间值可以是C1
的,也可以是C2
的,如果没有进一步的信息,GHC无法确定它应该是哪个。
编写两个版本的topFn
可以解决这个问题。
topFn1 :: String -> String
topFn1 = showC . (newC :: String -> C1)
topFn2 :: String -> String
topFn2 = showC . (newC :: String -> C2)
但由于这些本质上是相同的功能,这似乎是一个丑陋的解决方案。有没有更好的方法?
谢谢。
是的,使用 TypeApplications
、AllowAmbiguousTypes
、ExplicitForall
和 ScopedTypeVariables
扩展。然后,将topFn
修改为
topFn :: forall a. C a => String -> String
topFn = showC . newC @a
然后,您可以使用显式类型应用程序调用topFn
:topFn @C1 "calling the C1 variant"
和 topFn @C2 "calling the C2 variant"
。如果需要,可以定义:
topFn1 = topFn @C1
topFn2 = topFn @C2