anujuhi01
anujuhi01

Reputation: 71

Scrap your boilerplate

Regarding SYB,I am a beginner at it

I tried wring a code to get the variables in an expression with the code

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

where removeDuplicate elimates the duplicate variables in the list

ExprVar is my Datatype Datatype also includes ExprAdd , Exprsub ,ExprMul , ExprDiv ,ExprNum for addtion,subtraction Multiplication ,division and for a number respectively.

I am getting the following error on compiling :

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

Please let me know where am I going wrong?

Thank you

Upvotes: 0

Views: 134

Answers (1)

kosmikus
kosmikus

Reputation: 19657

The error message says exactly what's wrong and provides two possible solutions that both work:

  • add Data a => a -> [String] as type signature
  • enable the -XNoMonomorphismRestriction flag in GHC (or add a LANGUAGE pragma)

For more information, see https://www.haskell.org/haskellwiki/Monomorphism_restriction

Upvotes: 2

Related Questions