Advertisement

J Is for JavaScript: A Direct-Style Correspondence between Algol-Like Languages and JavaScript Using First-Class Continuations

  • Olivier Danvy
  • Chung-chieh Shan
  • Ian Zerny
Part of the Lecture Notes in Computer Science book series (LNCS, volume 5658)

Abstract

It is a time-honored fashion to implement a domain-specific language (DSL) by translation to a general-purpose language. Such an implementation is more portable, but an unidiomatic translation jeopardizes performance because, in practice, language implementations favor the common cases. This tension arises especially when the domain calls for complex control structures. We illustrate this tension by revisiting Landin’s original correspondence between Algol and Church’s lambda-notation.

We translate domain-specific programs with lexically scoped jumps to JavaScript. Our translation produces the same block structure and binding structure as in the source program, à la Abdali. The target code uses a control operator in direct style, à la Landin. In fact, the control operator used is almost Landin’s J—hence our title. Our translation thus complements a continuation-passing translation à la Steele. These two extreme translations require JavaScript implementations to cater either for first-class continuations, as Rhino does, or for proper tail recursion. Less extreme translations should emit more idiomatic control-flow instructions such as for, break, and throw.

The present experiment leads us to conclude that translations should preserve not just the data structures and the block structure of a source program, but also its control structure. We thus identify a new class of use cases for control structures in JavaScript, namely the idiomatic translation of control structures from DSLs.

Keywords

Return Function Source Program Partial Evaluator Return Continuation Direct Style 
These keywords were added by machine and not by the authors. This process is experimental and the keywords may be updated as the learning algorithm improves.

References

  1. 1.
    Kamal Abdali, S.: A lambda-calculus model of programming languages, part I: Simple constructs. Computer Languages 1(4), 287–301 (1976)CrossRefzbMATHGoogle Scholar
  2. 2.
    Kamal Abdali, S.: A lambda-calculus model of programming languages, part II: Jumps and procedures. Computer Languages 1(4), 303–320 (1976)CrossRefGoogle Scholar
  3. 3.
    Kamal Abdali, S., Wise, D.S.: Standard, storeless semantics for ALGOL-style block structure and call-by-name. In: Melton, A.C. (ed.) MFPS 1985. LNCS, vol. 239, pp. 1–19. Springer, Heidelberg (1986)CrossRefGoogle Scholar
  4. 4.
    Bawden, A.: Reification without evaluation. In: (Corky) Cartwright, R. (ed.) Proceedings of the 1988 ACM Conference on Lisp and Functional Programming, Snowbird, Utah, July 1988, pp. 342–351. ACM Press, New York (1988)CrossRefGoogle Scholar
  5. 5.
    Biernacki, D., Danvy, O., Shan, C.-c.: On the static and dynamic extents of delimited continuations. Science of Computer Programming 60(3), 274–297 (2006)MathSciNetCrossRefzbMATHGoogle Scholar
  6. 6.
    Bjørner, D., Jones, C.B.: Formal Specification & Software Development. Prentice-Hall International, London (1982)zbMATHGoogle Scholar
  7. 7.
    Böhm, C., Jacopini, G.: Flow diagrams, Turing machines and languages with only two formation rules. Communications of the ACM 9(5), 366–371 (1966)CrossRefzbMATHGoogle Scholar
  8. 8.
    Burge, W.H.: Recursive Programming Techniques. Addison-Wesley, Reading (1975)zbMATHGoogle Scholar
  9. 9.
    Burstall, R.M.: Writing search algorithms in functional form. In: Michie, D. (ed.) Machine Intelligence, vol. 5, pp. 373–385. Edinburgh University Press (1969)Google Scholar
  10. 10.
    Clements, J., Sundaram, A., Herman, D.: Implementing continuation marks in JavaScript. In: Clinger, W. (ed.) Proceedings of the 2008 ACM SIGPLAN Workshop on Scheme and Functional Programming, Victoria, British Columbia, September 2008, pp. 1–9 (2008)Google Scholar
  11. 11.
    Consel, C., Danvy, O.: Partial evaluation of pattern matching in strings. Information Processing Letters 30(2), 79–86 (1989)CrossRefGoogle Scholar
  12. 12.
    Conway, M.E.: Design of a separable transition-diagram compiler. Communications of the ACM 6(7), 396–408 (1963)CrossRefzbMATHGoogle Scholar
  13. 13.
    Danvy, O.: Sur un exemple de Patrick Greussay. Research Report BRICS RS-04-41, DAIMI, Department of Computer Science, Aarhus University, Aarhus, Denmark (December 2004)Google Scholar
  14. 14.
    Danvy, O., Millikin, K.: A rational deconstruction of Landin’s SECD machine with the J operator. Logical Methods in Computer Science 4(4:12), 1–67 (2008)MathSciNetzbMATHGoogle Scholar
  15. 15.
    Kent Dybvig, R., Hieb, R.: Engines from continuations. Computer Languages 14(2), 109–123 (1989)CrossRefGoogle Scholar
  16. 16.
    Fischer, M.J.: Lambda-calculus schemata. LISP and Symbolic Computation 6(3/4), 259–288 (1993), http://www.brics.dk/~hosc/vol06/03-fischer.html; A preliminary version was presented at the ACM Conference on Proving Assertions about Programs, SIGPLAN Notices 7(1) (January 1972)CrossRefGoogle Scholar
  17. 17.
    Flanagan, D., Matsumoto, Y.: The Ruby Programming Language. O’Reilly Media, Inc., Sebastopol (2008)Google Scholar
  18. 18.
    Ganz, S.E., Friedman, D.P., Wand, M.: Trampolined style. In: Lee, P. (ed.) Proceedings of the 1999 ACM SIGPLAN International Conference on Functional Programming, SIGPLAN Notices, Paris, France, vol. 34(9), pp. 18–27. ACM Press, New York (1999)Google Scholar
  19. 19.
    Girard, J.-Y.: Locus solum. Mathematical Structures in Computer Science 11(3), 301–506 (2001)MathSciNetCrossRefzbMATHGoogle Scholar
  20. 20.
    Hewitt, C., Bishop, P., Steiger, R., Greif, I., Smith, B., Matson, T., Hale, R.: Behavioral semantics of nonrecursive control structures. In: Robinet, B. (ed.) Programming Symposium. LNCS, vol. 19, pp. 385–407. Springer, Heidelberg (1974)CrossRefGoogle Scholar
  21. 21.
    Jones, N.D., Gomard, C.K., Sestoft, P.: Partial Evaluation and Automatic Program Generation. Prentice-Hall International, London (1993), http://www.dina.kvl.dk/~sestoft/pebook/ zbMATHGoogle Scholar
  22. 22.
    Knuth, D.E.: Man or boy? ALGOL Bulletin 17, 7 (1964)Google Scholar
  23. 23.
    Knuth, D.E.: Structured programming with go to statements. Computing Surveys 6(4), 261–301 (1974)CrossRefzbMATHGoogle Scholar
  24. 24.
    Knuth, D.E., Morris, J.H., Pratt, V.R.: Fast pattern matching in strings. SIAM Journal on Computing 6(2), 323–350 (1977)MathSciNetCrossRefzbMATHGoogle Scholar
  25. 25.
    Kranz, D., Kesley, R., Rees, J., Hudak, P., Philbin, J., Adams, N.: Orbit: An optimizing compiler for Scheme. In: Proceedings of the ACM SIGPLAN 1986 Symposium on Compiler Construction, Palo Alto, California, pp. 219–233. ACM Press, New York (1986)CrossRefGoogle Scholar
  26. 26.
    Kranz, D.A.: ORBIT: An Optimizing Compiler for Scheme. PhD thesis, Computer Science Department, Yale University, New Haven, Connecticut (February 1988), Research Report 632Google Scholar
  27. 27.
    Landin, P.J.: A correspondence between Algol 60 and Church’s lambda notation, Parts 1 and 2. Communications of the ACM 8, 89–101, 158–165 (1965)MathSciNetCrossRefzbMATHGoogle Scholar
  28. 28.
    Landin, P.J.: The next 700 programming languages. Communications of the ACM 9(3), 157–166 (1966)CrossRefzbMATHGoogle Scholar
  29. 29.
    Landin, P.J.: Histories of discoveries of continuations: Belles-lettres with equivocal tenses. In: Danvy, O. (ed.) Proceedings of the Second ACM SIGPLAN Workshop on Continuations, CW 1997 (1997); Technical report BRICS NS-96-13, Aarhus University, pp. 1:1–9, Paris, France (January 1997) Google Scholar
  30. 30.
    Loitsch, F.: Scheme to JavaScript Compilation. PhD thesis, Université de Nice, Nice, France (March 2009)Google Scholar
  31. 31.
    Mazurkiewicz, A.W.: Proving algorithms by tail functions. Information and Control 18, 220–226 (1971)MathSciNetCrossRefzbMATHGoogle Scholar
  32. 32.
    McCarthy, J.: Recursive functions of symbolic expressions and their computation by machine, part I. Communications of the ACM 3(4), 184–195 (1960)CrossRefzbMATHGoogle Scholar
  33. 33.
    McCarthy, J.: Towards a mathematical science of computation. In: Popplewell, C.M. (ed.) Information Processing 1962, Proceedings of IFIP Congress 62, pp. 21–28. North-Holland, Amsterdam (1962)Google Scholar
  34. 34.
    McCarthy, J.: A formal description of a subset of ALGOL. In: Steel, T.B. (ed.) Formal Language Description Languages for Computer Programming, pp. 1–12. North-Holland, Amsterdam (1966)Google Scholar
  35. 35.
    McCarthy, J.: Another samefringe. SIGART Newsletter 61 (February 1977)Google Scholar
  36. 36.
    McDermott, D.: An efficient environment allocation scheme in an interpreter for a lexically-scoped Lisp. In: Davis, R.E., Allen, J.R. (eds.) Conference Record of the 1980 LISP Conference, Stanford, California, August 1980, pp. 154–162 (1980)Google Scholar
  37. 37.
    Minamide, Y.: Selective tail call elimination. In: Cousot, R. (ed.) SAS 2003. LNCS, vol. 2694, pp. 153–170. Springer, Heidelberg (2003)CrossRefGoogle Scholar
  38. 38.
    Moggi, E.: Notions of computation and monads. Information and Computation 93, 55–92 (1991)MathSciNetCrossRefzbMATHGoogle Scholar
  39. 39.
    Lockwood Morris, F.: Correctness of Translations of Programming Languages – an Algebraic Approach. PhD thesis, Computer Science Department, Stanford University (August 1972), Technical report STAN-CS-72-303Google Scholar
  40. 40.
    Lockwood Morris, F.: The next 700 formal language descriptions. Lisp and Symbolic Computation 6(3/4), 249–258 (1993); Reprinted from a manuscript dated (1970)CrossRefGoogle Scholar
  41. 41.
    Reynolds, J.C.: Definitional interpreters for higher-order programming languages. In: Proceedings of 25th ACM National Conference, Boston, Massachusetts, pp. 717–740 (1972); Reprinted in Higher-Order and Symbolic Computation 11(4), 363–397 (1998), with a foreword [42] Google Scholar
  42. 42.
    Reynolds, J.C.: Definitional interpreters revisited. Higher-Order and Symbolic Computation 11(4), 355–361 (1998)CrossRefzbMATHGoogle Scholar
  43. 43.
    Schinz, M., Odersky, M.: Tail call elimination on the Java Virtual Machine. In: Benton, N., Kennedy, A. (eds.) BABEL 2001: First International Workshop on Multi-Language Infrastructure and Interoperability, Firenze, Italy, September 2001. Electronic Notes in Theoretical Computer Science, vol. (59), pp. 155–168. Elsevier Science, Amsterdam (2001)Google Scholar
  44. 44.
    Stallman, R.M.: Phantom stacks: If you look too hard, they aren’t there. In: AI Memo 556, Artificial Intelligence Laboratory, Massachusetts Institute of Technology, Cambridge, Massachusetts (July 1980)Google Scholar
  45. 45.
    Steele Jr., G.L.: Rabbit: A compiler for Scheme. Master’s thesis, Artificial Intelligence Laboratory. Massachusetts Institute of Technology, Cambridge, Massachusetts, Technical report AI-TR-474 (May 1978)Google Scholar
  46. 46.
    Talcott, C.L.: The Essence of \(\cal R\)um: A Theory of the Intensional and Extensional Aspects of Lisp-type Computation. PhD thesis, Department of Computer Science, Stanford University, Stanford, California (August 1985)Google Scholar
  47. 47.
    Tarditi, D., Lee, P., Acharya, A.: No assembly required: Compiling Standard ML to C. ACM Letters on Programming Languages and Systems 1(2), 161–177 (1992)CrossRefGoogle Scholar
  48. 48.
    Thielecke, H.: Comparing control constructs by double-barrelled CPS. Higher-Order and Symbolic Computation 15(2/3), 141–160 (2002)CrossRefzbMATHGoogle Scholar
  49. 49.
    Wadler, P.: The essence of functional programming (invited talk). In: Appel, A.W. (ed.) Proceedings of the Nineteenth Annual ACM Symposium on Principles of Programming Languages, Albuquerque, New Mexico, pp. 1–14. ACM Press, New York (1992)Google Scholar

Copyright information

© IFIP International Federation for Information Processing 2009

Authors and Affiliations

  • Olivier Danvy
    • 1
  • Chung-chieh Shan
    • 2
  • Ian Zerny
    • 1
  1. 1.Department of Computer ScienceAarhus UniversityAarhus NDenmark
  2. 2.Department of Computer ScienceRutgers UniversityPiscatawayUSA

Personalised recommendations