Difference between revisions of "Generics"

From HaskellWiki
Jump to navigation Jump to search
(Placeholder page)
Line 1: Line 1:
  +
''Datatype-generic programming'', also frequently just called ''generic programming'' or ''generics'' in Haskell, is a form of abstraction that allows defining functions that can operate on a large class of datatypes. In this page we summarise a number of popular approaches to generic programming that are often used with GHC. For a more in-depth introduction to generic programming in general, have a look at Gibbon's [http://www.comlab.ox.ac.uk/jeremy.gibbons/publications/dgp.pdf Datatype-Generic Programming], or the [http://dreixel.net/research/pdf/lgph.pdf Libraries for Generic Programming] paper.
This page will describe multiple generic programming approaches that can be used with GHC. For now, there's only documentation for [[GHC.Generics]].
 
  +
  +
== What is generic programming? ==
  +
  +
Haskell is a polymorphic language. This means that you can have a single datatype for lists:
  +
<haskell>
  +
data List a = Nil | Cons a (List a)
  +
</haskell>
  +
These lists can contain any type of information, such as integers, Booleans, or even other lists. Since the length of a list does not depend on the type of its elements, there is also a single definition for list length:
  +
<haskell>
  +
length :: List a -> Int
  +
length Nil = 0
  +
length (Cons _ t) = 1 + length t
  +
</haskell>
  +
  +
However, it's not only lists that have length. Consider a datatype for trees:
  +
<haskell>
  +
data Tree a = Leaf | Bin a (Tree a) (Tree a)
  +
</haskell>
  +
You can also compute the length of a tree (or its size, if you want), by recursively traversing the tree and counting the number of elements. Generic programming allows to define a ''single'' length function, that can operate on lists, trees, and many other datatypes. This reduces code duplication and makes the code more robust to changes, because you can change your datatypes without needing to adapt the generic functions that operate on them.
  +
  +
We now look at some approaches to generic programming in Haskell.
  +
  +
  +
== GHC.Generics ==
  +
  +
See [[GHC.Generics]].
  +
  +
== SYB ==
  +
  +
== Uniplate ==
  +
  +
== Multirec ==

Revision as of 08:31, 21 April 2012

Datatype-generic programming, also frequently just called generic programming or generics in Haskell, is a form of abstraction that allows defining functions that can operate on a large class of datatypes. In this page we summarise a number of popular approaches to generic programming that are often used with GHC. For a more in-depth introduction to generic programming in general, have a look at Gibbon's Datatype-Generic Programming, or the Libraries for Generic Programming paper.

What is generic programming?

Haskell is a polymorphic language. This means that you can have a single datatype for lists:

data List a = Nil | Cons a (List a)

These lists can contain any type of information, such as integers, Booleans, or even other lists. Since the length of a list does not depend on the type of its elements, there is also a single definition for list length:

length :: List a -> Int
length Nil        = 0
length (Cons _ t) = 1 + length t

However, it's not only lists that have length. Consider a datatype for trees:

data Tree a = Leaf | Bin a (Tree a) (Tree a)

You can also compute the length of a tree (or its size, if you want), by recursively traversing the tree and counting the number of elements. Generic programming allows to define a single length function, that can operate on lists, trees, and many other datatypes. This reduces code duplication and makes the code more robust to changes, because you can change your datatypes without needing to adapt the generic functions that operate on them.

We now look at some approaches to generic programming in Haskell.


GHC.Generics

See GHC.Generics.

SYB

Uniplate

Multirec