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.
- 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 Scholar
Digital Library
- 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 Scholar
Digital Library
- 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 Scholar
Digital Library
- J. Garrigue. Programming with polymorphic variants. In ACM SIGPLAN workshop on ML, 2008.Google Scholar
- 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 Scholar
- 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 Scholar
Digital Library
- M. P. Jones. Type classes with functional dependencies. In ESOP ’00, pages 230–244, Berlin, Germany, 2000. Springer-Verlag. Google Scholar
Digital Library
- 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 Scholar
Digital Library
- 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 Scholar
Digital Library
- ACM Press.Google Scholar
- 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 Scholar
Digital Library
- J. G. Morris. Types Classes and Instance Chains: A Relational Approach. PhD thesis, Portland State University, 2013.Google Scholar
- J. G. Morris and M. P. Jones. Instance chains: Type-class programming without overlapping instances. In ICFP ’10, Baltimore, MD, 2010. ACM. Google Scholar
Digital Library
- S. Peyton Jones, editor. Haskell 98 Language and Libraries – The Revised Report. Cambridge University Press, 2003.Google Scholar
- D. Rémy. Typechecking records and variants in a natural extension of ML. In POPL ’89, pages 77–88, Austin, Texas, 1989. ACM. Google Scholar
Digital Library
- D. Rémy. Typing record concatenation for free. In POPL ’92, pages 166–176, Albuquerque, New Mexico, 1992. ACM. Google Scholar
Digital Library
- T. Sheard and E. Pasalic. Two-level types and parameterized modules. J. Funct. Program., 14(5):547–587, Sep 2004. Google Scholar
Digital Library
- W. Swierstra. Data types à la carte. J. Funct. Program., 18(04):423–436, 2008. Google Scholar
Digital Library
- P. Wadler. The expression problem. http://homepages.inf.ed.ac. uk/wadler/papers/expression/expression.txt, 1998.Google Scholar
- M. Wand. Complete type inference for simple objects. In LICS ’87, pages 37–44, Ithaca, New York, 1987. IEEE.Google Scholar
Index Terms
Variations on variants
Recommendations
Abstracting extensible data types: or, rows by any other name
We present a novel typed language for extensible data types, generalizing and abstracting existing systems of row types and row polymorphism. Extensible data types are a powerful addition to traditional functional programming languages, capturing ideas ...
Variations on variants
Haskell '15: Proceedings of the 2015 ACM SIGPLAN Symposium on HaskellExtensible 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 ...
Good advice for type-directed programming aspect-oriented programming and extensible generic functions
WGP '06: Proceedings of the 2006 ACM SIGPLAN workshop on Generic programmingType-directed programming is an important idiom for software design. In type-directed programming the behavior of programs is guided by the type structure of data. It makes it possible to implement many sorts of operations, such as serialization,...






Comments