Polytypic programming is a way of defining type-indexed operations, such as map, fold and zip, based on type information. Run-time polytypic programming allows that type information to be dynamically computed-this support is essential in modern programming languages that support separate compilation, first-class type abstraction, or polymorphic recursion. However, in previous work we defined run-time polytypic programming with a typepassing semantics. Although it is natural to define polytypic programs as operating over first-class types, such a semantics suffers from a number of drawbacks. This paper describes how to recast that work in a type-erasure semantics, where terms represent type information in a safe manner. The resulting language is simple and easy to implement-we present a prototype implementation of the necessary machinery as a small Haskell library. Some functions are naturally defined by the type structure of their arguments. For example, a polytypic pretty printer can format any data structure by using type information to decompose it into basic parts. Without such a mechanism, one must write separate pretty printers for all data types and constantly update them as data types evolve. Polytypic programming simplifies the maintenance of software by allowing functions to automatically adapt to changes in the representation of data. Other classic examples of polytypic operations include reductions, comparison functions and mapping functions. The theory behind such operations has been developed in a variety of frameworks (Abadi et al., 1991; Abadi et al., 1995; Crary & Weirich, 1999; Dubois et al., 1995; Hinze, 2000; Jansson & Jeuring, 1997; Jay et al., 1998; Ruehr, 1998; Sheard, 1993; Trifonov et al., 2000; Wadler & Blott, 1989) . Many of these frameworks generate polytypic operations at compile time through a source-to-source translation determined by static type information. In contrast, run-time polytypic programming (also called higher-order intensional type analysis (Weirich, 2002a)) defines polytypic operations with run-time analysis of dynamic type information. Run-time type analysis has two advantages over static forms of polytypism: First, run-time analysis may index polytypic operations by types that
To motivate a programming language that allows the explicit manipulation and analysis of type expressions in a typed manner, Weirich REF uses a generic marshalling function that recursively analyzes the type of the value.
Type-Safe Run-time Polytypic Programming
J. Funct. Program.
J. Funct. Program.
Computer Science