以下测试让我感到意外
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE UndecidableInstances #-}
module Test where
class Type t where
encodeToField :: t -> String
class Rec r where
encodeToRec :: r -> String
data X a b = X a b
instance (Type t) => Rec t where
encodeToRec = encodeToField
instance (Type t, Rec r) => Rec (X t r) where
encodeToRec (X t r) =
let x = encodeToField t
y = encodeToRec r
in x ++ y
ghc失败了
Test.hs:19:17
Could not deduce (Type r) arising from a use of `encodeToRec'
from the context (Type t, Rec r)
出于某种原因,ghc希望在Type t => Rec t
使用y = encodeToRec r
实例,而不是仅从实例声明的上下文中获取Rec r
。
如果我避免使用let绑定而是写
instance (Type t, Rec r) => Rec (X t r) where
encodeToRec (X t r) = encodeToRec t ++ encodeToRec r
它编译。</ p>
这是ghc中的错误还是因为使用了语言扩展而应该预期这种行为?