0

关于SYB,我是初学者

我尝试编写代码以使用代码获取表达式中的变量

 variables = removeDuplicate $ (everything (++)  ([] `mkQ` f))
          where
             f (ExprVar st) = [st]
             f _  = []

whereremoveDuplicate消除列表中的重复变量

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 _ = []

请让我知道我哪里出错了?

谢谢

4

1 回答 1

2

错误消息准确地说明了问题所在,并提供了两种可行的解决方案:

  • 添加Data a => a -> [String]为类型签名
  • 在 GHC 中启用-XNoMonomorphismRestriction标志(或添加LANGUAGE编译指示)

有关更多信息,请参阅https://www.haskell.org/haskellwiki/Monomorphism_restriction

于 2014-11-07T07:50:48.427 回答