Advertisement

Higher-Order and Symbolic Computation

, Volume 24, Issue 4, pp 311–340 | Cite as

Functional un|unparsing

Article
  • 182 Downloads

Abstract

Danvy’s functional unparsing problem (Danvy in J. Funct. Program. 8(6), 621–625, 1998) is to implement a type-safe ‘printf’ function, which converts a sequence of heterogeneous arguments to a string according to a given format. The dual problem is to implement a type-safe ‘scanf’ function, which extracts a sequence of heterogeneous arguments from a string by interpreting (Friedman and Wand in LFP, pp. 348–355, 1984 and in Essentials of Programming Languages, MIT Press, 2008) the same format as an equally heterogeneous sequence of patterns that binds zero or more variables. We derive multiple solutions to both problems (Wand in J. ACM 27(1), 164–180, 1980) from their formal specifications (Wand in Theor. Comput. Sci. 20(1), 3–32, 1982).

On one hand, our solutions show how the Hindley-Milner type system, unextended, permits accessing heterogeneous sequences with the static assurance of type safety. On the other hand, our solutions demonstrate the use of control operators (Felleisen et al. in Proceedings of the 1988 ACM Conference on Lisp and Functional Programming, pp. 52–62, ACM Press, New York, 1988; Wand in POPL 85: Conference Record of the Annual ACM Symposium on Principles of Programming Languages, vol. 16, ACM Press, New York, 1985; Meyer and Wand in Logics of Programs, Lecture Notes in Computer Science, vol. 193, pp. 219–224, Springer, Berlin, 1985) to communicate with formats as coroutines (Wand in Proceedings of the 1980 ACM Conference on Lisp and Functional Programming, vol. 12, pp. 285–299, ACM Press, New York, 1980 and Haynes et al. in LFP, pp. 293–298, 1984).

Keywords

Typed printf Typed scanf Program derivation Functional programming Continuations Delimited control 

Notes

Acknowledgements

We would like to thank Yukiyoshi Kameyama and Mitch Wand for helpful discussions, and Olivier Danvy for many comments and encouragement. Many improvements to the presentation by the anonymous reviewers are gratefully acknowledged.

References

  1. 1.
    Asai, K.: On typing delimited continuations: three new solutions to the printf problem. Higher-Order Symb. Comput. 22(3), 275–291 (2009) MathSciNetMATHCrossRefGoogle Scholar
  2. 2.
    Asai, K., Kameyama, Y.: Polymorphic delimited continuations. In: Shao, Z. (ed.) Proceedings of APLAS 2007: 5th Asian Symposium on Programming Languages and Systems. Lecture Notes in Computer Science, vol. 4807, pp. 239–254. Springer, Berlin (2007) Google Scholar
  3. 3.
    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) MathSciNetMATHCrossRefGoogle Scholar
  4. 4.
    Danvy, O.: Functional unparsing. J. Funct. Program. 8(6), 621–625 (1998) MATHCrossRefGoogle Scholar
  5. 5.
    Danvy, O., Filinski, A.: A functional abstraction of typed contexts. Tech. Rep. 89/12, DIKU, University of Copenhagen, Denmark (1989). http://www.cs.au.dk/~danvy/Papers/fatc.ps.gz
  6. 6.
    Dybvig, R.K., Peyton Jones, S.L., Sabry, A.: A monadic framework for delimited continuations. J. Funct. Program. 17(6), 687–730 (2007) Google Scholar
  7. 7.
    Felleisen, M., Wand, M., Friedman, D.P., Duba, B.F.: Abstract continuations: a mathematical semantics for handling full jumps. In: Proceedings of the 1988 ACM Conference on Lisp and Functional Programming, pp. 52–62. ACM Press, New York (1988) CrossRefGoogle Scholar
  8. 8.
    Friedman, D.P., Wand, M.: Reification: reflection without metaphysics. In: Proceedings of the 1984 ACM Symposium on Lisp and Functional Programming, pp. 348–355. ACM Press, New York (1984). 1984 CrossRefGoogle Scholar
  9. 9.
    Friedman, D.P., Wand, M.: Essentials of Programming Languages, 3rd edn. MIT Press, Cambridge (2008) MATHGoogle Scholar
  10. 10.
    Goguen, J.A., Thatcher, J.W., Wagner, E.G.: An initial algebra approach to the specification, correctness and implementation of abstract data types. In: Yeh, R.T. (ed.) Current Trends in Programming Methodology, vol. 4, pp. 80–149. Prentice-Hall, Englewood Cliffs (1978) Google Scholar
  11. 11.
    Gunter, C.A., Rémy, D., Riecke, J.G.: A generalization of exceptions and control in ML-like languages. In: Peyton Jones, S.L. (ed.) Functional Programming Languages and Computer Architecture: 7th Conference, pp. 12–23. ACM Press, New York (1995) Google Scholar
  12. 12.
    Haynes, C.T., Friedman, D.P., Wand, M.: Continuations and coroutines. In: Proceedings of the 1984 ACM Symposium on Lisp and Functional Programming, pp. 293–298. ACM Press, New York (1984) CrossRefGoogle Scholar
  13. 13.
    Hinze, R.: Formatting: a class act. J. Funct. Program. 13(5), 935–944 (2003) MATHCrossRefGoogle Scholar
  14. 14.
    Hudak, P.: Building domain-specific embedded languages. ACM Comput. Surv. 28(4es), 196 (1996) CrossRefGoogle Scholar
  15. 15.
    Hutton, G.: A tutorial on the universality and expressiveness of fold. J. Funct. Program. 9(4), 355–372 (1999) MathSciNetMATHCrossRefGoogle Scholar
  16. 16.
    Kameyama, Y., Hasegawa, M.: A sound and complete axiomatization of delimited continuations. In: ICFP 03: Proceedings of the ACM International Conference on Functional Programming, pp. 177–188. ACM Press, New York (2003) CrossRefGoogle Scholar
  17. 17.
    Kamin, S.: Final data types and their specification. ACM Trans. Program. Lang. Syst. 5(1), 97–121 (1983) MATHCrossRefGoogle Scholar
  18. 18.
    Kay, M.: Syntactic processing and functional sentence perspective. In: TINLAP75: Proceedings of the 1975 Workshop on Theoretical Issues in Natural Language Processing, pp. 12–15. Association for Computational Linguistics, Stroudsburg (1975) CrossRefGoogle Scholar
  19. 19.
    Kiselyov, O.: Delimited control in OCaml, abstractly and concretely: system description. In: Proc. FLOPS 2010: 10th International Symposium on Functional and Logic Programming. Lecture Notes in Computer Science, vol. 6009, pp. 304–320. Springer, Berlin (2010) Google Scholar
  20. 20.
    Kiselyov, O., Shan, C.-c., Sabry, A.: Delimited dynamic binding. In: ICFP 06: Proceedings of the ACM International Conference on Functional Programming, pp. 26–37. ACM Press, New York (2006) CrossRefGoogle Scholar
  21. 21.
    Kohlbecker, E.E., Wand, M.: Macro-by-example: deriving syntactic transformations from their specifications. In: POPL 87: Conference Record of the Annual ACM Symposium on Principles of Programming Languages, pp. 77–84. ACM Press, New York (1987) CrossRefGoogle Scholar
  22. 22.
    Masuko, M., Asai, K.: Direct implementation of shift and reset in the MinCaml compiler. In: ACM SIGPLAN Workshop on ML, pp. 49–60. ACM Press, New York (2009) CrossRefGoogle Scholar
  23. 23.
    Meyer, A.R., Wand, M.: Continuation semantics in typed lambdacalculi (summary). In: Parikh, R. (ed.) Logics of Programs. Lecture Notes in Computer Science, vol. 193, pp. 219–224. Springer, Berlin (1985) CrossRefGoogle Scholar
  24. 24.
    Rendel, T., Ostermann, K.: Invertible syntax descriptions: unifying parsing and pretty printing. In: Gibbons, J. (ed.) Proceedings of the 3rd ACM SIGPLAN Symposium on Haskell, pp. 1–12. ACM Press, New York (2010) CrossRefGoogle Scholar
  25. 25.
    Sabry, A.: What is a purely functional language? J. Funct. Program. 8(1), 1–22 (1998) MathSciNetMATHCrossRefGoogle Scholar
  26. 26.
    Shieber, S.M.: A uniform architecture for parsing and generation. In: COLING88: Proceedings of the 12th International Conference on Computational Linguistics, vol. 2, pp. 614–619 (1988) Google Scholar
  27. 27.
    Swierstra, S.D.: Combinator parsers: a short tutorial. In: Bove, A., Barbosa, L., Pardo, A., Sousa Pinto, J. (eds.) Language Engineering and Rigorous Software Development. Lecture Notes in Computer Science, vol. 5520, pp. 252–300. Springer, Berlin (2009) CrossRefGoogle Scholar
  28. 28.
    Wadler, P.L.: Deforestation: transforming programs to eliminate trees. Theor. Comput. Sci. 73(2), 231–248 (1990) MathSciNetMATHCrossRefGoogle Scholar
  29. 29.
    Wand, M.: Final algebra semantics and data type extensions. J. Comput. Syst. Sci. 19(1), 27–44 (1979) MathSciNetMATHCrossRefGoogle Scholar
  30. 30.
    Wand, M.: Continuation-based multiprocessing. In: Proceedings of the 1980 ACM Conference on Lisp and Functional Programming, pp. 19–28. ACM Press, New York (1980). Reprinted in Higher-Order Symb. Comput. 12(3), 285–299 (1999) CrossRefGoogle Scholar
  31. 31.
    Wand, M.: Continuation-based program transformation strategies. J. ACM 27(1), 164–180 (1980) MathSciNetMATHCrossRefGoogle Scholar
  32. 32.
    Wand, M.: Deriving target code as a representation of continuation semantics. ACM Transactions on Programming Languages and Systems 4(3), 496–517 (1982) MATHCrossRefGoogle Scholar
  33. 33.
    Wand, M.: Specifications, models, and implementations of data abstractions. Theoretical Computer Science 20(1), 3–32 (1982) MathSciNetMATHCrossRefGoogle Scholar
  34. 34.
    Wand, M.: Embedding type structure in semantics. In: POPL 85: Conference record of the annual ACM symposium on principles of programming languages, pp. 1–6. ACM Press, New York (1985) CrossRefGoogle Scholar
  35. 35.
    Xi, H., Chen, C., Chen, G.: Guarded recursive datatype constructors. In: POPL 03: Conference Record of the Annual ACM Symposium on Principles of Programming Languages, pp. 224–235. ACM Press, New York (2003) CrossRefGoogle Scholar

Copyright information

© Springer Science+Business Media New York 2012

Authors and Affiliations

  1. 1.Ochanomizu UniversityTokyoJapan
  2. 2.MontereyUSA
  3. 3.Indiana UniversityBloomingtonUSA

Personalised recommendations