Nothing Special   »   [go: up one dir, main page]

skip to main content
article

Improving typeclass relations by being open

Published: 17 September 2018 Publication History

Abstract

Mathematical concepts such as monads, functors, monoids, and semigroups are expressed in Haskell as typeclasses. Therefore, in order to exploit relations such as “every monad is a functor”, and “every monoid is a semigroup”, we need to be able to also express relations between typeclasses.
Currently, the only way to do so is using superclasses. However, superclasses can be problematic due to their closed nature. Adding a superclass implies modifying the subclass’ definition, which is either impossible if one does not own such code, or painful as it requires cascading changes and the introduction of boilerplate throughout the codebase.
In this article, we introduce class morphisms, a way to relate classes in an open fashion, without changing class definitions. We show how class morphisms improve the expressivity, conciseness, and maintainability of code. Further, we show how to implement them while maintaining canonicity and coherence, two key properties of the Haskell type system. Extending a typechecker with class morphisms amounts to adding an elaboration phase and is an unintrusive change. We back this claim with a prototype extension of GHC.

References

[1]
Baldur Blöndal, Andres Löh, and Ryan Scott. 2018. Deriving Via or, How to Turn Hand-Written Instances into an Anti-Pattern. In Proceedings of the ACM SIGPLAN Haskell Symposium 2018.
[2]
Leonardo Mendonça de Moura, Jeremy Avigad, Soonho Kong, and Cody Roux. 2015. Elaboration in Dependent Type Theory. CoRR abs/1505.04324 (2015). arXiv: 1505.04324 http://arxiv.org/abs/1505.04324
[3]
Dominique Devriese and Frank Piessens. 2011. On the Bright Side of Type Classes: Instance Arguments in Agda. In Proceedings of the 16th ACM SIGPLAN International Conference on Functional Programming (ICFP ’11). ACM, New York, USA, 143–155.
[4]
Richard Eisenberg. 2014. Instance Template Proposal. https://ghc.haskell. org/trac/ghc/wiki/InstanceTemplates
[5]
Haskell Wiki. 2014. Functor-Applicative-Monad Proposal. https://wiki. haskell.org/Functor-Applicative-Monad_Proposal
[6]
Mark P. Jones. 1995. Qualified Types: Theory and Practice. Cambridge University Press, New York, NY, USA.
[7]
Guido Martínez, Mauro Jaskelioff, and Guido De Luca. 2018. Improving Typeclass Relations by Being Open (extended version). https://www. fceia.unr.edu.ar/~mauro/pubs/cm-extended.pdf
[8]
Conor McBride. 2011. Default Superclasses Proposal. https://ghc.haskell. org/trac/ghc/wiki/DefaultSuperclassInstances
[9]
Conor McBride. 2014. Intrinsic Superclasses Proposal. https://ghc.haskell. org/trac/ghc/wiki/IntrinsicSuperclasses
[10]
J. Garrett Morris and Mark P. Jones. 2010. Instance Chains: Type Class Programming Without Overlapping Instances. In Proceedings of the 15th ACM SIGPLAN International Conference on Functional Programming (ICFP ’10). ACM, New York, NY, USA, 375–386.
[11]
Matthieu Sozeau and Nicolas Oury. 2008. First-Class Type Classes. In Theorem Proving in Higher Order Logics, Otmane Ait Mohamed, César Muñoz, and Sofiène Tahar (Eds.). Springer Berlin Heidelberg, Berlin, Heidelberg, 278–293.
[12]
Martin Sulzmann and Meng Wang. 2006. Modular Generic Programming with Extensible Superclasses. In Proceedings of the 2006 ACM SIGPLAN Workshop on Generic Programming (WGP ’06). ACM, New York, NY, USA, 55–65.
[13]
The Glasgow Haskell Team. 1989–2018. GHC: The Glasgow Haskell Compiler. https://www.haskell.org/ghc/ .
[14]
P. Wadler and S. Blott. 1989. How to Make Ad-hoc Polymorphism Less Ad Hoc. In Proceedings of the 16th ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages (POPL ’89). ACM, New York, NY, USA, 60–76.

Recommendations

Comments

Please enable JavaScript to view thecomments powered by Disqus.

Information & Contributors

Information

Published In

cover image ACM SIGPLAN Notices
ACM SIGPLAN Notices  Volume 53, Issue 7
Haskell '18
July 2018
185 pages
ISSN:0362-1340
EISSN:1558-1160
DOI:10.1145/3299711
Issue’s Table of Contents
  • cover image ACM Conferences
    Haskell 2018: Proceedings of the 11th ACM SIGPLAN International Symposium on Haskell
    September 2018
    185 pages
    ISBN:9781450358354
    DOI:10.1145/3242744
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 the author(s) 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: 17 September 2018
Published in SIGPLAN Volume 53, Issue 7

Check for updates

Author Tags

  1. Ad-hoc polymorphism
  2. Type systems
  3. Typeclasses

Qualifiers

  • Article

Contributors

Other Metrics

Bibliometrics & Citations

Bibliometrics

Article Metrics

  • Downloads (Last 12 months)6
  • Downloads (Last 6 weeks)0
Reflects downloads up to 25 Nov 2024

Other Metrics

Citations

View Options

Login options

View options

PDF

View or Download as a PDF file.

PDF

eReader

View online with eReader.

eReader

Media

Figures

Other

Tables

Share

Share

Share this Publication link

Share on social media