## A Lightweight Implementation of Generics and Dynamics (2002)

Citations: | 74 - 6 self |

### BibTeX

@MISC{Cheney02alightweight,

author = {James Cheney and Ralf Hinze},

title = {A Lightweight Implementation of Generics and Dynamics},

year = {2002}

}

### Years of Citing Articles

### OpenURL

### Abstract

The recent years have seen a number of proposals for extending statically typed languages by dynamics or generics. Most proposals --- if not all --- require significant extensions to the underlying language. In this paper we show that this need not be the case. We propose a particularly lightweight extension that supports both dynamics and generics. Furthermore, the two features are smoothly integrated: dynamic values, for instance, can be passed to generic functions. Our proposal makes do with a standard Hindley-Milner type system augmented by existential types. Building upon these ideas we have implemented a small library that is readily usable both with Hugs and with the Glasgow Haskell compiler.

### Citations

704 |
Types and Programming Languages
- Pierce
- 2002
(Show Context)
Citation Context ...gramming, dynamic typing, type representations 1 Introduction A desirable feature of programming languages is safety. Broadly speaking, safe programming languages prevent untrapped errors at run time =-=[25-=-]. Safety can be achieved either by static checking, by dynamic checking, or by a combination of static and dynamic checks. Each approach has its pros and cons. This work was supported in part by the... |

329 | Theorems for free
- Wadler
- 1989
(Show Context)
Citation Context ...! Bool, 1 We will always write universal quantifiers explicitly. where Rep is the type of type representations. A moment's reflection, however, reveals that this won't work. The parametricity theorem =-=[29]-=- implies that a function of this type must necessarily ignore its second and its third argument. The trick is to use a parametric type for type representations: ( ) :: 8a : Rep a ! a ! a ! Bool: Here ... |

267 | A type-theoretic approach to higher-order modules with sharing
- Harper, Lillibridge
- 1994
(Show Context)
Citation Context ...a and b with t = a+b. Existential types are supported both by Hugs and by the Glasgow Haskell compiler. REMARK 2. Equational type constraints have been studied in the context of module systems for ML =-=[20, 13]-=- and typed closure conversion in the presence of intensional polymorphism [22]. In those contexts, the problem is that important relationships between types are hidden by modular abstraction or existe... |

259 | Compiling Polymorphism Using Intensional Type Analysis
- Harper, Morrisett
- 1995
(Show Context)
Citation Context ...tions; ad-hoc polymorphism [31, 11] (i.e. Haskell's type classes), in which types are associated with classes that indicate the presence of overloaded functions like ` '; intensional polymorphism [1=-=4]-=-, in which type information is preserved throughout compilation, so that run-time type dispatch can be performed. However, none of the above techniques is both easy to implement and powerful enough to... |

237 | Multi-Stage Programming with Explicit Annotations
- Taha, Sheard
- 1997
(Show Context)
Citation Context ...ence Clean's TC is analogous to our Representable class. Shields, Sheard, and Peyton Jones [27] present an alternative implementation of dynamics based on staged type inference. In staged computation =-=[10, 28]-=-, compilation of parts of a program may be delayed, so functions may be specialized to arguments available at compile time. Staged type inference delays type inference and type checking until run time... |

223 | Manifest types, modules, and separate compilation
- Leroy
- 1994
(Show Context)
Citation Context ...a and b with t = a+b. Existential types are supported both by Hugs and by the Glasgow Haskell compiler. REMARK 2. Equational type constraints have been studied in the context of module systems for ML =-=[20, 13]-=- and typed closure conversion in the presence of intensional polymorphism [22]. In those contexts, the problem is that important relationships between types are hidden by modular abstraction or existe... |

185 | A modal analysis of staged computation
- Davies, Pfenning
(Show Context)
Citation Context ...ence Clean's TC is analogous to our Representable class. Shields, Sheard, and Peyton Jones [27] present an alternative implementation of dynamics based on staged type inference. In staged computation =-=[10, 28]-=-, compilation of parts of a program may be delayed, so functions may be specialized to arguments available at compile time. Staged type inference delays type inference and type checking until run time... |

174 | PolyP – a polytypic programming language extension
- Jansson, Jeuring
- 1997
(Show Context)
Citation Context ...ic casts within statically typed languages include: explicit dynamic typing [21, 1], in which the language is augmented with a Dynamic type and a typecase or cast construct; polytypic programming [1=-=9, 5, 16-=-], in which type-dependent functions written in a language extension are translated to pure polymorphic functions; ad-hoc polymorphism [31, 11] (i.e. Haskell's type classes), in which types are assoc... |

154 | Dynamic typing in a statically typed language
- Abadi, Cardelli, et al.
- 1991
(Show Context)
Citation Context ...lues within classes but are limited to kind ?. Clean's generics system [3] generalizes derivable type classes to allow generic type classes defined at arbitrary kinds rather than just ?. Abadi et al. =-=[2]-=- first considered rigorously the problem of adding a Dynamic type and type pattern matching typecase to a monomorphic ML-like language. Leroy and Mauny [21] studied the interaction of Dynamic with imp... |

154 | Typed closure conversion
- Minamide, Morrisett, et al.
- 1996
(Show Context)
Citation Context ...gow Haskell compiler. REMARK 2. Equational type constraints have been studied in the context of module systems for ML [20, 13] and typed closure conversion in the presence of intensional polymorphism =-=[22]-=-. In those contexts, the problem is that important relationships between types are hidden by modular abstraction or existential quantification; it is solved using translucent sum types, singleton kind... |

142 | Intensional polymorphism in type erasure semantics
- Crary, Weirich, et al.
- 2002
(Show Context)
Citation Context ... how to safely combine dynamics and polymorphism. Hinze and Peyton Jones [18] have introduced derivable type classes, which permit polytypic definitions of type classes. Crary, Weirich, and Morrisett =-=[9]-=- have reconciled intensional polymorphism with type erasure using explicit type representations. However, implementations of dynamics, derivable type classes, and type representations still seem to re... |

122 | Type classes in Haskell - Hall, Hammond, et al. - 1996 |

121 | Derivable type classes
- Hinze, Jones
(Show Context)
Citation Context ...studied in isolation. 90 Recent work has addressed some of these shortcomings: Leroy and Mauny [21] and Abadi et al. [1] showed how to safely combine dynamics and polymorphism. Hinze and Peyton Jones =-=[18]-=- have introduced derivable type classes, which permit polytypic definitions of type classes. Crary, Weirich, and Morrisett [9] have reconciled intensional polymorphism with type erasure using explicit... |

107 | Polytypic values possess polykinded types
- Hinze
- 2000
(Show Context)
Citation Context ...askell that includes a polytypic function construct permitting definitions by primitive recursion on the structure of regular data types, but did not support higher-order kinded type arguments. Hinze =-=[17]-=- proposed an approach based on indexing values by types and types by kinds. This made it possible to write definitions of functions like map that work for arbitrary polymorphic data structures. This a... |

97 | Dynamic Typing in Polymorphic Languages
- Abadi, Cardelli, et al.
- 1992
(Show Context)
Citation Context ...e HindleyMilner type system. Previous approaches to supporting generic programming, typedependent optimizations, and dynamic casts within statically typed languages include: explicit dynamic typing [=-=21, 1-=-], in which the language is augmented with a Dynamic type and a typecase or cast construct; polytypic programming [19, 5, 16], in which type-dependent functions written in a language extension are tr... |

94 | A new approach to generic functional programming
- Hinze
- 2000
(Show Context)
Citation Context ...ic casts within statically typed languages include: explicit dynamic typing [21, 1], in which the language is augmented with a Dynamic type and a typecase or cast construct; polytypic programming [1=-=9, 5, 16-=-], in which type-dependent functions written in a language extension are translated to pure polymorphic functions; ad-hoc polymorphism [31, 11] (i.e. Haskell's type classes), in which types are assoc... |

84 | Generic programming – an introduction
- Backhouse, Jansson, et al.
- 1999
(Show Context)
Citation Context ...ic casts within statically typed languages include: explicit dynamic typing [21, 1], in which the language is augmented with a Dynamic type and a typecase or cast construct; polytypic programming [1=-=9, 5, 16-=-], in which type-dependent functions written in a language extension are translated to pure polymorphic functions; ad-hoc polymorphism [31, 11] (i.e. Haskell's type classes), in which types are assoc... |

79 | Nested datatypes
- BIRD, MEERTENS
- 1998
(Show Context)
Citation Context ...ather awkward and never completely general as type recursion may span over several types (mutual recursion) and as it may involve type constructors rather than types (so-called nested data types, see =-=[6]-=-). Thus, we would need an infinite family of fixed point operators. Furthermore, checking equality of higher-order kinded type constructors is undecidable. Haskell avoids the latter problem by using n... |

76 | Flexible Type Analysis
- Crary, Weirich
- 1999
(Show Context)
Citation Context ...ns are not new either: they were introduced by Crary, Weirich, and Morrisett [9]; the authors also observed that representations could be used to implement an explicit Dynamic type. Crary and Weirich =-=[8]-=- and Weirich [33] have also considered encodings of type representations in the more powerful type systems LX , which includes function, sum, product, and recursive kinds, and LU , which includes impr... |

56 | Dynamics in ML
- Leroy, Mauny
- 1993
(Show Context)
Citation Context ...e HindleyMilner type system. Previous approaches to supporting generic programming, typedependent optimizations, and dynamic casts within statically typed languages include: explicit dynamic typing [=-=21, 1-=-], in which the language is augmented with a Dynamic type and a typecase or cast construct; polytypic programming [19, 5, 16], in which type-dependent functions written in a language extension are tr... |

38 | Dynamic typing as staged type inference
- Shields, Sheard, et al.
- 1998
(Show Context)
Citation Context ... (lookup s t) For alternative solutions the reader is referred to Weirich [32]. EXAMPLE 2. Dynamic values also provide a simple way of implementing C's printf function in a type-safe manner; see also =-=[27]-=-. The printf function allows the programmer to show an arbitrary number of arguments of different types. Both the number of arguments and their types are specified by a so-called format string which i... |

36 |
clause programs with polymorphic types: semantics and resolution
- Horn
- 1991
(Show Context)
Citation Context ... second but last equation of rShows, for instance, handles nullary constructors while the last equation takes care of the remaining cases. 5 Related work The polymorphic Horn clause language of Hanus =-=[12]-=- generalizes the untyped Horn clause resolution semantics of Prolog to typed and polymorphically typed terms. Our initial definition of Rep in Section 2.1 is legal in this language, which appears to b... |

32 | The Generic Haskell user’s guide
- Clarke, Hinze, et al.
- 2001
(Show Context)
Citation Context ...y types and types by kinds. This made it possible to write definitions of functions like map that work for arbitrary polymorphic data structures. This approach has been implemented in Generic Haskell =-=[7]-=-, a successor to PolyP. Hinze and Peyton Jones [18] introduced derivable type classes, which can define type-indexed values within classes but are limited to kind ?. Clean's generics system [3] genera... |

28 | Dynamic types and type dependent functions
- Pil
- 1998
(Show Context)
Citation Context ...onent TypeReps from a product TypeRep. On the other hand, our typed versions constitute a safe implementation of these constructs. Clean also includes support in development for a richer type Dynamic =-=[26]-=- that includes typecase with pattern matching on (possibly polymorphic) types, in the style of Leroy and Mauny, and also supports type-dependent functions. Clean's dynamics employ a type class TC a th... |

14 |
Doaitse Swierstra. Typing dynamic typing
- Baars, S
- 2002
(Show Context)
Citation Context ...of type representations in the more powerful type systems LX , which includes function, sum, product, and recursive kinds, and LU , which includes impredicative kind polymorphism. Baars and Swierstra =-=[4]-=- have independently discovered the type representation encoding presented in Section 2. However, instead of starting with representation-passing generic functions and attempting to implement represent... |

13 | Memo functions, polytypically
- Hinze
- 2000
(Show Context)
Citation Context ...moization relies critically on the fact that they are computed only on demand and then at most once. This is guaranteed if the implementation is fully lazy. The interested reader is referred to Hinze =-=[15]-=- for background information. 3 New types So far equivalence types t $ t 0 have actually consisted of proofs of type equality, since they have been instantiated only with identity functions. But we cou... |

8 | The Girard–Reynolds isomorphism
- Wadler
- 2007
(Show Context)
Citation Context ...whether we can devise an equivalence type that only admits equal types. Perhaps surprisingly, this is indeed possible and even more surprisingly, the underlying idea goes back to Leibniz---see Wadler =-=[30]-=- for a related application of Leibniz's idea. According to Leibniz, two terms are equal if one may be substituted for the other. Adapting this principle to types, we define, newtype a $ b = EPfunEP ::... |

1 |
Playing by the rules: Rewriting as an optimization technique in GHC
- Jones, Tolmach, et al.
- 2001
(Show Context)
Citation Context ...n. Staged computation may also be useful in optimizing representation-passing by specializing generic functions to particular representations. We have also experimented with using GHC's rewrite rules =-=[24]-=- to automatically rewrite representation-based functions when type information is known at compile time. We found that functions can be fully specialized to non-recursive types, but not to recursive t... |