19 Sep 23:19 2012

## ANNOUCE: one-liner-0, SYB-like generics with constraint kinds

Sjoerd Visscher <sjoerd <at> w3future.com>

2012-09-19 21:19:25 GMT

2012-09-19 21:19:25 GMT

Hi all, I am pleased to announce the first release of One-Liner, a package for writing short and concise generic instances of type classes. It works a bit like Scrap-Your-Boilerplate, but it uses the new constraint kinds instead of the Typeable class. On hackage: http://hackage.haskell.org/package/one-liner-0 On github: https://github.com/sjoerdvisscher/one-liner For example, this is how to write generic equality (using the All monoid): eqADT :: (ADT t, Constraints t Eq) => t -> t -> Bool eqADT s t = ctorIndex s == ctorIndex t && getAll (mbuilds (For :: For Eq) (\fld -> All $ s ! fld == t ! fld) `at` s) The code works like this: "Constraints t Eq" means it requires that all subcomponents of type t have an Eq instance, and then values s and t are equal if they are built by the same constructor and each subcomponent is equal. The package is called One-Liner because the generic functions often end up as short as eqADT, especially if there's already an appropriate Monoid or Applicative functor available. Here are two more examples, generic put and get for the Binary package (after adding the missing Monoid instance for Put) putADT :: (ADT t, Constraints t Binary) => t -> Put putADT t = putWord8 (toEnum (ctorIndex t)) >> gfoldMap (For :: For Binary) put getADT :: (ADT t, Constraints t Binary) => Get t getADT = do(Continue reading)