Skip to main content

A Reflection on Types

  • Chapter
  • First Online:
A List of Successes That Can Change the World

Part of the book series: Lecture Notes in Computer Science ((LNTCS,volume 9600))

Abstract

The ability to perform type tests at runtime blurs the line between statically-typed and dynamically-checked languages. Recent developments in Haskell’s type system allow even programs that use reflection to themselves be statically typed, using a type-indexed runtime representation of types called \(\textsf {\textit{TypeRep}}\). As a result we can build dynamic types as an ordinary, statically-typed library, on top of \(\textsf {\textit{TypeRep}}\) in an open-world context.

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

Access this chapter

Chapter
USD 29.95
Price excludes VAT (USA)
  • Available as PDF
  • Read on any device
  • Instant download
  • Own it forever
eBook
USD 39.99
Price excludes VAT (USA)
  • Available as EPUB and PDF
  • Read on any device
  • Instant download
  • Own it forever
Softcover Book
USD 54.99
Price excludes VAT (USA)
  • Compact, lightweight 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

Institutional subscriptions

Notes

  1. 1.

    There is another connection with Phil’s work here: an API like this was first proposed in “Imperative functional programming” [PJW93], a collaboration between one of the present authors and Phil, directly inspired by Phil’s ground-breaking paper “Comprehending monads” [Wad90].

  2. 2.

    For our present purposes you can safely ignore the “\(\textsf {\textit{s}}\)” type parameter; the paper “State in Haskell” explains what is going on [LPJ95].

  3. 3.

    https://hackage.haskell.org/package/vault.

  4. 4.

    These types also motivate the \(\textsf {\textit{Typeable}}\) constraint above. We discuss that constraint further in Sect. 5.2, but without looking that far ahead, Phil’s insight about “theorems for free” tells us that the type \(\textsf {\textit{fromDynamic}}\mathbin {::}\textsf {\textit{Dynamic}}\rightarrow \textsf {\textit{Maybe}}\;\textsf {\textit{a}}\) is a non-starter [Wad89]. Any function with that type must return \(\textsf {\textit{Nothing}}\), \(\textsf {\textit{Just}}\;\bot \), or diverge.

  5. 5.

    Although the set of type constructors is fixed, you can use them to build an infinite number of types; e.g. \((\textsf {\textit{Int}},\textsf {\textit{Bool}})\), \((\textsf {\textit{Int}},(\textsf {\textit{Bool}},\textsf {\textit{Int}}))\), etc.

  6. 6.

    Recall that \(\bot \) is not a value.

  7. 7.

    Here we are using GHC’s ability to define infix type constructors.

  8. 8.

    The kind signatures are optional. With \(\textsf {\textit{PolyKinds}}\) enabled, GHC infers them, but we often add them for clarity.

  9. 9.

    Though, some Haskellers have hacked around this restriction with abandon. See Kiselyov and Shan [KS04] and Edward Kmett’s \(\textsf {\textit{reflection}}\) package (at http://hackage.haskell.org/package/reflection).

  10. 10.

    See also http://stackoverflow.com/q/32576018/791604 for another use case for a map keyed on type representations.

  11. 11.

    The dependent-map library is an example of such a data structure. See https://hackage.haskell.org/package/dependent-map-0.1.1.3/docs/Data-Dependent-Map.html.

  12. 12.

    See the tree of wiki pages rooted at https://ghc.haskell.org/trac/ghc/wiki/DistributedHaskell for lots more information.

References

  1. Abadi, M., Cardelli, L., Pierce, B., Plotkin, G.: Dynamic typing in a statically-typed language. ACM Trans. Program. Lang. Syst. 13(2), 237–268 (1991)

    Article  Google Scholar 

  2. Abadi, M., Cardelli, L., Pierce, B., Rémy, D.: Dynamic typing in polymorphic languages. J. Funct. Program. 5(1), 111–130 (1995)

    Article  MATH  Google Scholar 

  3. Bracha, G., Odersky, M., Stoutamire, D., Wadler, P.: Making the future safe for the past: adding genericity to the Java programming language. In: Conference on Object-oriented Programming, Systems, Languages, and Applications, pp. 183–200. ACM (1998)

    Google Scholar 

  4. Baars, A.I., Swierstra, D.: Typing dynamic typing. In: International Conference on Functional Programming, pp. 157–166. ACM (2002)

    Google Scholar 

  5. Cheney, J., Hinze, R.: A lightweight implementation of generics and dynamics. In: Workshop on Haskell, pp. 90–104. ACM (2002)

    Google Scholar 

  6. Epstein, J., Black, A.P., Peyton Jones, S.: Towards Haskell in the cloud. In: Haskell Symposium. ACM (2011)

    Google Scholar 

  7. Eisenberg, R.A., Weirich, S.: Dependently typed programming with singletons. In: Haskell Symposium. ACM (2012)

    Google Scholar 

  8. Frisch, A.: Runtime types in OCaml. In: Presentation at Meeting of the Caml Consortium, November 2011

    Google Scholar 

  9. Gibbons, J.: Datatype-generic programming. In: Backhouse, R., Gibbons, J., Hinze, R., Jeuring, J. (eds.) SSDGP 2006. LNCS, vol. 4719, pp. 1–71. Springer, Heidelberg (2007)

    Chapter  Google Scholar 

  10. Guillemettte, L.-J., Monnier, S.: A type-preserving compiler in Haskell. In: International Conference on Functional Programming. ACM (2008)

    Google Scholar 

  11. Henry, G., Garrique, J.: Dynamic typing in OCaml. Presentation at Nagoya University (2013)

    Google Scholar 

  12. Hudak, P., Hughes, J., Peyton Jones, S., Wadler, P.: A history of Haskell: being lazy with class. In: Conference on History of Programming Languages (2007)

    Google Scholar 

  13. Kiselyov, O., Shan, C.-C.: Functional pearl: implicit configurations-or, type classes reflect the values of types. In: Workshop on Haskell, pp. 33–44. ACM (2004)

    Google Scholar 

  14. Leroy, X., Mauny, M.: Dynamics in ML. In: Hughes, J. (ed.) FPCA 1991. LNCS, vol. 523, pp. 406–423. Springer, Heidelberg (1991)

    Chapter  Google Scholar 

  15. Launchbury, J., Peyton Jones, S.: State in Haskell. Lisp Symb. Comput. 8(4), 293–341 (1995)

    Article  Google Scholar 

  16. Lämmel, R., Peyton Jones, S.: Scrap your boilerplate: a practical design pattern for generic programming. In: Workshop on Types in Languages Design and Implementation. ACM (2003)

    Google Scholar 

  17. Lämmel, R., Peyton Jones, S.: Scrap your boilerplate with class: extensible generic functions. In: International Conference on Functional Programming. ACM (2005)

    Google Scholar 

  18. McBride, C.: Elimination with a motive. In: Callaghan, P., Luo, Z., McKinna, J., Pollack, R. (eds.) TYPES 2000. LNCS, vol. 2277, pp. 197–216. Springer, Heidelberg (2002)

    Chapter  Google Scholar 

  19. McDonell, T., Chakravarty, M., Grover, V., Newton, R.: Type-safe runtime code generation. In: Haskell Symposium, pp. 201–212. ACM (2015)

    Google Scholar 

  20. Magalhaes, J.P., Dijkstra, A., Jeuring, J., Loeh, A.: A generic deriving mechanism for Haskell. In: Haskell Symposium, pp. 37–48. ACM (2010)

    Google Scholar 

  21. Mitchell, N.: Shake before building: replacing Make with Haskell. In: International Conference on Functional Programming. ACM (2012)

    Google Scholar 

  22. Marlow, S., Peyton Jones, S., Moran, A., Reppy, J.: Asynchronous exceptions in Haskell. In: Programming Language Design and Implementation. ACM (2001)

    Google Scholar 

  23. Mitchell, N., Runciman, C.: Uniform boilerplate and list processing. In: Workshop on Haskell. ACM (2007)

    Google Scholar 

  24. Naftalin, M., Wadler, P.: Java Generics and Collections. O’Reilly Media, Sebastopol (2006)

    Google Scholar 

  25. Pil, M.: Dynamic types and type dependent functions. In: Hammond, K., Davie, T., Clack, C. (eds.) IFL 1998. LNCS, vol. 1595, pp. 169–185. Springer, Heidelberg (1999)

    Chapter  Google Scholar 

  26. Peyton Jones, S., Vytiniotis, D., Weirich, S., Washburn, G.: Simple unification-based type inference for GADTs. In: International Conference on Functional Programming, pp. 50–61. ACM (2006)

    Google Scholar 

  27. Peyton Jones, S.L., Wadler, P.: Imperative functional programming. In: Principles of Programming Languages. ACM (1993)

    Google Scholar 

  28. Pasalic, E., Taha, W., Sheard, T.: Tagless staged interpreters for typed languages. In: International Conference on Functional Programming. ACM (2002)

    Google Scholar 

  29. Sulzmann, M., Chakravarty, M.M.T., Peyton Jones, S., Donnelly, K.: System F with type equality coercions. In: Workshop on Types in Languages Design and Implementation. ACM (2007)

    Google Scholar 

  30. Vytiniotis, D., Weirich, S.: Parametricity, type equality, and higher-order polymorphism. J. Funct. Program. 20, 175–210 (2010)

    Article  MathSciNet  MATH  Google Scholar 

  31. Wadler, P.: Theorems for free! In: International Conference on Functional Programming Languages and Computer Architecture. ACM (1989)

    Google Scholar 

  32. Wadler, P.: Comprehending monads. In: Conference on LISP and Functional Programming. ACM (1990)

    Google Scholar 

  33. Wadler, P., Blott, S.: How to make ad-hoc polymorphism less ad-hoc. In: Principles of Programming Languages, pp. 60–76. ACM (1989)

    Google Scholar 

  34. Weirich, S.: Type-safe cast. J. Funct. Program. 14(6), 681–695 (2004)

    Article  MathSciNet  MATH  Google Scholar 

  35. Weirich, S.: Replib: a library for derivable type classes. In: Workshop on Haskell. ACM (2006)

    Google Scholar 

  36. Weirich, S., Hsu, J., Eisenberg, R.A.: System FC with explicit kind equality. In: International Conference on Functional Programming, pp. 275–286. ACM (2013)

    Google Scholar 

  37. Xi, H., Chen, C., Chen, G.: Guarded recursive datatype constructors. In: Principles of Programming Languages. ACM (2003)

    Google Scholar 

  38. Yang, Z.: Encoding types in ML-like languages. In: International Conference on Functional Programming, pp. 289–300. ACM (1998)

    Google Scholar 

  39. Yorgey, B.A., Weirich, S., Cretin, J., Peyton Jones, S., Vytiniotis, D., Magalha\(\tilde{\rm e}\)s, J.P.: Giving Haskell a promotion. In: Workshop on Types in Language Design and Implementation. ACM (2012)

    Google Scholar 

Download references

Acknowledgements

Thanks to Neil Mitchell both for help in understanding Shake and for a rapid review. Kenneth Foner also provided feedback on a draft. Ben Price’s internship at Microsoft Research was helpful in bringing some of the key issues to the surface. This paper was typeset with \(\textsf {\textit{lhs2TeX}}\) (http://www.andres-loeh.de/lhs2tex/).

Finally, thanks to Phil, who has spent much of his professional life reflecting on types. Increasingly, Haskell can too. Happy birthday Phil!

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

© 2016 Springer International Publishing Switzerland

About this chapter

Cite this chapter

Peyton Jones, S., Weirich, S., Eisenberg, R.A., Vytiniotis, D. (2016). A Reflection on Types. In: Lindley, S., McBride, C., Trinder, P., Sannella, D. (eds) A List of Successes That Can Change the World. Lecture Notes in Computer Science(), vol 9600. Springer, Cham. https://doi.org/10.1007/978-3-319-30936-1_16

Download citation

  • DOI: https://doi.org/10.1007/978-3-319-30936-1_16

  • Published:

  • Publisher Name: Springer, Cham

  • Print ISBN: 978-3-319-30935-4

  • Online ISBN: 978-3-319-30936-1

  • eBook Packages: Computer ScienceComputer Science (R0)

Publish with us

Policies and ethics