Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Advantages of SYB (scrap your boilerplate) over GHC Generics

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

like image 277
Petr Avatar asked Nov 12 '14 18:11

Petr


1 Answers

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.

like image 132
Thomas M. DuBuisson Avatar answered Oct 27 '22 05:10

Thomas M. DuBuisson