Skip to main content

Fun with Type Functions

  • Chapter
  • First Online:

Abstract

Tony Hoare has always been a leader in writing down and proving properties of programs. To prove properties of programs automatically, the most widely used technology today is the ubiquitous type checker. Alas, static type systems inevitably exclude some good programs and allow some bad ones. Thus motivated, we describe some fun we have been having with Haskell, by making the type system more expressive without losing the benefits of automatic proof and compact expression. Specifically, we offer a programmer’s tour of so-calledtype families, a recent extension to Haskell that allows functions on types to be expressed as straightforwardly as functions on values. This facility makes it easier for programmers to effectively extend the compiler by writing functional programs that execute during type checking. Source code for all the examples is available at http://research.microsoft.com/simonpj/papers/assoc-types/fun-with-type-funs.zip.

This is a preview of subscription content, log in via an institution.

Buying options

Chapter
USD   29.95
Price excludes VAT (USA)
  • Available as PDF
  • Read on any device
  • Instant download
  • Own it forever
eBook
USD   129.00
Price excludes VAT (USA)
  • Available as EPUB and PDF
  • Read on any device
  • Instant download
  • Own it forever
Softcover Book
USD   169.99
Price excludes VAT (USA)
  • Compact, lightweight edition
  • Dispatched in 3 to 5 business days
  • Free shipping worldwide - see info
Hardcover Book
USD   169.99
Price excludes VAT (USA)
  • Durable hardcover edition
  • Dispatched in 3 to 5 business days
  • Free shipping worldwide - see info

Tax calculation will be finalised at checkout

Purchases are for personal use only

Learn about institutional subscriptions

Notes

  1. 1.

    “=” is used for too many other things.

  2. 2.

    A possible extension, not currently implemented by GHC, would be to allow an associatedtypesynonym declaration optionally to specify that it should be injective, and to check that this property is maintained as eachinstanceis added.

  3. 3.

    http://hackage.haskell.org/cgi-bin/hackage-scripts/package/pointless-haskell

  4. 4.

    Cmp” is short for “compose”.

  5. 5.

    GHC requires the alarming flag-XAllowUndecidableInstancesto accept the(Cf1f2)instance forTPrinter, because thenestedrecursive call toTPrinterdoes not “obviously terminate.” Of course, every call toTPrinterdoes terminate, because the second argument (where the nested recursive call is made) is not scrutinised by any of the equations, but this is a non-local property that GHC does not check. The flag promises the compiler thatTPrinterwill terminate; the worst that can happen if the programmer makes an erroneous promise is that the type checker diverges.

  6. 6.

    http://okmij.org/ftp/Haskell/types.html#polyvar-fn

  7. 7.

    http://okmij.org/ftp/Haskell/typecast.html#solving-read-show

  8. 8.

    http://okmij.org/ftp/Haskell/typecast.html#is-function-type

  9. 9.

    http://okmij.org/ftp/Haskell/keyword-arguments.lhs

References

  1. Asai, K.: On typing delimited continuations: three new solutions to the printf problem. Higher-Order and Symbolic Computation.22(3), 275–291 (2009)

    Article  MATH  MathSciNet  Google Scholar 

  2. Atkey, R.: Parameterised notions of computation. J. Funct. Program.19(3&4), 355–376 (2009)

    MathSciNet  Google Scholar 

  3. Bove, A., Dybjer, P.: Dependent types at work. In: International summer school on language engineering and rigorous software development. Lecture Notes in Computer Science 5520 (2009)

    Chapter  Google Scholar 

  4. Carette, J., Kiselyov, O., Shan, C.-c.: Finally tagless, partially evaluated: tagless staged interpreters for simpler typed languages. J. Funct. Program.19(5), 509–543 (2009)

    Article  MATH  MathSciNet  Google Scholar 

  5. Chakravarty, M.M.T.: Type families.http://haskell.org/haskellwiki/GHC/Indexed_types(2008)

    Google Scholar 

  6. Chakravarty, M.M.T., Keller, G., PeytonJones, S.L.: Associated type synonyms. In: ICFP ’05: Proc. ACM international conference on functional programming, pp.241–253. ACM Press, New York (2005)

    Chapter  Google Scholar 

  7. Chakravarty, M.M.T., Keller, G., PeytonJones, S.L., Marlow, S.: Associated types with class. In: Palsberg, J., Abadi, M. (eds.), POPL ’05: Conference Record of the Annual ACM Symposium on Principles of Programming Languages, pp.1–13. ACM Press, New York (2005)

    Chapter  Google Scholar 

  8. Cunha A., Pinto, J.S., Proença, J.: A framework for point-free program transformation. In: Butterfield, A., Grelck, C., Huch, F. (eds.), Revised selected papers from IFL 2005: Implementation and application of functional languages, pp.1–18. Lecture Notes in Computer Science 4015, Springer, Berlin (2006)

    Google Scholar 

  9. Danvy, O.: Functional unparsing. J. Funct. Program.8(6), 621–625 (1998)

    Article  MATH  Google Scholar 

  10. Danvy, O., Nielsen, L.R.: Defunctionalization at work. In: Proceedings of the 3rd international conference on principles and practice of declarative programming, pp.162–174. ACM Press, New York (2001)

    Google Scholar 

  11. Diatchki, I.S., Jones, M.P.: Strongly typed memory areas: programming systems-level data structures in a functional language. In: Proceedings of the 2006 Haskell Workshop. ACM Press, New York (2006)

    Google Scholar 

  12. Elliott, C.: Elegant memoization with functional memo tries. http://conal.net/blog/posts/elegant-memoization-with-fun%ctional-memo-tries (2008)

    Google Scholar 

  13. Fluet, M., Pucella, R.: Practical datatype specializations with phantom types and recursion schemes. In: Proceedings of the 2005 Workshop on ML. Electronic Notes in Theoretical Computer Science (2005)

    Google Scholar 

  14. Fluet, M., Pucella, R.: Phantom types and subtyping. J. Funct. Program.16(6), 751–791 (2006)

    Article  MATH  MathSciNet  Google Scholar 

  15. Garcia, R., Järvi, J., Lumsdaine, A., Siek, J., Willcock, J.: An extended comparative study of language support for generic programming. J. Funct. Program.17(2), 145–205 (2007)

    Article  MATH  Google Scholar 

  16. Gill, A. (ed.): Proceedings of the 1st ACM SIGPLAN symposium on Haskell. ACM Press, New York (2008)

    Google Scholar 

  17. Girard, J.-Y., Taylor, P., Lafont, Y.: Proofs and types. Cambridge University Press, Cambridge (1989)

    MATH  Google Scholar 

  18. Guillemette, L.-J., Monnier, S.: A type-preserving compiler in Haskell. In [25], 75–90 (2008)

    Google Scholar 

  19. Hinze, R.: Generalizing generalized tries. J. Funct. Program.10(4), 327–351 (2000)

    Article  MATH  MathSciNet  Google Scholar 

  20. Hinze, R.: Formatting: a class act. J. Funct. Program.13(5), 935–944 (2003)

    Article  MATH  Google Scholar 

  21. Hinze, R.: Fun with phantom types. In: Gibbons, J., de Moor, O. (eds.), The fun of programming, pp.245–262. Palgrave (2003)

    Google Scholar 

  22. Hinze, R., Jeuring, J., Löh, A.: Type-indexed data types. In: Proceedings of the Sixth International Conference on Mathematics of Program Construction (MPC 2002), pp.148–174. Lecture Notes in Computer Science 2386, Springer Verlag (2002)

    Google Scholar 

  23. Howard, W.A.: The formulae-as-types notion of construction. In: Seldin, J.P., Hindley, J.R. (eds.), To H. B. Curry: Essays on Combinatory Logic, Lambda Calculus and Formalism, pp.479–490. Academic Press, San Diego, CA (1980).

    Google Scholar 

  24. Hutton, T.: Fun with type functions.http://www.haskell.org/pipermail/haskell-cafe/2008-Novem%ber/051105.html(2008)

    Google Scholar 

  25. Hook, J., Thiemann, P. ICFP ’08: Proc. ACM international conference on functional programming. ACM Press, New York (2008)

    Book  Google Scholar 

  26. Imai, K., Yuen, S., Agusa, K.: A full implementation of session types in Haskell. In: PPL2009: 11th Programming and Programming Languages Workshop. http://www.agusa.i.is.nagoya-u.ac.jp/person/sydney/fulls%ession-ppl2009/20090224/imai-ppl2009-submitted1.pdf (2009)

    Google Scholar 

  27. Ingram, R.: Fun with type functions.http://www.haskell.org/pipermail/haskell-cafe/2008-Novem%ber/051108.html(2008)

    Google Scholar 

  28. Jones, M.P.: Languages and program design for functional dependencies. In [16], 87–98 (2008)

    Google Scholar 

  29. Jones, M.P.: Type classes with functional dependencies. In: Programming Languages and Systems: Proceedings of ESOP 2000, 9th European Symposium on Programming, Smolka, G. (ed.), pp.230–244. Lecture Notes in Computer Science 1782, Springer, Berlin (2000)

    Chapter  Google Scholar 

  30. Kennedy, A.: Programming languages and dimensions. Ph.D. thesis, University of Cambridge (1995)

    Google Scholar 

  31. Kiselyov, O.: Formatted IO as an embedded DSL: the initial view. http://okmij.org/ftp/typed-formatting/\#DSL-In (2008)

    Google Scholar 

  32. Kiselyov, O., Shan, C.-c.: Lightweight static resources: sexy types for embedded and systems programming. In: Morazán, M.T., Nilsson, H. (eds.), Draft Proceedings of TFP 2007: 6th Symposium on Trends in Functional Programming. Tech. Rep. TR-SHU-CS-2007-04-1, Department of Mathematics and Computer Science, Seton Hall University (2007)

    Google Scholar 

  33. Krishnamurthi, S., Felleisen, M., Friedman, D.P.: Synthesizing object-oriented and functional design to promote re-use. In: Jul, E. (ed.), Proceedings of ECOOP’98: 12th European Conference on Object-oriented Programming, pp.91–113. Lecture Notes in Computer Science 1445, Springer, Berlin (1998)

    Google Scholar 

  34. Meyer, A.R., Wand, M.: Continuation semantics in typed lambda-calculi (summary). In: Logics of programs, Parikh, R. (ed.), pp.219–224. Lecture Notes in Computer Science 193, Springer, Berlin (1985)

    Chapter  Google Scholar 

  35. Michie, D.: “Memo” functions and machine learning. Nature218:19–22 (1968)

    Article  Google Scholar 

  36. Neubauer, M., Thiemann, P.: An implementation of session types. In: Practical Aspects of Declarative Languages: 6th International Symposium, PADL 2004, Jayaraman, B. (ed.), pp.56–70. Lecture Notes in Computer Science 3057, Springer, Berlin (2004)

    Chapter  Google Scholar 

  37. Peyton Jones, S.L., Vytiniotis, D., Weirich, S., Washburn, G.A.: Simple unification-based type inference for GADTs. In: ICFP ’06: Proc. ACM international conference on functional programming, pp.50–61. ACM Press, New York (2006)

    Chapter  Google Scholar 

  38. Pucella, R., Tov, J.: Haskell session types with (almost) no class. In [16], 25–36 (2008)

    Google Scholar 

  39. Reynolds, J.C.: Definitional interpreters for higher-order programming languages. In: Proceedings of the ACM National Conference, vol.2, pp.717–740. ACM Press, New York. Reprinted as [40] (1972)

    Google Scholar 

  40. Reynolds, J.C.: Definitional interpreters for higher-order programming languages. Higher-Order and Symbolic Computation11(4), 363–397 (1998)

    Article  MATH  Google Scholar 

  41. Sackman, M.: A tutorial for session types.http://www.wellquite.org/sessions/tutorial_1.html(2008)

    Google Scholar 

  42. Schrijvers, T., Peyton Jones, S.L., Chakravarty, M.M.T., Sulzmann, M.: Type checking with open type functions. In: [25], 51–62 (2008)

    Google Scholar 

  43. Sheard, T.: Languages of the future. Onward Track, OOPSLA’04. Reprinted in: ACM SIGPLAN Notices, Dec. 2004.39:116–119. OOPSLA Companion Volume (2004)

    Google Scholar 

  44. Sheard, T.: Generic programming inΩ. In: Backhouse, R., Gibbons, J., Hinze, R., Jeuring, J. (eds.), Datatype-generic programming, vol. 4719 of Lecture Notes in Computer Science, pp.258–284. Springer (2006)

    Chapter  Google Scholar 

  45. Sheard, T., Linger, N.: Programming inΩ. In: Horváth, Z., Plasmeijer, R., Soós, A., Zsók,V. (eds.), 2nd Central European Functional Programming School, vol. 5161 of Lecture Notes in Computer Science, pp.158–227. Springer (2007)

    Google Scholar 

  46. Sheard, T., Pasalic, E.: Meta-programming with built-in type equality. In: Proceedings of the fourth international workshop on logical frameworks and meta-languages (LFM’04) (2004)

    Google Scholar 

  47. Siek, J., Lee, L.-Q., Lumsdaine, A.: The Boost Graph Library User Guide and Reference Manual. Addison-Wesley (2001)

    Google Scholar 

Download references

Acknowledgements

We would like to thank people who responded to our invitation to suggest interesting examples of programming with type families, or commented on a draft of the chapter: Lennart Augustsson, Neil Brown, Toby Hutton, Ryan Ingram, Chris Kuklewicz, Dave Menendez, Benjamin Moseley, Hugh Pacheco, Conrad Parker, Bernie Pope, Tom Schrijvers, Josef Svenningsson, Paulo Tanimoto, Magnus Therning, Ashley Yakeley and Brent Yorgey.

Author information

Authors and Affiliations

Authors

Corresponding author

Correspondence to Simon Peyton Jones .

Editor information

Editors and Affiliations

Rights and permissions

Reprints and permissions

Copyright information

© 2010 Springer London

About this chapter

Cite this chapter

Kiselyov, O., Jones, S.P., Shan, Cc. (2010). Fun with Type Functions. In: Roscoe, A., Jones, C., Wood, K. (eds) Reflections on the Work of C.A.R. Hoare. Springer, London. https://doi.org/10.1007/978-1-84882-912-1_14

Download citation

  • DOI: https://doi.org/10.1007/978-1-84882-912-1_14

  • Published:

  • Publisher Name: Springer, London

  • Print ISBN: 978-1-84882-911-4

  • Online ISBN: 978-1-84882-912-1

  • eBook Packages: Computer ScienceComputer Science (R0)

Publish with us

Policies and ethics