Double Applicative Functors
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.
KeywordsApplicative functors Monads Parsing Error handling
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.
- 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.Aho, A.V., Ullman, J.D.: The Theory of Parsing, Translation, and Compiling. 1: Parsing. Prentice-Hall, Englewood Cliffs (1972)Google Scholar
- 3.Bifunctors and biapplicatives. https://github.com/purescript/purescript-bifunctors
- 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.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.Haskell. https://www.haskell.org
- 9.Haskell hierarchical libraries. https://downloads.haskell.org/~ghc/latest/docs/html/libraries/index.html
- 10.Hinze, R.: Lifting operators and laws (2010). https://www.cs.ox.ac.uk/ralf.hinze/Lifting.pdf
- 12.Kmett, E.: Biapplicative bifunctors. https://hackage.haskell.org/package/bifunctors-22.214.171.124/docs/Data-Biapplicative.html
- 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.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
- 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.Moggi, E.: An abstract view of programming languages. Technical report, ECS-LFCS-90-113, University of Edinburgh (1990)Google Scholar
- 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