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.
- 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 Scholar
Digital Library
- 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 Scholar
- 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 Scholar
Digital Library
- Richard Eisenberg. 2014. Instance Template Proposal. https://ghc.haskell. org/trac/ghc/wiki/InstanceTemplatesGoogle Scholar
- Haskell Wiki. 2014. Functor-Applicative-Monad Proposal. https://wiki. haskell.org/Functor-Applicative-Monad_ProposalGoogle Scholar
- Mark P. Jones. 1995. Qualified Types: Theory and Practice. Cambridge University Press, New York, NY, USA. Google Scholar
Digital Library
- 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 Scholar
- Conor McBride. 2011. Default Superclasses Proposal. https://ghc.haskell. org/trac/ghc/wiki/DefaultSuperclassInstancesGoogle Scholar
- Conor McBride. 2014. Intrinsic Superclasses Proposal. https://ghc.haskell. org/trac/ghc/wiki/IntrinsicSuperclassesGoogle Scholar
- 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 Scholar
Digital Library
- 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 Scholar
Digital Library
- 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 Scholar
Digital Library
- The Glasgow Haskell Team. 1989–2018. GHC: The Glasgow Haskell Compiler. https://www.haskell.org/ghc/ .Google Scholar
- 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 Scholar
Digital Library
Index Terms
Improving typeclass relations by being open
Recommendations
Improving typeclass relations by being open
Haskell 2018: Proceedings of the 11th ACM SIGPLAN International Symposium on HaskellMathematical 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 ...
PolyAML: a polymorphic aspect-oriented functional programming language
ICFP '05: Proceedings of the tenth ACM SIGPLAN international conference on Functional programmingThis paper defines PolyAML, a typed functional, aspect-oriented programming language. The main contribution of Poly<SMALL>AML</SMALL> is the seamless integration of polymorphism, run-time type analysis and aspect-oriented programming language features. ...
PolyAML: a polymorphic aspect-oriented functional programming language
Proceedings of the tenth ACM SIGPLAN international conference on Functional programmingThis paper defines PolyAML, a typed functional, aspect-oriented programming language. The main contribution of Poly<SMALL>AML</SMALL> is the seamless integration of polymorphism, run-time type analysis and aspect-oriented programming language features. ...







Comments