## Generic programming, now (2006)

Venue: | Generic Programming, Advanced Lectures, LNCS |

Citations: | 3 - 1 self |

### BibTeX

@INPROCEEDINGS{Hinze06genericprogramming,,

author = {Ralf Hinze and Andres Löh},

title = {Generic programming, now},

booktitle = {Generic Programming, Advanced Lectures, LNCS},

year = {2006},

pages = {150--208},

publisher = {Springer-Verlag}

}

### OpenURL

### Abstract

Abstract. Tired of writing boilerplate code? Tired of repeating essentially the same function definition for lots of different datatypes? Datatype-generic programming promises to end these coding nightmares. In these lecture notes, we present the key abstractions of datatype-generic programming, give several applications, and provide an elegant embedding of generic programming into Haskell. The embedding builds on recent advances in type theory: generalised algebraic datatypes and open datatypes. We hope to convince you that generic programming is useful and that you can use generic programming techniques today! 1

### Citations

329 | Theorems for free
- Wadler
- 1989
(Show Context)
Citation Context ...to text. As a first try, we could assign the pretty-printer the type Type → α → Text where Type is the type of type representations. Unfortunately, this is too simpleminded: the parametricity theorem =-=[42]-=- implies that a function of this type must necessarily ignore its second parameter. This argument breaks down, however, if we additionally parameterise Type by the type it represents. The signature of... |

259 | Compiling Polymorphism Using Intensional Type Analysis
- Harper, Morrisett
- 1995
(Show Context)
Citation Context ...oices. Overloaded functions can be expressed using – type reflection: This is the approach we have used in these lecture notes. Its origins can be traced back to the work on intensional type analysis =-=[11, 8, 7, 40, 43]-=- (ITA). ITA is intensively used in typed intermediate languages,s54 R. Hinze and A. Löh view(s) representation of overloaded functions none ITA [11, 8, 7, 40, 43] type reflection type classes type-saf... |

178 | A system of constructor classes: overloading and implicit higher-order polymorphism
- Jones
- 1993
(Show Context)
Citation Context ...e ′ Id)) 55 With the help of the conversion functions we can implement each of the four different views on a list of trees of integers. Since Haskell employs a kinded first-order unification of types =-=[27]-=-, the calls almost always additionally involve a change on the value level. The type equation ϕ τ = List (Tree Int) is solved by setting ϕ = List and τ = Tree Int, that is, Haskell picks one of the fo... |

174 | PolyP – a polytypic programming language extension
- Jansson, Jeuring
- 1997
(Show Context)
Citation Context ...systems the key to genericity is a uniform view on data. In Section 3.3 we introduced the spine view, which views data as constructor applications. Of course, this is not the only generic view. PolyP =-=[26]-=-, for instance, views data types as fixed points of regular functors; Generic Haskell [19] uses a sum-of-products view. We shall see that these two approaches can be characterised as type-oriented: th... |

153 | Type Classes with Functional Dependencies
- Jones
- 2001
(Show Context)
Citation Context ... is set up as a preprocessor that translates PolyP code into Haskell. A later version [35] embeds PolyP programs into Haskell augmented by multiple parameter type classes with functional dependencies =-=[28]-=-. Oliveira and Gibbons [36] present a lightweight variant of PolyP that works within Haskell 98. – sum-of-products view: Generic Haskell [19, 33, 34] (GH) builds upon this view. In its original form i... |

142 | Intensional polymorphism in type erasure semantics
- Crary, Weirich, et al.
- 2002
(Show Context)
Citation Context ...oices. Overloaded functions can be expressed using – type reflection: This is the approach we have used in these lecture notes. Its origins can be traced back to the work on intensional type analysis =-=[11, 8, 7, 40, 43]-=- (ITA). ITA is intensively used in typed intermediate languages,s54 R. Hinze and A. Löh view(s) representation of overloaded functions none ITA [11, 8, 7, 40, 43] type reflection type classes type-saf... |

126 | Domain specific embedded compilers
- Leijen, Meijer
(Show Context)
Citation Context ...The type Expr β might not even be inhabited: there are, for instance, no terms of type Expr String. Since the type argument of Expr is not related to any component, Expr is also called a phantom type =-=[31, 16]-=-. It is instructive to see where the attempt to generalise size or map to GADTs fails technically. We can, in fact, define a lifted version of the Expr type (we confine ourselves to one constructor). ... |

122 | Type classes in Haskell
- Hall, Hammond, et al.
- 1996
(Show Context)
Citation Context ...ary case expression. Cheney and Hinze [5] present a library for generics and dynamics (LIGD) that uses an encoding of type representations in Haskell 98 augmented by existential types. – type classes =-=[10]-=-: Type classes are Haskell’s major innovation for supporting ad-hoc polymorphism. A type class declaration corresponds to the type signature of an overloaded value — or rather, to a collection of type... |

121 | Derivable type classes
- Hinze, Jones
(Show Context)
Citation Context ...verloaded functions none ITA [11, 8, 7, 40, 43] type reflection type classes type-safe cast specialisation – – – fixed point Reloaded [22] PolyP [35, 36] – PolyP [26] sum-of-products LIGD [5, 16] DTC =-=[24]-=-, GC [1], GM [17] spine Reloaded [22], Revolutions [21] SYB [30], Reloaded [23] – GH [15, 19, 33, 34] SYB [38, 29] – Fig. 1. Generic programming: the design space. in particular, for optimising purely... |

107 | Polytypic values possess polykinded types
- Hinze
- 2000
(Show Context)
Citation Context ...licable to all datatypes definable in Haskell 98. We have seen in Section 5.4 that it can be generalised to GADTs. Generic Haskell is a full-fledged implementation of generics based on ideas by Hinze =-=[15, 20]-=- that features generic functions, generic types and various extensions such as default cases and constructor cases [6]. Generic Haskell supports the definition of functions that work for all types of ... |

98 | Higher-order functions for parsing
- Hutton
- 1992
(Show Context)
Citation Context ...l’s readsPrec function of type Int →ReadS α. The Int argument specifies the operator precedence of the enclosing context; ReadS abbreviates String → [Pair α String ], the type of backtracking parsers =-=[25]-=-. open readsPrec :: Type α → Int → ReadS α readsPrec (Char) d = readsPrecChar d readsPrec (Int) d = readsPrecInt d readsPrec (String) d = readsPrecString d readsPrec (List a) d = readsList (reads a) r... |

94 | A new approach to generic functional programming
- Hinze
- 2000
(Show Context)
Citation Context ... (Tree a), and λa → List (Tree Int). Alas, we are just trying to get rid of the functional representation. There are several ways out of this dilemma. One possibility is to lift the type constructors =-=[14]-=- so that they become members of Type ′ and to include Id, defined as newtype Id α = InId{out Id :: α} as a representation of the type variable α: Id :: Type ′ Id Char ′ :: Type ′ Char ′ Int ′ :: Type ... |

84 | Generic programming – an introduction
- Backhouse, Jansson, et al.
- 1999
(Show Context)
Citation Context ...{outl ′ :: ϕ α, outr ′ :: ψ α} The reader may wish to fill in the details. 6 Related work There is a wealth of material on the subject of generic programming. The tutorials of previous summer schools =-=[2, 19, 18]-=- provide an excellent overview of the field. We have seen that support for generic programming consists of three essential ingredients: – a type reflection mechanism, – a type representation, and – a ... |

79 | Nested datatypes
- BIRD, MEERTENS
- 1998
(Show Context)
Citation Context ...on of Type, consider the type of perfect binary trees [12]. data Perfect α = Zero α | Succ (Perfect (α, α))sGeneric Programming, Now! 13 As an aside, note that Perfect is a so-called nested data type =-=[3]-=-. To be able to pretty-print perfect trees, we add a constructor to the type Type of type representations and extend pretty by suitable equations. Perfect :: Type α → Type (Perfect α) pretty (Zero x :... |

76 | Flexible Type Analysis
- Crary, Weirich
- 1999
(Show Context)
Citation Context ...oices. Overloaded functions can be expressed using – type reflection: This is the approach we have used in these lecture notes. Its origins can be traced back to the work on intensional type analysis =-=[11, 8, 7, 40, 43]-=- (ITA). ITA is intensively used in typed intermediate languages,s54 R. Hinze and A. Löh view(s) representation of overloaded functions none ITA [11, 8, 7, 40, 43] type reflection type classes type-saf... |

74 | A lightweight implementation of generics and dynamics
- Cheney, Hinze
- 2002
(Show Context)
Citation Context ...ntation of overloaded functions none ITA [11, 8, 7, 40, 43] type reflection type classes type-safe cast specialisation – – – fixed point Reloaded [22] PolyP [35, 36] – PolyP [26] sum-of-products LIGD =-=[5, 16]-=- DTC [24], GC [1], GM [17] spine Reloaded [22], Revolutions [21] SYB [30], Reloaded [23] – GH [15, 19, 33, 34] SYB [38, 29] – Fig. 1. Generic programming: the design space. in particular, for optimisi... |

68 | Exploring Generic Haskell
- Löh
- 2004
(Show Context)
Citation Context ...ructors ‘+’ and ‘×’ are nested accordingly. With respect to the nesting there are several choices: we can use a right-deep or a left-deep nesting, a list-like nesting or a (balanced) treelike nesting =-=[33]-=-. For the following examples, we choose – more or less arbitrarily – a tree-like encoding. We first add suitable constructors to the type of type representations. infixr 7 × infixr 6 + 0 :: Type Zero ... |

65 | Generic Haskell: Practice and theory
- Hinze, Jeuring
- 2003
(Show Context)
Citation Context ...spine view, which views data as constructor applications. Of course, this is not the only generic view. PolyP [26], for instance, views data types as fixed points of regular functors; Generic Haskell =-=[19]-=- uses a sum-of-products view. We shall see that these two approaches can be characterised as type-oriented: they provide a uniform view on all elements of a datatype. By contrast, the spine view is va... |

62 |
Scrap your boilerplate with class: extensible generic functions. Pages 204–215 of: ICFP’05
- Lämmel, Jones, et al.
- 2005
(Show Context)
Citation Context ...type classes type-safe cast specialisation – – – fixed point Reloaded [22] PolyP [35, 36] – PolyP [26] sum-of-products LIGD [5, 16] DTC [24], GC [1], GM [17] spine Reloaded [22], Revolutions [21] SYB =-=[30]-=-, Reloaded [23] – GH [15, 19, 33, 34] SYB [38, 29] – Fig. 1. Generic programming: the design space. in particular, for optimising purely polymorphic functions. Type reflection avoids the duplication o... |

59 | Scrap more boilerplate: reflection, zips, and generalised casts
- Lämmel, Jones
- 2004
(Show Context)
Citation Context ... – fixed point Reloaded [22] PolyP [35, 36] – PolyP [26] sum-of-products LIGD [5, 16] DTC [24], GC [1], GM [17] spine Reloaded [22], Revolutions [21] SYB [30], Reloaded [23] – GH [15, 19, 33, 34] SYB =-=[38, 29]-=- – Fig. 1. Generic programming: the design space. in particular, for optimising purely polymorphic functions. Type reflection avoids the duplication of features: a type case, for instance, boils down ... |

58 | Type-indexed data types
- HINZE, JEURING, et al.
- 2004
(Show Context)
Citation Context ...licable to all datatypes definable in Haskell 98. We have seen in Section 5.4 that it can be generalised to GADTs. Generic Haskell is a full-fledged implementation of generics based on ideas by Hinze =-=[15, 20]-=- that features generic functions, generic types and various extensions such as default cases and constructor cases [6]. Generic Haskell supports the definition of functions that work for all types of ... |

54 | Revisiting catamorphism over datatypes with embedded functions
- Fegaras, Sheard
- 1996
(Show Context)
Citation Context ...ce the ‘type variable’ carries its own meaning, the count instance is particularly simple. count (Count c) = c A moment’s reflection reveals that this approach is an instance of the ‘embedding trick’ =-=[9]-=- for higher-order abstract syntax: Count is the pre-inverse or right inverse of count. Using Count we can specify the action on the free type variable when we call count: Now〉 let ts = [tree [0 . . i ... |

54 |
98 Language and Libraries
- Haskell
- 2003
(Show Context)
Citation Context ... The lack of flexibility is particularly vexing when it comes to implementing fundamental operations such as showing a value or comparing two values. In a statically typed language such as Haskell 98 =-=[37]-=- it is simply not possible, for instance, to define an equality test that works for all types. As a rule of thumb, the more expressive a type system, the more fine-grained the type information and the... |

53 |
Scrap your boilerplate: a practical approach to generic programming
- Lämmel, Jones
- 2003
(Show Context)
Citation Context ... – fixed point Reloaded [22] PolyP [35, 36] – PolyP [26] sum-of-products LIGD [5, 16] DTC [24], GC [1], GM [17] spine Reloaded [22], Revolutions [21] SYB [30], Reloaded [23] – GH [15, 19, 33, 34] SYB =-=[38, 29]-=- – Fig. 1. Generic programming: the design space. in particular, for optimising purely polymorphic functions. Type reflection avoids the duplication of features: a type case, for instance, boils down ... |

51 |
Generics for the masses
- Hinze
- 2004
(Show Context)
Citation Context ...ns none ITA [11, 8, 7, 40, 43] type reflection type classes type-safe cast specialisation – – – fixed point Reloaded [22] PolyP [35, 36] – PolyP [26] sum-of-products LIGD [5, 16] DTC [24], GC [1], GM =-=[17]-=- spine Reloaded [22], Revolutions [21] SYB [30], Reloaded [23] – GH [15, 19, 33, 34] SYB [38, 29] – Fig. 1. Generic programming: the design space. in particular, for optimising purely polymorphic func... |

44 | Fully reflexive intensional type analysis
- Trifonov, Saha, et al.
- 2000
(Show Context)
Citation Context |

41 | scrap your boilerplate” reloaded
- Hinze, Löh, et al.
- 2006
(Show Context)
Citation Context ...iate languages,s54 R. Hinze and A. Löh view(s) representation of overloaded functions none ITA [11, 8, 7, 40, 43] type reflection type classes type-safe cast specialisation – – – fixed point Reloaded =-=[22]-=- PolyP [35, 36] – PolyP [26] sum-of-products LIGD [5, 16] DTC [24], GC [1], GM [17] spine Reloaded [22], Revolutions [21] SYB [30], Reloaded [23] – GH [15, 19, 33, 34] SYB [38, 29] – Fig. 1. Generic p... |

35 |
Generic Haskell, Specifically
- Clarke, Löh
- 2002
(Show Context)
Citation Context ...Haskell is a full-fledged implementation of generics based on ideas by Hinze [15, 20] that features generic functions, generic types and various extensions such as default cases and constructor cases =-=[6]-=-. Generic Haskell supports the definition of functions that work for all types of all kinds, such as, for example, a generalised mapping function. – spine views: The spine view treats data uniformly a... |

35 | Higher-order intensional type analysis
- Weirich
- 2002
(Show Context)
Citation Context |

33 |
Fun with phantom types
- Hinze
- 2003
(Show Context)
Citation Context ...The type Expr β might not even be inhabited: there are, for instance, no terms of type Expr String. Since the type argument of Expr is not related to any component, Expr is also called a phantom type =-=[31, 16]-=-. It is instructive to see where the attempt to generalise size or map to GADTs fails technically. We can, in fact, define a lifted version of the Expr type (we confine ourselves to one constructor). ... |

30 | Generic haskell: Applications
- Hinze, Jeuring
- 2003
(Show Context)
Citation Context ...{outl ′ :: ϕ α, outr ′ :: ψ α} The reader may wish to fill in the details. 6 Related work There is a wealth of material on the subject of generic programming. The tutorials of previous summer schools =-=[2, 19, 18]-=- provide an excellent overview of the field. We have seen that support for generic programming consists of three essential ingredients: – a type reflection mechanism, – a type representation, and – a ... |

26 | TypeCase: A design pattern for type-indexed functions
- Oliveira, Gibbons
- 2005
(Show Context)
Citation Context ...ges,s54 R. Hinze and A. Löh view(s) representation of overloaded functions none ITA [11, 8, 7, 40, 43] type reflection type classes type-safe cast specialisation – – – fixed point Reloaded [22] PolyP =-=[35, 36]-=- – PolyP [26] sum-of-products LIGD [5, 16] DTC [24], GC [1], GM [17] spine Reloaded [22], Revolutions [21] SYB [30], Reloaded [23] – GH [15, 19, 33, 34] SYB [38, 29] – Fig. 1. Generic programming: the... |

22 |
Open data types and open functions
- Löh, Hinze
- 2006
(Show Context)
Citation Context ... via a preprocessor, the defining equations have to be reordered in such a way that the more specific equations come first. The details of open datatypes and functions are described in a recent paper =-=[32]-=-. 3 A guided tour 3.1 Type-indexed functions In Haskell, showing values of a datatype is particularly easy: one simply attaches a deriving (Show) clause to the declaration of the datatype. data Tree α... |

21 | Type-safe cast
- Weirich
(Show Context)
Citation Context ...t kinds. A pure Haskell 98 implementation of generics (GM) is described by Hinze [17]. The implementation builds upon a class-based encoding of the type Type of type representations. – type-safe cast =-=[44]-=-: A cast operation converts a value from one type to another, provided the two types are identical at run-time. A cast can be seen as a type-case with exactly one branch. The original SYB paper [38] i... |

18 | Scrap Your Boilerplate” revolutions
- Hinze, Löh
- 2006
(Show Context)
Citation Context ...flection type classes type-safe cast specialisation – – – fixed point Reloaded [22] PolyP [35, 36] – PolyP [26] sum-of-products LIGD [5, 16] DTC [24], GC [1], GM [17] spine Reloaded [22], Revolutions =-=[21]-=- SYB [30], Reloaded [23] – GH [15, 19, 33, 34] SYB [38, 29] – Fig. 1. Generic programming: the design space. in particular, for optimising purely polymorphic functions. Type reflection avoids the dupl... |

16 | Polytypic programming in haskell
- Norell, Jansson
- 2005
(Show Context)
Citation Context ...ges,s54 R. Hinze and A. Löh view(s) representation of overloaded functions none ITA [11, 8, 7, 40, 43] type reflection type classes type-safe cast specialisation – – – fixed point Reloaded [22] PolyP =-=[35, 36]-=- – PolyP [26] sum-of-products LIGD [5, 16] DTC [24], GC [1], GM [17] spine Reloaded [22], Revolutions [21] SYB [30], Reloaded [23] – GH [15, 19, 33, 34] SYB [38, 29] – Fig. 1. Generic programming: the... |

13 | Memo functions, polytypically
- Hinze
- 2000
(Show Context)
Citation Context ... most once. This is guaranteed if the implementation is fully lazy. Usually, memoisation is defined as the composition of a function that constructs a memo table and a function that queries the table =-=[13]-=-. If we fuse the two functions, thereby eliminating the memo data structure, we obtain the memo function above. Despite appearances, the memo data structures did not vanish into thin air. Rather, they... |

11 |
A logarithmic implementation of flexible arrays. Memorandum MR83/4
- Braun, Rem
- 1983
(Show Context)
Citation Context ...→ [α] inorder Empty = Nil inorder (Node l a r) = inorder l + [a ] + inorder r The function tree, also used in Section 3.1, turns a list of elements into a balanced binary tree, a so-called Braun tree =-=[4]-=-. tree :: ∀α . [α] → Tree α tree x | null x = Empty | otherwise = Node (tree x1) a (tree x2) where (x1, Cons a x2) = splitAt (length x ‘div‘ 2) x The function perfect d a, used in Section 3.2, generat... |

10 |
Functional Pearl: Perfect trees and bit-reversal permutations
- Hinze
(Show Context)
Citation Context ...dd new constructors to the Type datatype and so that we can freely add new equations to existing open functions on Type. To illustrate the extension of Type, consider the type of perfect binary trees =-=[12]-=-. data Perfect α = Zero α | Succ (Perfect (α, α))sGeneric Programming, Now! 13 As an aside, note that Perfect is a so-called nested data type [3]. To be able to pretty-print perfect trees, we add a co... |

8 | The Generic Haskell user’s guide, version 1.42 (Coral
- Löh, Jeuring, et al.
- 2005
(Show Context)
Citation Context ...st specialisation – – – fixed point Reloaded [22] PolyP [35, 36] – PolyP [26] sum-of-products LIGD [5, 16] DTC [24], GC [1], GM [17] spine Reloaded [22], Revolutions [21] SYB [30], Reloaded [23] – GH =-=[15, 19, 33, 34]-=- SYB [38, 29] – Fig. 1. Generic programming: the design space. in particular, for optimising purely polymorphic functions. Type reflection avoids the duplication of features: a type case, for instance... |

2 |
The expression problem. Note to Java Genericity mailing list
- Wadler
- 1998
(Show Context)
Citation Context ...tors for open datatypes and all the defining equations for open functions. Using open datatypes and open functions gives us both directions of extensibility mentioned in the famous expression problem =-=[41]-=-: we can add additional sorts of data, by providing new constructors, and we can add additional operations, by defining new functions. Here is another function on expressions, which turns a given expr... |