Benefits of SYB (Recycling Your Template) over GHC Generics

Are there any tasks that are possible only with SYB or much easier with it compared to G6C Generics?

+8
haskell scrap-your-boilerplate ghc-generics
source share
1 answer

GHC Generics is a fairly detailed way to execute basically any request or workaround. For example, consider the AST language with the types Stmt and Expr , which output Typeable , Generic and Data :

 data Stmt = ... lots of constrs ... data Expr = Const Int | ... lots of other constrs ... 

How do you use SYB to get all constants starting with Expr or Stmt ? Something like:

 getConst (Const i) = [i] getConst _ = [] getAllConst = everything (++) (mkQ getConst) 

Compare this to the typical use of Generics, which requires two classes, bypassing the sum of the product representations and instantiating the class N times for the N types that need to be passed. Where SYB and, indeed, most common systems will fall exactly in performance .

+3
source share

All Articles