我在与寻找合适的类型约束以下代码问题而引起的没有实例(通用(FA))从使用'从”
{-# LANGUAGE DeriveGeneriC#-}
{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE FlexibleContexts #-}
import GHC.Generics
data Value = One | Two deriving Generic
class Class a where
cname :: a -> String -> Bool
default cname :: (Generic a, GClass (Rep a))
=> a -> String -> Bool
cname = gname . from
class GClass f where
gname :: f a -> String -> Bool
instance GClass (f :+: g) where
gname (L1 x) s | conName (from x) == s = True
| otherwise = False
gname (R1 x) s | conName (from x) == s = True
| otherwise = False
它失败
No instance for (Generic (f a)) arising from a use of `from'
添加约束gname
这样
instance (Generic (f a)) => GClass (f :+: g) where
失败
Could not deduce (Generic (f a1)) arising from a use of `from'
from the context (Generic (f a))
编辑:为完整的片段
Generic.hs:19:31:
No instance for (Generic (f a)) arising from a use of `from'
Possible fix: add an instance declaration for (Generic (f a))
In the first argument of `conName', namely `(from x)'
In the first argument of `(==)', namely `conName (from x)'
In the expression: conName (from x) == s
Generic.hs:21:31:
No instance for (Generic (g a)) arising from a use of `from'
Possible fix: add an instance declaration for (Generic (g a))
In the first argument of `conName', namely `(from x)'
In the first argument of `(==)', namely `conName (from x)'
In the expression: conName (from x) == s
这个完整的错误消息是GHC 7.6.3
你确定这就是你与发布代码得到的错误这三个实例?你使用的是什么版本的ghc? 'Class'和'GClass'都可以用ghc 7.8.3编译,但':+:'的'GClass'实例不能编译时会出现不同的错误信息。包含更多的错误信息也很有用;在代码中有三个'from'出现,而来自ghc的完整错误信息会告诉他们哪些没有所需的实例。 – Cirdec 2015-01-31 19:07:02