Java as a Functional Programming Language

  • Anton Setzer
Conference paper
Part of the Lecture Notes in Computer Science book series (LNCS, volume 2646)

Abstract

We introduce a direct encoding of the typed λ-calculus into Java: for any Java types A, B we introduce the type A → B together with function application and λ-abstraction. The latter makes use of anonymous inner classes. We show that λ-terms are evaluated by call-by-value. We then look at how to model domain equations in Java and as an example consider the untyped lambda calculus. Then we investigate the use of function parameters in order to control overriding and in order to dynamically update methods, which can substitute certain applications of the state pattern. Further we introduce a foreach-loop in collection classes. Finally we introduce algebraic types. Elements of the resulting type are given by their elimination rules. Algebraic types with infinitely many arguments like Kleene’s O and simultaneous algebraic types are already contained in that notion. Further we introduce an operation selfupdate, which allows to modify for instance a subtree of a tree, without making a copy of the original tree. All the above constructions are direct and can be done by hand.

Keywords

Lambda calculus functional programming Java object-oriented programming object calculi higher types algebraic types initial algebras call-by-value visitor pattern state pattern 

Preview

Unable to display preview. Download preview PDF.

Unable to display preview. Download preview PDF.

References

  1. [AC94]
    M. Abadi and L. Cardelli. A semantics of object types. In Proceedings of the 9th Symposium on Logic in Computer Science, pages 332–341, 1994.Google Scholar
  2. [AC96a]
    M. Abadi and L. Cardelli. A theory of primitive objects: Untyped and first order system. Information and Computation, 125(2):78–102, 1996.MATHCrossRefMathSciNetGoogle Scholar
  3. [AC96b]
    M. Abadi and Luca Cardelli, editors. A Theory of Objects. Springer, Berlin, Heidelberg, New York, 1996.MATHGoogle Scholar
  4. [Alb]
    Ben Albahari. A comparative overview of C#. http://genamics.com/developer/csharp_comparative.htm.
  5. [B+01]
    Gilad Bracha et al. Adding generics to the Java programming language: Participant draft specification. http://jcp.org/aboutJava/communityprocess/review/jsr014/index.html, 2001.
  6. [Bir98]
    Richard Bird. Introduction to functional programming using Haskell. Pearson Education, Harlow, second edition, 1998.Google Scholar
  7. [BOSW98]
    Gilad Bracha, Martin Odersky, David Stoutamire, and Philip Wadler. Making the future safe for the past: Adding genericity to the Java programming language. ACMSIGPLAN Notices, 33(10):183–200, 1998.CrossRefGoogle Scholar
  8. [BS91]
    U. Berger and H. Schwichtenberg. An inverse of the evaluation functional for typed lambda-calculus. In R. Vemuri, editor, Proceedings of the Sixth Annual IEEE Symposium on Logic in Computer Science (LICS), pages 203–211. IEEE Computer Society Press, 1991.Google Scholar
  9. [Dom99]
    Mark-Jason Dominus. Pure untyped lambda-calculus and popular programming languages. J. Funct. Progr., pages 1–7, 1999.Google Scholar
  10. [GHJV95]
    Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides. Design Patterns. Elements of reusable object-oriented software. Addison-Wesley, 1995.Google Scholar
  11. [GJSB00]
    J. Gosling, B. Joy, G. Steele, and G. Brache. The Java Language Specification. Addison-Wesley, second edition, 2000.Google Scholar
  12. [IP00]
    Atsushi Igarashi and Benjamin C. Pierce. On inner classes. In European Conference on Object-Oriented Programming (ECOOP), volume 1850 of Springer Lecture Notes in Computer Science, pages 129–153, 2000.Google Scholar
  13. [Jav02]
    The Java Language Team. About Microsoft’s delegates. http://java.sun.com/docs/white/delegates.html, 2002.
  14. [JP]
    Jaakko Järvi and Gary Powell. The lambda library. Available from http://lambda.cs.utu.fi and http://www.boost.org/libs/lambda/doc/.
  15. [KFF98]
    S. Krishnamurthi, M. Felleisen, and D. Friedman. Synthesizing object-oriented and functional design to promote re-use. In European Conference on Object-Oriented Programming, pages 91–113, 1998.Google Scholar
  16. [Kis98]
    Ole Kiselyov. Functional style in c++: Closures, late binding, and lambda abstractions. A poster presentation at the 1998 International Conference on Functional Programming (ICFP’98), available from http://okmij.org/ftp/c++-digest/Functional-Cpp.html, 1998.
  17. [Lan66]
    P. J. Landin. The next 700 programming languages. Communications of the ACM, 9(3):157–164, March 1966. Originally presented at the Proceedings of the ACM Programming Language and Pragmatics Conference, August 8–12, 1965.MATHCrossRefGoogle Scholar
  18. [Läu95]
    K. Läufer. A framework for higher-order functions in C++. In Proc. Conf. Object-Oriented Technologies (COOTS). USENIX, 1995.Google Scholar
  19. [Mic02]
    Microsoft Corporation. The truth about delegates. http://msdn.microsoft.com/visualj/technical/articles/delegates/truth.asp, 2002.
  20. [MTHM97]
    Robin Milner, Mads Tofte, Robert Harper, and David MacQueen. The definition of Standard ML (revised). MIT Press, Cambridge, Massachusetts and London, 1997.Google Scholar
  21. [Oka98]
    Chris Okasaki. Purely functional data structures. Cambridge University Press, Cambridge, 1998.Google Scholar
  22. [OW97]
    Martin Odersky and Philip Wadler. Pizza into Java: translating theory into practice. In Conference record of POPL’ 97, pages 146–159, New York, NY 10036, USA, 1997. ACM Press.Google Scholar
  23. [PJ98]
    J. Palsberg and B. Jay. The essence of the visitor pattern. In Proc. 22nd IEEE Int. Computer Software and Applications Conf, COMPSAC, pages 9–15, 1998.Google Scholar
  24. [Sho67]
    J. Shoenfield. Mathematical Logic. Addison-Wesley, Reading, Massachusetts, 1967.MATHGoogle Scholar
  25. [SS00]
    Jörg Striegnitz and Stephen A. Smith. An expression template aware lambda function. In Proceedings of the 2000 Workshop on C++ Template Programming, 2000. available from http://www.fz-juelich.de/zam/docs/autoren2001/striegnitz.html.
  26. [SSB01]
    R. Stärk, J. Schmid, and E. Börger. Java and the Java Virtual Machine-Definition, Verification, Validation. Springer, 2001.Google Scholar
  27. [Str97]
    Bjarne Stroustrup. The C++ Programming Language. Addison-Wesley, third edition, 1997.Google Scholar
  28. [Sun97]
  29. [Tho96]
    Simon Thompson. Haskell. The craft of functional programming. Addison-Wesley, 1996.Google Scholar
  30. [Tro73]
    A. Troelstra, editor. Metamathematical investigations of intuitionistic arithmetic and analysis, volume 344 of Springer Lecture Notes in Mathematics. Springer, Berlin, Heidelberg, New York, 1973.Google Scholar
  31. [Vel95]
    Todd L. Veldhuizen. Expression templates. C++ Report, 7(5):26–31, June 1995.Google Scholar
  32. [Vel99]
    Todd L. Veldhuizen. C++ templates as partial evaluation. In Proceedings of the ACM SIGPLAN Workshop on Partial Evaluation and Semantics-Based Program Manipulation, Tech. Report NS-99-1, pages 13–18. BRICS, 1999.Google Scholar
  33. [VJ03]
    David Vandevoorde and Nicolai M. Josuttis. C++ Templates. The complete guide. Addison-Wesley, 2003.Google Scholar
  34. [Win98]
    Glynn Winskel. The formal semantics of programming languages. MIT Press, Cambridge, Massachusetts and London, 1998.Google Scholar
  35. [ZO01]
    Matthias Zenger and Martin Odersky. Extensible algebraic datatypes with defaults. In Proceedings of ICFP’ 01, SIGPLAN Notices 36(10), pages 241–252, 2001.CrossRefGoogle Scholar

Copyright information

© Springer-Verlag Berlin Heidelberg 2003

Authors and Affiliations

  • Anton Setzer
    • 1
  1. 1.Dept. of Computing ScienceUniversity of Wales SwanseaSwanseaUK

Personalised recommendations