skip to main content
article

Generics for the masses

Published:19 September 2004Publication History
Skip Abstract Section

Abstract

A generic function is a function that can be instantiated on many data types to obtain data type specific functionality. Examples of generic functions are the functions that can be derived in Haskell, such as show, read, and '=='. The recent years have seen a number of proposals that support the definition of generic functions. Some of the proposals define new languages, some define extensions to existing languages. As a common characteristic none of the proposals can be made to work within Haskell 98: they all require something extra, either a more sophisticated type system or an additional language construct. The purpose of this pearl is to show that one can, in fact, program generically within Haskell 98 obviating to some extent the need for fancy type systems or separate tools. Haskell's type classes are at the heart of this approach: they ensure that generic functions can be defined succinctly and, in particular, that they can be used painlessly.

References

  1. Roland Backhouse, Patrik Jansson, Johan Jeuring, and Lambert Meertens. Generic Programming --- An Introduction ---. In S. Doaitse Swierstra, Pedro R. Henriques, and Jose N. Oliveira, editors, 3rd International Summer School on Advanced Functional Programming, Braga, Portugal, volume 1608 of Lecture Notes in Computer Science, pages 28--115. Springer-Verlag, Berlin, 1999.Google ScholarGoogle Scholar
  2. James Cheney and Ralf Hinze. A lightweight implementation of generics and dynamics. In Manuel M.T. Chakravarty, editor, Proceedings of the 2002 ACM SIGPLAN Haskell Workshop, pages 90--104. ACM Press, October 2002. Google ScholarGoogle ScholarDigital LibraryDigital Library
  3. Ralf Hinze. A new approach to generic functional programming. In Thomas W. Reps, editor, Proceedings of the 27th Annual ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages (POPL'00), Boston, Massachusetts, January 19-21, pages 119--132, January 2000. Google ScholarGoogle ScholarDigital LibraryDigital Library
  4. Ralf Hinze. Polytypic values possess polykinded types. Science of Computer Programming, 43:129--159, 2002.Google ScholarGoogle ScholarCross RefCross Ref
  5. Ralf Hinze. Fun with phantom types. In Jeremy Gibbons and Oege de Moor, editors, The Fun of Programming, pages 245--262. Palgrave Macmillan, 2003. ISBN 1-4039-0772-2 hardback, ISBN 0-333-99285-7 paperback.Google ScholarGoogle ScholarCross RefCross Ref
  6. Ralf Hinze and Johan Jeuring. Generic Haskell: Applications. In Roland Backhouse and Jeremy Gibbons, editors, Generic Programming: Advanced Lectures, volume 2793 of Lecture Notes in Computer Science. Springer-Verlag, 2003.Google ScholarGoogle Scholar
  7. Ralf Hinze and Johan Jeuring. Generic Haskell: Practice and theory. In Roland Backhouse and Jeremy Gibbons, editors, Generic Programming: Advanced Lectures, volume 2793 of Lecture Notes in Computer Science. Springer-Verlag, 2003.Google ScholarGoogle Scholar
  8. Ralf Hinze, Johan Jeuring, and Andres Löh. Type-indexed data types. Science of Computer Programming, 51:117--151, 2004. Google ScholarGoogle ScholarDigital LibraryDigital Library
  9. Patrik Jansson and Johan Jeuring. PolyP---a polytypic programming language extension. In Conference Record 24th ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages (POPL'97), Paris, France, pages 470--482. ACM Press, January 1997. Google ScholarGoogle ScholarDigital LibraryDigital Library
  10. Ralf Lämmel and Simon Peyton Jones. Scrap more boilerplate: reflection, zips, and generalised casts. In Kathleen Fisher, editor, Proceedings of the 2004 International Conference on Functional Programming, Snowbird, Utah, September 19--22, 2004, September 2004. Google ScholarGoogle ScholarDigital LibraryDigital Library
  11. Simon Peyton Jones. Haskell 98 Language and Libraries. Cambridge University Press, 2003.Google ScholarGoogle Scholar
  12. Simon Peyton Jones and Ralf Lämmel. Scrap your boilerplate: a practical approach to generic programming. In Proceedings of the ACM SIGPLAN Workshop on Types in Language Design and Implementation (TLDI 2003), New Orleans, January 2003. Google ScholarGoogle ScholarDigital LibraryDigital Library
  13. Philip Wadler. A prettier printer. In Jeremy Gibbons and Oege de Moor, editors, The Fun of Programming, Cornerstones of Computing, pages 223--243. Palgrave Macmillan Publishers Ltd, March 2003.Google ScholarGoogle Scholar
  14. Stephanie Weirich. Higher-order intensional type analysis in type-erasure semantics. Available from http://www.cis.upenn.edu/~sweirich/papers/erasure/erasure-paper-july03.pdf, 2003.Google ScholarGoogle Scholar

Index Terms

  1. Generics for the masses

    Recommendations

    Comments

    Login options

    Check if you have access through your login credentials or your institution to get full access on this article.

    Sign in

    Full Access

    • Published in

      cover image ACM SIGPLAN Notices
      ACM SIGPLAN Notices  Volume 39, Issue 9
      ICFP '04
      September 2004
      254 pages
      ISSN:0362-1340
      EISSN:1558-1160
      DOI:10.1145/1016848
      Issue’s Table of Contents
      • cover image ACM Conferences
        ICFP '04: Proceedings of the ninth ACM SIGPLAN international conference on Functional programming
        September 2004
        264 pages
        ISBN:1581139055
        DOI:10.1145/1016850

      Copyright © 2004 ACM

      Permission to make digital or hard copies of all or part of this work for personal or classroom use is granted without fee provided that copies are not made or distributed for profit or commercial advantage and that copies bear this notice and the full citation on the first page. Copyrights for components of this work owned by others than ACM must be honored. Abstracting with credit is permitted. To copy otherwise, or republish, to post on servers or to redistribute to lists, requires prior specific permission and/or a fee. Request permissions from [email protected]

      Publisher

      Association for Computing Machinery

      New York, NY, United States

      Publication History

      • Published: 19 September 2004

      Check for updates

      Qualifiers

      • article

    PDF Format

    View or Download as a PDF file.

    PDF

    eReader

    View online with eReader.

    eReader