废弃样板

时间:2014-11-07 07:13:51

标签: haskell scrap-your-boilerplate

关于SYB,我是初学者

我试过拧一个代码来获取带有代码

的表达式中的变量
 variables = removeDuplicate $ (everything (++)  ([] `mkQ` f))
          where
             f (ExprVar st) = [st]
             f _  = []

其中removeDuplicate删除列表中的重复变量

ExprVar是我的数据类型 数据类型还包括ExprAddExprsubExprMulExprDivExprNum用于添加,减法乘法,除法和数字。

我在编译时遇到以下错误:

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

请告诉我哪里出错?

谢谢

1 个答案:

答案 0 :(得分:2)

错误消息确切地说明了错误,并提供了两种可行的解决方案:

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

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