Scrap Your Boilerplate
(SYB) is a library for generic programming in Haskell.
It is supported since the GHC >= 6.0 implementation of Haskell. Using this approach, you can
write generic functions such as traversal schemes (e.g.,
as well as generic read, generic show and generic equality (i.e.,
). This approach
is based on just a few primitives for type-safe cast and processing constructor applications.
It was originally developed by Ralf Lämmel and Simon Peyton Jones. Since then, many people have contributed with research relating to SYB or its applications.
Datatype-generic programming consists of defining functions on the structure
of datatypes, rather than on a datatype itself. In this way, one can define functions
that work for many different datatypes.
In SYB, the structure of datatypes is not directly exposed to the programmer. Instead,
generic combinators are used to define the generic functions. These combinators
are implemented using fundamental functions from the
Generic functions are usually written using methods from the
which contains the generic combinators provided by SYB for this end.
Not all datatypes can be used immediately with generic functions, but adding
support is very simple. All that is necessary is an instance of the datatype for
classes. These are readily derivable by GHC, using
mechanism. One can simply write:
data MyDataType = MyDataType deriving (Data, Typeable)
For API documentation, refer to the Haddock documentation
You can also have a look at the original webpage
, which contains
many examples. The original SYB papers are also very informative:
SYB comes with GHC
, but it is also available on
. The version on
Hackage is always the most up-to-date.
The source can be cloned from its repository using git
git clone email@example.com:dreixel/syb.git
You can also view the files online
Handling the 6.10
In the discussion towards the release of the 6.10 version of GHC
, it was decided
that SYB would be separated from the compiler itself. This allows for easier maintainability, since updating the library
does not have to depend on updating the compiler. For the context, refer to the discussion in the mailing list (several threads:
This splitting amounts to moving the
modules from the
package into a new package called
class is very tightly coupled with the compiler due to the automatic generation of instances.
Completely moving the entire SYB library from the
package would give a false sense of separation,
since the methods of
cannot be changed independently from the compiler. So, a separation was necessary:
what depended on GHC stayed in
while the rest moved to
on how to split SYB resulted in the following
- Most of SYB was removed from
base4. All the
Data.Generics modules have been moved, but a new module,
Data.Data, has been created. This module contains:
- The entire contents of what was previously in
Data.Generics.Basics. This is the
Data class, all of its methods and associated utility functions and datatypes.
- All of the instances that were previously in
Data.Generics.Instances, except the instances for the following datatypes:
a -> b,
ST s a,
TVar a, and
MVar a. Those were moved to the
- This new package contains the following modules:
Data.Generics.Basics This module is empty, and simply re-exports the new
Data.Data module. It is provided for compatibility.
Data.Generics.Instances This module re-exports the instances from the new
Data.Data module and also define the other instances that were previously in
a -> b,
ST s a,
TVar a, and
MVar a. These instances were moved here because they were considered "dubious". What is meant by this is that it is not clear how these datatypes can be traversed. They might disappear or be changed in future releases of the library.
Data.Generics.Aliases, Data.Generics.Schemes, Data.Generics.Text, Data.Generics.Twins were moved directly from base and kept unchanged.
Data.Generics simply re-exports all the other modules for convenience (just like before).
- This package is provided for compatibility reasons. Here, all SYB modules remain unchanged.
As a result,
is now available as a package on Hackage
Bugs & Support
To report bugs or suggest improvements, use the Google Code issue tracker for SYB
For general concerns and questions, use the Generics mailing list