skip to main content
research-article

On the bright side of type classes: instance arguments in Agda

Published:19 September 2011Publication History
Skip Abstract Section

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.

Skip Supplemental Material Section

Supplemental Material

_talk1.mp4

References

  1. Y. Bertot, G. Gonthier, S. Ould Biha, and I. Pasca. Canonical big operators. In TPHOLs, 2008. Google ScholarGoogle ScholarDigital LibraryDigital Library
  2. B. C. d. S. Oliveira, A. Moors, and M. Odersky. Type classes as objects and implicits. In OOPSLA, 2010. Google ScholarGoogle ScholarDigital LibraryDigital Library
  3. N. Danielsson, U. Norell, S. Mu, S. Bronson, D. Doel, P. Jansson, and L. Chen. The Agda standard library, 2009.Google ScholarGoogle Scholar
  4. A. Dijkstra and S. D. Swierstra. Making implicit parameters explicit. Technical Report UU-CS-2005-032, Dept. ICS, Utrecht University, 2005.Google ScholarGoogle Scholar
  5. D. Dreyer, R. Harper, M. M. T. Chakravarty, and G. Keller. Modular type classes. In POPL, 2007. Google ScholarGoogle ScholarDigital LibraryDigital Library
  6. D. Duggan and J. Ophel. Type-checking multi-parameter type classes. Journal of Functional Programming, 12 (02): 133--158, 2002. Google ScholarGoogle ScholarDigital LibraryDigital Library
  7. B. Heeren, J. Hage, and S. Swierstra. Scripting the type inference process. In ACM SIGPLAN Notices, volume 38, 2003. Google ScholarGoogle ScholarDigital LibraryDigital Library
  8. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  9. M. Jones. Type classes with functional dependencies. PLAS, pages 230--244, 2003. Google ScholarGoogle ScholarDigital LibraryDigital Library
  10. W. Kahl and J. Scheffczyk. Named instances for Haskell type classes. In HASKELL, 2001.Google ScholarGoogle Scholar
  11. O. Kiselyov and C.-c. Shan. Functional pearl: Implicit configurations. In HASKELL, 2004. Google ScholarGoogle ScholarDigital LibraryDigital Library
  12. R. Lämmel and S. Peyton Jones. Scrap your boilerplate with class: extensible generic functions. In ICFP, pages 204--215, 2005.Google ScholarGoogle ScholarDigital LibraryDigital Library
  13. J. Lewis, J. Launchbury, E. Meijer, and M. Shields. Implicit parameters: Dynamic scoping with static types. In POPL, pages 108--118, 2000. Google ScholarGoogle ScholarDigital LibraryDigital Library
  14. C. McBride. Faking it: Simulating dependent types in haskell. Journal of Functional Programming, 12 (4 & 5), 2002. Google ScholarGoogle ScholarDigital LibraryDigital Library
  15. U. Norell. Towards a practical programming language based on dependent type theory. PhD thesis, Chalmers University of Technology, 2007.Google ScholarGoogle Scholar
  16. 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 ScholarGoogle Scholar
  17. M. Odersky. The Scala language specification, version 2.8. online, 2010. URL http://www.scala-lang.org/docu/files/ScalaReference.pdf.Google ScholarGoogle Scholar
  18. D. Orchard and T. Schrijvers. Haskell type constraints unleashed. In FLOPS, 2010. Google ScholarGoogle ScholarDigital LibraryDigital Library
  19. S. Peyton Jones and R. Lämmel. Scrap your boilerplate. In APLAS, page 357, 2003.Google ScholarGoogle Scholar
  20. S. Peyton Jones, M. Jones, and E. Meijer. Type classes: an exploration of the design space. In HASKELL, 1997.Google ScholarGoogle Scholar
  21. S. Peyton Jones, D. Vytiniotis, S. Weirich, and G. Washburn. Simple unification-based type inference for GADTs. In ICFP, 2006. Google ScholarGoogle ScholarDigital LibraryDigital Library
  22. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  23. T. Schrijvers, S. Peyton Jones, M. Chakravarty, and M. Sulzmann. Type checking with open type functions. In ICFP, 2008. Google ScholarGoogle ScholarDigital LibraryDigital Library
  24. M. Sozeau and N. Oury. First-class type classes. In TPHOLs, 2008. Google ScholarGoogle ScholarDigital LibraryDigital Library
  25. The Coq development team. The Coq reference manual. online, 2010. URL http://coq.inria.fr/refman/. v8.3.Google ScholarGoogle Scholar
  26. P. Wadler and S. Blott. How to make ad-hoc polymorphism less ad hoc. In POPL, 1989. Google ScholarGoogle ScholarDigital LibraryDigital Library
  27. S. Weirich. Replib: a library for derivable type classes. In HASKELL, 2006. Google ScholarGoogle ScholarDigital LibraryDigital Library

Index Terms

  1. On the bright side of type classes: instance arguments in Agda

    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 46, Issue 9
      ICFP '11
      September 2011
      456 pages
      ISSN:0362-1340
      EISSN:1558-1160
      DOI:10.1145/2034574
      Issue’s Table of Contents
      • cover image ACM Conferences
        ICFP '11: Proceedings of the 16th ACM SIGPLAN international conference on Functional programming
        September 2011
        470 pages
        ISBN:9781450308656
        DOI:10.1145/2034773

      Copyright © 2011 ACM

      Publisher

      Association for Computing Machinery

      New York, NY, United States

      Publication History

      • Published: 19 September 2011

      Check for updates

      Qualifiers

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