Scrap Your Boilerplate Revolutions

Ralf Hinze, Andres Löh. Scrap Your Boilerplate Revolutions. In Tarmo Uustalu, editor, Mathematics of Program Construction, 8th International Conference, MPC 2006, Kuressaare, Estonia, July 3-5, 2006, Proceedings. Volume 4014 of Lecture Notes in Computer Science, pages 180-208, Springer, 2006. [doi]

Abstract

Generic programming allows you to write a function once, and use it many times at different types. Traditionally, generic functions are defined by induction on the structure of types. “Scrap your boilerplate” (SYB) is a radically different approach that dispatches on the structure of values. In previous work, we have shown how to reconcile both approaches using the concept of generic views: many generic functions can be based either on the classical sum-of-products view or on the view that underlies SYB, the so-called ‘spine’ view. One distinct advantage of the spine view is its generality: it is applicable to a large class of data types, including generalised algebraic data types. Its main weakness roots in the value-orientation: one can only define generic functions that consume data (show) but not ones that produce data (read). Furthermore, functions that abstract over type constructors (map, reduce) are out of reach. In this paper, we show how to overcome both limitations. The main technical contributions are the ‘type spine’ view and the ‘lifted spine’ view.