Advantages of SYB (scrap your boilerplate) over GHC Generics

563 views Asked by At

Are any tasks that are possible only with SYB, or are much easier with it, when compared to GHC Generics?

1

There are 1 answers

0
Thomas M. DuBuisson On

GHC Generics is a rather verbose way to perform basically any query or traversal. For example, consider a language AST with Stmt and Expr types that both derive Typeable, Generic, and Data:

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

How do you leverage SYB to get all constants starting from either Expr or Stmt? Something like:

getConst (Const i) = [i]
getConst _         = []

getAllConst = everything (++) (mkQ getConst)

Contrast this with the typical use of Generics requiring two classes, a traversal over the sum of products representation, and instantiate the class N times for the N types needing traversed. Where SYB, and indeed most generic systems, fall flat is in performance.