Skip to main content
Log in

Functional un|unparsing

  • Published:
Higher-Order and Symbolic Computation

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).

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

Access this article

Price excludes VAT (USA)
Tax calculation will be finalised during checkout.

Instant access to the full article PDF.

Fig. 1
Fig. 2
Fig. 3
Fig. 4
Fig. 5
Fig. 6
Fig. 7
Fig. 8
Fig. 9

Similar content being viewed by others

References

  1. Asai, K.: On typing delimited continuations: three new solutions to the printf problem. Higher-Order Symb. Comput. 22(3), 275–291 (2009)

    Article  MathSciNet  MATH  Google Scholar 

  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. 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)

    Article  MathSciNet  MATH  Google Scholar 

  4. Danvy, O.: Functional unparsing. J. Funct. Program. 8(6), 621–625 (1998)

    Article  MATH  Google Scholar 

  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. 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. 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)

    Chapter  Google Scholar 

  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

    Chapter  Google Scholar 

  9. Friedman, D.P., Wand, M.: Essentials of Programming Languages, 3rd edn. MIT Press, Cambridge (2008)

    MATH  Google Scholar 

  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. 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. 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)

    Chapter  Google Scholar 

  13. Hinze, R.: Formatting: a class act. J. Funct. Program. 13(5), 935–944 (2003)

    Article  MATH  Google Scholar 

  14. Hudak, P.: Building domain-specific embedded languages. ACM Comput. Surv. 28(4es), 196 (1996)

    Article  Google Scholar 

  15. Hutton, G.: A tutorial on the universality and expressiveness of fold. J. Funct. Program. 9(4), 355–372 (1999)

    Article  MathSciNet  MATH  Google Scholar 

  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)

    Chapter  Google Scholar 

  17. Kamin, S.: Final data types and their specification. ACM Trans. Program. Lang. Syst. 5(1), 97–121 (1983)

    Article  MATH  Google Scholar 

  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)

    Chapter  Google Scholar 

  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. 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)

    Chapter  Google Scholar 

  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)

    Chapter  Google Scholar 

  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)

    Chapter  Google Scholar 

  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)

    Chapter  Google Scholar 

  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)

    Chapter  Google Scholar 

  25. Sabry, A.: What is a purely functional language? J. Funct. Program. 8(1), 1–22 (1998)

    Article  MathSciNet  MATH  Google Scholar 

  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. 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)

    Chapter  Google Scholar 

  28. Wadler, P.L.: Deforestation: transforming programs to eliminate trees. Theor. Comput. Sci. 73(2), 231–248 (1990)

    Article  MathSciNet  MATH  Google Scholar 

  29. Wand, M.: Final algebra semantics and data type extensions. J. Comput. Syst. Sci. 19(1), 27–44 (1979)

    Article  MathSciNet  MATH  Google Scholar 

  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)

    Chapter  Google Scholar 

  31. Wand, M.: Continuation-based program transformation strategies. J. ACM 27(1), 164–180 (1980)

    Article  MathSciNet  MATH  Google Scholar 

  32. Wand, M.: Deriving target code as a representation of continuation semantics. ACM Transactions on Programming Languages and Systems 4(3), 496–517 (1982)

    Article  MATH  Google Scholar 

  33. Wand, M.: Specifications, models, and implementations of data abstractions. Theoretical Computer Science 20(1), 3–32 (1982)

    Article  MathSciNet  MATH  Google Scholar 

  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)

    Chapter  Google Scholar 

  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)

    Chapter  Google Scholar 

Download references

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.

Author information

Authors and Affiliations

Authors

Corresponding author

Correspondence to Kenichi Asai.

Rights and permissions

Reprints and permissions

About this article

Cite this article

Asai, K., Kiselyov, O. & Shan, Cc. Functional un|unparsing. Higher-Order Symb Comput 24, 311–340 (2011). https://doi.org/10.1007/s10990-012-9087-2

Download citation

  • Published:

  • Issue Date:

  • DOI: https://doi.org/10.1007/s10990-012-9087-2

Keywords

Navigation