skip to main content
article

Improving typeclass relations by being open

Published:17 September 2018Publication History
Skip Abstract Section

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. Google ScholarGoogle ScholarDigital LibraryDigital Library
  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.04324Google ScholarGoogle Scholar
  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. Google ScholarGoogle ScholarDigital LibraryDigital Library
  4. Richard Eisenberg. 2014. Instance Template Proposal. https://ghc.haskell. org/trac/ghc/wiki/InstanceTemplatesGoogle ScholarGoogle Scholar
  5. Haskell Wiki. 2014. Functor-Applicative-Monad Proposal. https://wiki. haskell.org/Functor-Applicative-Monad_ProposalGoogle ScholarGoogle Scholar
  6. Mark P. Jones. 1995. Qualified Types: Theory and Practice. Cambridge University Press, New York, NY, USA. Google ScholarGoogle ScholarDigital LibraryDigital Library
  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.pdfGoogle ScholarGoogle Scholar
  8. Conor McBride. 2011. Default Superclasses Proposal. https://ghc.haskell. org/trac/ghc/wiki/DefaultSuperclassInstancesGoogle ScholarGoogle Scholar
  9. Conor McBride. 2014. Intrinsic Superclasses Proposal. https://ghc.haskell. org/trac/ghc/wiki/IntrinsicSuperclassesGoogle ScholarGoogle Scholar
  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. Google ScholarGoogle ScholarDigital LibraryDigital Library
  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. Google ScholarGoogle ScholarDigital LibraryDigital Library
  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. Google ScholarGoogle ScholarDigital LibraryDigital Library
  13. The Glasgow Haskell Team. 1989–2018. GHC: The Glasgow Haskell Compiler. https://www.haskell.org/ghc/ .Google ScholarGoogle Scholar
  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. Google ScholarGoogle ScholarDigital LibraryDigital Library

Index Terms

  1. Improving typeclass relations by being open

      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 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

        Copyright © 2018 ACM

        Publisher

        Association for Computing Machinery

        New York, NY, United States

        Publication History

        • Published: 17 September 2018

        Check for updates

        Qualifiers

        • 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!