Typed Tagless Final Interpreters

  • Oleg Kiselyov
Part of the Lecture Notes in Computer Science book series (LNCS, volume 7470)

Abstract

The so-called ‘typed tagless final’ approach of [6] has collected and polished a number of techniques for representing typed higher-order languages in a typed metalanguage, along with type-preserving interpretation, compilation and partial evaluation. The approach is an alternative to the traditional, or ‘initial’ encoding of an object language as a (generalized) algebraic data type. Both approaches permit multiple interpretations of an expression, to evaluate it, pretty-print, etc. The final encoding represents all and only typed object terms without resorting to generalized algebraic data types, dependent or other fancy types. The final encoding lets us add new language forms and interpretations without breaking the existing terms and interpreters.

These lecture notes introduce the final approach slowly and in detail, highlighting extensibility, the solution to the expression problem, and the seemingly impossible pattern-matching. We develop the approach further, to type-safe cast, run-time-type representation, Dynamics, and type reconstruction. We finish with telling examples of type-directed partial evaluation and encodings of type-and-effect systems and linear lambda-calculus.

References

  1. 1.
    Asai, K.: On typing delimited continuations: Three new solutions to the printf problem. Higher-Order and Symbolic Computation 22(3), 275–291 (2009)MathSciNetMATHCrossRefGoogle Scholar
  2. 2.
    Atkey, R.: Syntax for Free: Representing Syntax with Binding Using Parametricity. In: Curien, P.-L. (ed.) TLCA 2009. LNCS, vol. 5608, pp. 35–49. Springer, Heidelberg (2009)CrossRefGoogle Scholar
  3. 3.
    Atkey, R., Lindley, S., Yallop, J.: Unembedding domain-specific languages. In: Weirich, S. (ed.) Proceedings of the 2nd ACM SIGPLAN Symposium on Haskell, pp. 37–48. ACM Press, New York (2009)CrossRefGoogle Scholar
  4. 4.
    Baars, A.I., Swierstra, S.D.: Typing dynamic typing. In: [21], pp. 157–166 (2002)Google Scholar
  5. 5.
    Böhm, C., Berarducci, A.: Automatic synthesis of typed Λ-programs on term algebras. Theoretical Computer Science 39, 135–154 (1985)MathSciNetMATHCrossRefGoogle Scholar
  6. 6.
    Carette, J., Kiselyov, O., Shan, C.-c.: Finally tagless, partially evaluated: Tagless staged interpreters for simpler typed languages. Journal of Functional Programming 19(5), 509–543 (2009)MathSciNetMATHCrossRefGoogle Scholar
  7. 7.
    Church, A.: A formulation of the simple theory of types. Journal of Symbolic Logic 5(2), 56–68 (1940)MathSciNetMATHCrossRefGoogle Scholar
  8. 8.
    Danvy, O.: Functional unparsing. Journal of Functional Programming 8(6), 621–625 (1998)MATHCrossRefGoogle Scholar
  9. 9.
    Danvy, O.: Lecture notes on type-directed partial evaluation (1999), http://www.brics.dk/~danvy/tdpe-ln.pdf
  10. 10.
    Danvy, O., Filinski, A.: Representing control: A study of the CPS transformation. Mathematical Structures in Computer Science 2(4), 361–391 (1992)MathSciNetMATHCrossRefGoogle Scholar
  11. 11.
  12. 12.
    Fischer, S., Kiselyov, O., Shan, C.-c.: Purely functional lazy non-deterministic programming. In: Hutton, G., Tolmach, A.P. (eds.) ICFP 2009: Proceedings of the ACM International Conference on Functional Programming, pp. 11–22. ACM Press, New York (2009)CrossRefGoogle Scholar
  13. 13.
    Garrigue, J.: Code reuse through polymorphic variants. In: Workshop on Foundations of Software Engineering (2000)Google Scholar
  14. 14.
    Gries, D. (ed.): Programming methodology: A collection of articles by members of IFIP WG 2.3. Springer, Berlin (1978)Google Scholar
  15. 15.
    Guillemette, L.-J., Monnier, S.: A typepreserving compiler in Haskell. In: Hook, J., Thiemann, P. (eds.) ICFP 2008: Proceedings of the ACM International Conference on Functional Programming. ACM SIGPLAN Notices, vol. 43(9), pp. 75–86. ACM Press, New York (2008)CrossRefGoogle Scholar
  16. 16.
    Gunter, C.A., Mitchell, J.C.: Theoretical aspects of object-oriented programming: Types, semantics, and language design. MIT Press, Cambridge (1994)MATHGoogle Scholar
  17. 17.
    Hall, C.V., Hammond, K., Peyton-Jones, S.L., Wadler, P.L.: Type classes in Haskell. ACM Transactions on Programming Languages and Systems 18(2), 109–138 (1996)CrossRefGoogle Scholar
  18. 18.
    Hinze, R.: Formatting: A class act. Journal of Functional Programming 13(5), 935–944 (2003)MATHCrossRefGoogle Scholar
  19. 19.
    Hofer, C., Ostermann, K., Rendel, T., Moors, A.: Polymorphic embedding of DSLs. In: Smaragdakis, Y., Siek, J.G. (eds.) Proceedings of GPCE 2008: 7th International Conference on Generative Programming and Component Engineering, pp. 137–147. ACM Press, New York (2008)CrossRefGoogle Scholar
  20. 20.
    Hudak, P.: Building domain-specific embedded languages. ACM Computing Surveys 28(4es), 196 (1996)Google Scholar
  21. 21.
    ICFP, ICFP 2002: Proceedings of the ACM International Conference on Functional Programming. ACM Press, New York (2002)Google Scholar
  22. 22.
    Johann, P., Ghani, N.: Foundations for structured programming with GADTs. In: Necula, G.C., Wadler, P. (eds.) POPL 2008: Conference Record of the Annual ACM Symposium on Principles of Programming Languages, pp. 297–308. ACM Press, New York (2008)CrossRefGoogle Scholar
  23. 23.
    Kamin, S.: Final data types and their specification. ACM Transactions on Programming Languages and Systems 5(1), 97–121 (1983)MATHCrossRefGoogle Scholar
  24. 24.
    Lämmel, R., Kiselyov, O.: Exploring typed language design in Haskell: Lecture 1. Haskell’s take on the Expression Problem (2010), http://userpages.uni-koblenz.de/~laemmel/TheEagle/
  25. 25.
    Lämmel, R., Kiselyov, O.: Lecture 2: Spin-offs from the Expression Problem (2010), http://userpages.uni-koblenz.de/~laemmel/TheEagle/resources/xproblem2.html
  26. 26.
    Magi, S.: Mobile code in C# via finally tagless interpreters (2009), http://higherlogics.blogspot.com/2009/06/mobile-code-in-c-via-finally-tagless.html
  27. 27.
    McAdam, B.J.: Y in practical programs. In: Workshop on Fixed Points in Computer Science (2001), http://www.dsi.uniroma1.it/~labella/absMcAdam.ps
  28. 28.
    Miller, D., Nadathur, G.: A logic programming approach to manipulating formulas and programs. In: Haridi, S. (ed.) IEEE Symposium on Logic Programming, pp. 379–388. IEEE Computer Society Press, Washington, DC (1987)Google Scholar
  29. 29.
    Mu, S.-C.: Typed λ-Calculus Interpreter in Agda (2008), http://www.iis.sinica.edu.tw/~scm/2008/typed-lambda-calculus-interprete/
  30. 30.
    Oliveira, B.C.d.S., Gibbons, J.: TypeCase: A design pattern for type-indexed functions. In: Proceedings of the 2005 Haskell Workshop, pp. 98–109. ACM Press, New York (2005)CrossRefGoogle Scholar
  31. 31.
    Pašalić, E., Taha, W., Sheard, T.: Tagless staged interpreters for typed languages. In: [21], pp. 157–166 (2002)Google Scholar
  32. 32.
    Pfenning, F., Elliott, C.: Higher-order abstract syntax. In: PLDI 1988: Proceedings of the ACM Conference on Programming Language Design and Implementation. ACM SIGPLAN Notices, vol. 23(7), pp. 199–208. ACM Press, New York (1988)CrossRefGoogle Scholar
  33. 33.
    Plotkin, G.D.: LCF considered as a programming language. Theoretical Computer Science 5, 223–255 (1977)MathSciNetCrossRefGoogle Scholar
  34. 34.
    Pogodalla, S.: Abstract Categorial Grammar homepage, http://www.loria.fr/equipes/calligramme/acg
  35. 35.
    Reynolds, J.C.: User-defined types and procedural data structures as complementary approaches to data abstraction. In: Schuman, S.A. (ed.) New Directions in Algorithmic Languages. IFIP Working Group 2.1 on Algol, Rocquencourt, pp. 157–168. INRIA, France (1975); Also in [14, 309–317], [16, 13–23]Google Scholar
  36. 36.
    Swierstra, W.: Data types á la carte. Journal of Functional Programming 18(4), 423–436 (2008)MathSciNetMATHCrossRefGoogle Scholar
  37. 37.
    Szabó, Z.G.: Compositionality. In: Zalta, E.N. (ed.) The Stanford Encyclopedia of Philosophy (Winter 2008)Google Scholar
  38. 38.
    Thiemann, P.: Combinators for program generation. Journal of Functional Programming 9(5), 483–525 (1999)MathSciNetMATHCrossRefGoogle Scholar
  39. 39.
    Thiemann, P., Sulzmann, M.: Tag-Free Combinators for Binding-Time Polymorphic Program Generation. In: Blume, M., Kobayashi, N., Vidal, G. (eds.) FLOPS 2010. LNCS, vol. 6009, pp. 87–102. Springer, Heidelberg (2010)CrossRefGoogle Scholar
  40. 40.
    Wadler, P.: The expression problem. Message to java-genericity electronic mailing list (1998), http://www.daimi.au.dk/~madst/tool/papers/expression.txt
  41. 41.
    Wand, M.: Final algebra semantics and data type extensions. Journal of Computer and System Sciences 19(1), 27–44 (1979)MathSciNetMATHCrossRefGoogle Scholar
  42. 42.
    Washburn, G.A., Weirich, S.: Boxes go bananas: Encoding higher-order abstract syntax with parametric polymorphism. Journal of Functional Programming 18(1), 87–140 (2008)MathSciNetMATHCrossRefGoogle Scholar
  43. 43.
    Weirich, S.: Encoding Intensional Type Analysis. In: Sands, D. (ed.) ESOP 2001. LNCS, vol. 2028, pp. 92–106. Springer, Heidelberg (2001)CrossRefGoogle Scholar
  44. 44.
    Weirich, S.: Typechecking into GADT (2004), http://www.comlab.ox.ac.uk/projects/gip/school/tc.hs
  45. 45.
    Weirich, S.: Type-safe run-time polytypic programming. Journal of Functional Programming 16(6), 751–791 (2006)MathSciNetCrossRefGoogle Scholar
  46. 46.
    Winskel, G.: Formal semantics of programming languages. MIT Press, Cambridge (1993)MATHGoogle Scholar
  47. 47.
    Xi, H., Chen, C., Chen, G.: Guarded recursive datatype constructors. In: POPL 2003: Conference Record of the Annual ACM Symposium on Principles of Programming Languages, pp. 224–235. ACM Press, New York (2003)Google Scholar
  48. 48.
    Xi, H., Pfenning, F.: Eliminating array bound checking through dependent types. In: PLDI 1998: Proceedings of the ACM Conference on Programming Language Design and Implementation. ACM SIGPLAN Notices, vol. 33(5), pp. 249–257. ACM Press, New York (1998)CrossRefGoogle Scholar
  49. 49.
    Yang, Z.: Encoding types in ML-like languages. In: ICFP 1998: Proceedings of the ACM International Conference on Functional Programming. ACM SIGPLAN Notices, vol. 34(1), pp. 289–300. ACM Press, New York (1998)CrossRefGoogle Scholar

Copyright information

© Springer-Verlag Berlin Heidelberg 2012

Authors and Affiliations

  • Oleg Kiselyov

There are no affiliations available

Personalised recommendations