[Haskell-cafe] Poor man's generic programming

Neil Mitchell ndmitchell at gmail.com
Mon Jan 18 14:56:00 EST 2010


Hi Henning,

Uniplate might be the answer you are looking for -
http://community.haskell.org/~ndm/uniplate

Uniplate is simple (only multi parameter type classes, and even then
only in a very simple usage), fast (one of the fastest generics
libraries) and concise (probably the most concise generics library).
It's also not as powerful as most of the other generics libraries, but
I find it does about 98% of the generics tasks I need. Uniplate is
used extensively in virtually all my tools, for example HLint.

As an example, I guess your function returns all the Int's embedded
within a data type, at any level? If so, uniplate already has the
universeBi method which does that for you, and doesn't require you to
do anything.

Thanks, Neil

2010/1/16 Henning Thielemann <schlepptop at henning-thielemann.de>:
> Is any of the existing Generics packages able to work without compiler extensions, that is Haskell 98?
> I mean, it is ok if the developer of the generic parts of a library needs compiler extensions or extra tools, but the user who calls 'cabal install' shall not need them and the developer, who does not touch generic definitions, should not need them as well.
>
> I think this could be done with pragmas in the following way:
>
> class C a where
>  f :: a -> [Int]
>  {-# GENERICDEFAULT
>  f {| Unit |} Unit = []
>  #-}
>
> {-# GENERATEDINSTANCEBEGIN Automatically generated - Do not edit! #-}
> instance C () where
>  f () = []
> {-# GENERATEDINSTANCEEND Automatically generated - Do not edit! #-}
>
> Now, by running a tool, the part between the GENERATEDINSTANCE pragmas can generated or updated.
> Everything outside the pragmas can be Haskell 98.
>
> It does not only avoid extensions of the building compiler, but also extensions of a programmer's brain. Because for programmers it is often easier to understand a set of instances and see the common patterns, than to understand generic code that builds the instances.
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>


More information about the Haskell-Cafe mailing list