skip to main content
research-article

Variations on variants

Published:30 August 2015Publication History
Skip Abstract Section

Abstract

Extensible variants improve the modularity and expressiveness of programming languages: they allow program functionality to be decomposed into independent blocks, and allow seamless extension of existing code with both new cases of existing data types and new operations over those data types. This paper considers three approaches to providing extensible variants in Haskell. Row typing is a long understood mechanism for typing extensible records and variants, but its adoption would require extension of Haskell's core type system. Alternatively, we might hope to encode extensible variants in terms of existing mechanisms, such as type classes. We describe an encoding of extensible variants using instance chains, a proposed extension of the class system. Unlike many previous encodings of extensible variants, ours does not require the definition of a new type class for each function that consumes variants. Finally, we translate our encoding to use closed type families, an existing feature of GHC. Doing so demonstrates the interpretation of instances chains and functional dependencies in closed type families. One concern with encodings like ours is how completely they match the encoded system. We compare the expressiveness of our encodings with each other and with systems based on row types. We find that, while equivalent terms are typable in each system, both encodings require explicit type annotations to resolve ambiguities in typing not present in row type systems, and the type family implementation retains more constraints in principal types than does the instance chain implementation. We propose a general mechanism to guide the instantiation of ambiguous type variables, show that it eliminates the need for type annotations in our encodings, and discuss conditions under which it preserves coherence.

References

  1. P. Bahr. Composing and decomposing data types: a closed type families implementation of data types à la carte. In WGP 2014, pages 71–82, Gothenburg, Sweden, 2014. ACM. Google ScholarGoogle ScholarDigital LibraryDigital Library
  2. M. Blume, U. A. Acar, and W. Chae. Extensible programming with first-class cases. In ICFP ’06, pages 239–250, Portland, Oregon, 2006. ACM. Google ScholarGoogle ScholarDigital LibraryDigital Library
  3. R. A. Eisenberg, D. Vytiniotis, S. Peyton Jones, and S. Weirich. Closed type families with overlapping equations. In POPL ’14, pages 671–683, San Diego, California, USA, 2014. ACM. Google ScholarGoogle ScholarDigital LibraryDigital Library
  4. J. Garrigue. Programming with polymorphic variants. In ACM SIGPLAN workshop on ML, 2008.Google ScholarGoogle Scholar
  5. B. R. Gaster and M. P. Jones. A polymorphic type system for extensible records and variants. Technical Report NOTTCS-TR-96-3, University of Nottingham, 1996.Google ScholarGoogle Scholar
  6. M. P. Jones. A theory of qualified types. In B. K. Bruckner, editor, Proceedings of the 4th European symposium on programming, volume 582 of ESOP’92. Springer-Verlag, Rennes, France, 1992. Google ScholarGoogle ScholarDigital LibraryDigital Library
  7. M. P. Jones. Type classes with functional dependencies. In ESOP ’00, pages 230–244, Berlin, Germany, 2000. Springer-Verlag. Google ScholarGoogle ScholarDigital LibraryDigital Library
  8. M. P. Jones and I. S. Diatchki. Language and program design for functional dependencies. In Haskell ’08, pages 87–98, Victoria, BC, Canada, 2008. ACM. Google ScholarGoogle ScholarDigital LibraryDigital Library
  9. O. Kiselyov, R. Lämmel, and K. Schupke. Strongly typed heterogeneous collections. In Proceedings of the 2004 ACM SIGPLAN workshop on Haskell, Haskell ’04, pages 96–107, Snowbird, Utah, USA, 2004. Google ScholarGoogle ScholarDigital LibraryDigital Library
  10. ACM Press.Google ScholarGoogle Scholar
  11. S. Liang, P. Hudak, and M. Jones. Monad transformers and modular interpreters. In Proceedings of the 22nd ACM SIGPLAN-SIGACT symposium on Principles of programming languages, POPL ’95, pages 333–343, San Francisco, California, 1995. ACM. Google ScholarGoogle ScholarDigital LibraryDigital Library
  12. J. G. Morris. Types Classes and Instance Chains: A Relational Approach. PhD thesis, Portland State University, 2013.Google ScholarGoogle Scholar
  13. J. G. Morris and M. P. Jones. Instance chains: Type-class programming without overlapping instances. In ICFP ’10, Baltimore, MD, 2010. ACM. Google ScholarGoogle ScholarDigital LibraryDigital Library
  14. S. Peyton Jones, editor. Haskell 98 Language and Libraries – The Revised Report. Cambridge University Press, 2003.Google ScholarGoogle Scholar
  15. D. Rémy. Typechecking records and variants in a natural extension of ML. In POPL ’89, pages 77–88, Austin, Texas, 1989. ACM. Google ScholarGoogle ScholarDigital LibraryDigital Library
  16. D. Rémy. Typing record concatenation for free. In POPL ’92, pages 166–176, Albuquerque, New Mexico, 1992. ACM. Google ScholarGoogle ScholarDigital LibraryDigital Library
  17. T. Sheard and E. Pasalic. Two-level types and parameterized modules. J. Funct. Program., 14(5):547–587, Sep 2004. Google ScholarGoogle ScholarDigital LibraryDigital Library
  18. W. Swierstra. Data types à la carte. J. Funct. Program., 18(04):423–436, 2008. Google ScholarGoogle ScholarDigital LibraryDigital Library
  19. P. Wadler. The expression problem. http://homepages.inf.ed.ac. uk/wadler/papers/expression/expression.txt, 1998.Google ScholarGoogle Scholar
  20. M. Wand. Complete type inference for simple objects. In LICS ’87, pages 37–44, Ithaca, New York, 1987. IEEE.Google ScholarGoogle Scholar

Index Terms

  1. Variations on variants

    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 50, Issue 12
      Haskell '15
      December 2015
      212 pages
      ISSN:0362-1340
      EISSN:1558-1160
      DOI:10.1145/2887747
      Issue’s Table of Contents
      • cover image ACM Conferences
        Haskell '15: Proceedings of the 2015 ACM SIGPLAN Symposium on Haskell
        August 2015
        212 pages
        ISBN:9781450338080
        DOI:10.1145/2804302

      Copyright © 2015 ACM

      Publisher

      Association for Computing Machinery

      New York, NY, United States

      Publication History

      • Published: 30 August 2015

      Check for updates

      Qualifiers

      • research-article

    PDF Format

    View or Download as a PDF file.

    PDF

    eReader

    View online with eReader.

    eReader
    About Cookies On This Site

    We use cookies to ensure that we give you the best experience on our website.

    Learn more

    Got it!