Advertisement

Double Applicative Functors

  • Härmel Nestra
Conference paper
Part of the Lecture Notes in Computer Science book series (LNCS, volume 11187)

Abstract

Writing easily readable parser code is a classic application of monads in functional programming. For simpler cases, the Applicative and Alternative type classes in Haskell can be used for this purpose instead of the more powerful Monad and MonadPlus classes. Counterparts of all parsing expression grammar constructs except lookaheads are expressible via the Applicative and Alternative class methods. Yet their error handling capabilities are unsatisfactory even for simple applications. This paper proposes double applicative functors for increasing the flexibility of error handling without full monadic power, along with an extended set of operations, and studies relationships between mathematical laws that these operations are assumed to fulfill. Many properties of these operations are generalizations of semantic equivalences previously known for parsing expression grammars.

Keywords

Applicative functors Monads Parsing Error handling 

Notes

Acknowledgement

The work was partially supported by the Estonian Research Council under R&D project No. IUT2-1.

The author thanks Tarmo Uustalu for fruitful discussions and also the anonymous reviewers for valuable feedback.

References

  1. 1.
    Ahman, D., Uustalu, T.: Update monads: cointerpreting directed containers. In: Matthes, R., Schubert, A. (eds.) 19th International Conference on Types for Proofs and Programs, TYPES 2013. Leibniz International Proceedings in Informatics, Toulouse, April 2013, vol. 26, pp. 1–23. Dagstuhl Publishing, Saarbrücken/Wadern (2014).  https://doi.org/10.4230/lipics.types.2013.1
  2. 2.
    Aho, A.V., Ullman, J.D.: The Theory of Parsing, Translation, and Compiling. 1: Parsing. Prentice-Hall, Englewood Cliffs (1972)Google Scholar
  3. 3.
  4. 4.
    Birman, A., Ullman, J.D.: Parsing algorithms with backtrack. Inf. Control 23(1), 1–34 (1973).  https://doi.org/10.1016/s0019-9958(73)90851-6MathSciNetCrossRefzbMATHGoogle Scholar
  5. 5.
    Cockett, J.R.B., Lack, S.: Restriction categories III: colimits, partial limits and extensivity. Math. Struct. Comput. Sci. 17(4), 775–817 (2007).  https://doi.org/10.1017/s0960129507006056MathSciNetCrossRefzbMATHGoogle Scholar
  6. 6.
    Danielsson, N.A., Hughes, J., Jansson, P., Gibbons, J.: Fast and loose reasoning is morally correct. In: Proceedings of 33rd ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages, POPL 2006, Charleston, SC, pp. 206–217. ACM Press, New York (2006).  https://doi.org/10.1145/1111037.1111056
  7. 7.
    Ford, B.: Parsing expression grammars: a recognition-based syntactic foundation. In: Proceedings of 31st ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages, POPL 2004, Venice, January 2004, pp. 111–122. ACM Press, New York (2004).  https://doi.org/10.1145/964001.964011
  8. 8.
  9. 9.
  10. 10.
    Hinze, R.: Lifting operators and laws (2010). https://www.cs.ox.ac.uk/ralf.hinze/Lifting.pdf
  11. 11.
    King, D.J., Wadler, P.: Combining monads. In: Launchbury, J., Sansom, P.M. (eds.) Functional Programming, Glasgow 1992. Workshops in Computing, pp. 134–143. Springer, London (1993).  https://doi.org/10.1007/978-1-4471-3215-8_12CrossRefGoogle Scholar
  12. 12.
  13. 13.
    Liang, S., Hudak, P., Jones, M.P.: Monad transformers and modular interpreters. In: Conference Record of 22nd ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages, POPL 1995, San Francisco, CA, January 1995, pp. 333–343. ACM Press, New York (1995).  https://doi.org/10.1145/199448.199528
  14. 14.
    Lindley, S.: Algebraic effects and effect handlers for idioms and arrows. In: Proceedings of 10th ACM SIGPLAN Workshop on Generic Programming, WGP 2014, Gothenburg, August 2014, pp. 47–58. ACM Press, New York (2014).  https://doi.org/10.1145/2633628.2633636
  15. 15.
    Lindley, S., Wadler, P., Yallop, J.: Idioms are oblivious, arrows are meticulous, monads are promiscuous. Electron. Notes Theor. Comput. Sci. 229(5), 97–117 (2011).  https://doi.org/10.1016/j.entcs.2011.02.018MathSciNetCrossRefzbMATHGoogle Scholar
  16. 16.
    Maidl, A.M., Mascarenhas, F., Medeiros, S., Ierusalimschy, R.: Error reporting in parsing expression grammars. Sci. Comput. Program. 132, 129–140 (2016).  https://doi.org/10.1016/j.scico.2016.08.004CrossRefzbMATHGoogle Scholar
  17. 17.
    McBride, C., Paterson, R.: Applicative programming with effects. J. Funct. Program. 18(1), 1–13 (2008).  https://doi.org/10.1017/s0956796807006326CrossRefzbMATHGoogle Scholar
  18. 18.
    Mizushima, K., Maeda, A., Yamaguchi, Y.: Packrat parsers can handle practical grammars in mostly constant space. In: Proceedings of 9th ACM SIGPLAN-SIGSOFT Workshop on Program Analysis for Software Tools and Engineering, PASTE 2010, Toronto, ON, June 2010, pp. 29–36. ACM Press, New York (2010).  https://doi.org/10.1145/1806672.1806679
  19. 19.
    Moggi, E.: An abstract view of programming languages. Technical report, ECS-LFCS-90-113, University of Edinburgh (1990)Google Scholar
  20. 20.
    Röjemo, N.: Highlights from nhc–a space-efficient Haskell compiler. In: Proceedings of 7th International Conference on Functional Programming Languages and Computer Architecture, FPCA 1995, La Jolla, CA, June 1995, pp. 282–292. ACM Press (1995).  https://doi.org/10.1145/224164.224217
  21. 21.
    Swierstra, S.D.: Combinator parsing: a short tutorial. In: Bove, A., Barbosa, L.S., Pardo, A., Pinto, J.S. (eds.) LerNet 2008. LNCS, vol. 5520, pp. 252–300. Springer, Heidelberg (2009).  https://doi.org/10.1007/978-3-642-03153-3_6CrossRefGoogle Scholar
  22. 22.
    Swierstra, S.D., Duponcheel, L.: Deterministic, error-correcting combinator parsers. In: Launchbury, J., Meijer, E., Sheard, T. (eds.) AFP 1996. LNCS, vol. 1129, pp. 184–207. Springer, Heidelberg (1996).  https://doi.org/10.1007/3-540-61628-4_7CrossRefGoogle Scholar
  23. 23.
    Uustalu, T., Veltri, N.: The delay monad and restriction categories. In: Hung, D.V., Kapur, D. (eds.) ICTAC 2017. LNCS, vol. 10580, pp. 32–50. Springer, Cham (2017).  https://doi.org/10.1007/978-3-319-67729-3_3CrossRefGoogle Scholar
  24. 24.
    Uustalu, T., Veltri, N.: Partiality and container monads. In: Chang, B.-Y.E. (ed.) APLAS 2017. LNCS, vol. 10695, pp. 406–425. Springer, Cham (2017).  https://doi.org/10.1007/978-3-319-71237-6_20CrossRefzbMATHGoogle Scholar
  25. 25.
    Wadler, P.: Comprehending monads. Math. Struct. Comput. Sci. 2(4), 461–493 (1992).  https://doi.org/10.1017/s0960129500001560MathSciNetCrossRefzbMATHGoogle Scholar

Copyright information

© Springer Nature Switzerland AG 2018

Authors and Affiliations

  1. 1.University of Tartu, Institute of Computer ScienceTartuEstonia

Personalised recommendations