Abstract
We present instance arguments: an alternative to type classes and related features in the dependently typed, purely functional programming language/proof assistant Agda. They are a new, general type of function arguments, resolved from call-site scope in a type-directed way. The mechanism is inspired by both Scala's implicits and Agda's existing implicit arguments, but differs from both in important ways. Our mechanism is designed and implemented for Agda, but our design choices can be applied to other programming languages as well.
Like Scala's implicits, we do not provide a separate structure for type classes and their instances, but instead rely on Agda's standard dependently typed records, so that standard language mechanisms provide features that are missing or expensive in other proposals. Like Scala, we support the equivalent of local instances. Unlike Scala, functions taking our new arguments are first-class citizens and can be abstracted over and manipulated in standard ways. Compared to other proposals, we avoid the pitfall of introducing a separate type-level computational model through the instance search mechanism. All values in scope are automatically candidates for instance resolution. A final novelty of our approach is that existing Agda libraries using records gain the benefits of type classes without any modification.
We discuss our implementation in Agda (to be part of Agda 2.2.12) and we use monads as an example to show how it allows existing concepts in the Agda standard library to be used in a similar way as corresponding Haskell code using type classes. We also demonstrate and discuss equivalents and alternatives to some advanced type class-related patterns from the literature and some new patterns specific to our system.
Supplemental Material
- Y. Bertot, G. Gonthier, S. Ould Biha, and I. Pasca. Canonical big operators. In TPHOLs, 2008. Google Scholar
Digital Library
- B. C. d. S. Oliveira, A. Moors, and M. Odersky. Type classes as objects and implicits. In OOPSLA, 2010. Google Scholar
Digital Library
- N. Danielsson, U. Norell, S. Mu, S. Bronson, D. Doel, P. Jansson, and L. Chen. The Agda standard library, 2009.Google Scholar
- A. Dijkstra and S. D. Swierstra. Making implicit parameters explicit. Technical Report UU-CS-2005-032, Dept. ICS, Utrecht University, 2005.Google Scholar
- D. Dreyer, R. Harper, M. M. T. Chakravarty, and G. Keller. Modular type classes. In POPL, 2007. Google Scholar
Digital Library
- D. Duggan and J. Ophel. Type-checking multi-parameter type classes. Journal of Functional Programming, 12 (02): 133--158, 2002. Google Scholar
Digital Library
- B. Heeren, J. Hage, and S. Swierstra. Scripting the type inference process. In ACM SIGPLAN Notices, volume 38, 2003. Google Scholar
Digital Library
- P. Hudak, J. Hughes, S. Peyton Jones, and P. Wadler. A history of haskell: being lazy with class. In History of Programming Languages, 2007. Google Scholar
Digital Library
- M. Jones. Type classes with functional dependencies. PLAS, pages 230--244, 2003. Google Scholar
Digital Library
- W. Kahl and J. Scheffczyk. Named instances for Haskell type classes. In HASKELL, 2001.Google Scholar
- O. Kiselyov and C.-c. Shan. Functional pearl: Implicit configurations. In HASKELL, 2004. Google Scholar
Digital Library
- R. Lämmel and S. Peyton Jones. Scrap your boilerplate with class: extensible generic functions. In ICFP, pages 204--215, 2005.Google Scholar
Digital Library
- J. Lewis, J. Launchbury, E. Meijer, and M. Shields. Implicit parameters: Dynamic scoping with static types. In POPL, pages 108--118, 2000. Google Scholar
Digital Library
- C. McBride. Faking it: Simulating dependent types in haskell. Journal of Functional Programming, 12 (4 & 5), 2002. Google Scholar
Digital Library
- U. Norell. Towards a practical programming language based on dependent type theory. PhD thesis, Chalmers University of Technology, 2007.Google Scholar
- U. Norell and C. Coquand. Type checking in the presence of meta-variables. Unpublished draft, 2007. URL http://www.cse.chalmers.se/~ulfn/papers/meta-variables.html.Google Scholar
- M. Odersky. The Scala language specification, version 2.8. online, 2010. URL http://www.scala-lang.org/docu/files/ScalaReference.pdf.Google Scholar
- D. Orchard and T. Schrijvers. Haskell type constraints unleashed. In FLOPS, 2010. Google Scholar
Digital Library
- S. Peyton Jones and R. Lämmel. Scrap your boilerplate. In APLAS, page 357, 2003.Google Scholar
- S. Peyton Jones, M. Jones, and E. Meijer. Type classes: an exploration of the design space. In HASKELL, 1997.Google Scholar
- S. Peyton Jones, D. Vytiniotis, S. Weirich, and G. Washburn. Simple unification-based type inference for GADTs. In ICFP, 2006. Google Scholar
Digital Library
- S. Peyton Jones, D. Vytiniotis, S. Weirich, and M. Shields. Practical type inference for arbitrary-rank types. Journal of Functional Programming, 17 (1), 2007. Google Scholar
Digital Library
- T. Schrijvers, S. Peyton Jones, M. Chakravarty, and M. Sulzmann. Type checking with open type functions. In ICFP, 2008. Google Scholar
Digital Library
- M. Sozeau and N. Oury. First-class type classes. In TPHOLs, 2008. Google Scholar
Digital Library
- The Coq development team. The Coq reference manual. online, 2010. URL http://coq.inria.fr/refman/. v8.3.Google Scholar
- P. Wadler and S. Blott. How to make ad-hoc polymorphism less ad hoc. In POPL, 1989. Google Scholar
Digital Library
- S. Weirich. Replib: a library for derivable type classes. In HASKELL, 2006. Google Scholar
Digital Library
Index Terms
On the bright side of type classes: instance arguments in Agda
Recommendations
On the bright side of type classes: instance arguments in Agda
ICFP '11: Proceedings of the 16th ACM SIGPLAN international conference on Functional programmingWe present instance arguments: an alternative to type classes and related features in the dependently typed, purely functional programming language/proof assistant Agda. They are a new, general type of function arguments, resolved from call-site scope ...
Modular type classes
Proceedings of the 2007 POPL ConferenceML modules and Haskell type classes have proven to be highly effective tools for program structuring. Modules emphasize explicit configuration of program components and the use of data abstraction. Type classes emphasize implicit program construction ...
Type families with class, type classes with family
Haskell '15: Proceedings of the 2015 ACM SIGPLAN Symposium on HaskellType classes and type families are key ingredients in Haskell programming. Type classes were introduced to deal with ad-hoc polymorphism, although with the introduction of functional dependencies, their use expanded to type-level programming. Type ...







Comments