关于SYB,我还是个初学者。
我试着编写一段代码来获得表达式中的变量
variables = removeDuplicate $ (everything (++) ([] `mkQ` f))
where
f (ExprVar st) = [st]
f _ = []其中removeDuplicate消除了列表中的重复变量
ExprVar是我的数据类型Datatype还包括用于加法、减法乘法、除法和数字的ExprAdd、Exprsub、ExprMul、ExprDiv、ExprNum。
我在编译时遇到以下错误:
No instance for (Data a0) arising from a use of `everything'
The type variable `a0' is ambiguous
Possible cause: the monomorphism restriction applied to the following:
variables :: a0 -> [[Char]] (bound at ParserExpr.hs:107:1)
Probable fix: give these definition(s) an explicit type signature
or use -XNoMonomorphismRestriction
Note: there are several potential instances:
instance Data Expr -- Defined at ParserExpr.hs:24:28
instance (Data a, Data b) => Data (a -> b)
-- Defined in `Data.Generics.Instances'
instance Data DataType -- Defined in `Data.Generics.Instances'
...plus 43 others
In the expression: (everything (++) ([] `mkQ` f))
In an equation for `variables':
variables
= (everything (++) ([] `mkQ` f))
where
f (ExprVar st) = [st]
f _ = []请让我知道我哪里错了?
谢谢
发布于 2014-11-07 15:50:48
错误消息准确地指出了问题所在,并提供了两种可能的解决方案,这两种方案都有效:
在GHC中添加Data a => a -> [String] as type signature
-XNoMonomorphismRestriction标志(或添加LANGUAGE杂注)有关更多信息,请参阅https://www.haskell.org/haskellwiki/Monomorphism_restriction
https://stackoverflow.com/questions/26795997
复制相似问题