关于SYB,我是初学者
我试过拧一个代码来获取带有代码
的表达式中的变量 variables = removeDuplicate $ (everything (++) ([] `mkQ` f))
where
f (ExprVar st) = [st]
f _ = []
其中removeDuplicate
删除列表中的重复变量
ExprVar
是我的数据类型
数据类型还包括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 _ = []
请告诉我哪里出错?
谢谢
答案 0 :(得分:2)
错误消息确切地说明了错误,并提供了两种可行的解决方案:
Data a => a -> [String]
添加为类型签名-XNoMonomorphismRestriction
标记(或添加LANGUAGE
编译指示)有关详细信息,请参阅https://www.haskell.org/haskellwiki/Monomorphism_restriction