The F# Computation Expression Zoo

  • Tomas Petricek
  • Don Syme
Part of the Lecture Notes in Computer Science book series (LNCS, volume 8324)

Abstract

Program logic can often be structured using abstract computation types such as monoids, monad transformers or applicative functors. Functional programmers use those abstractions directly while main-stream languages often integrate concrete instances as language features – e.g. generators in Python or asynchronous computations in C# 5.0. The question is, is there a sweet spot between convenient, hardwired language features, and an inconvenient but flexible libraries?

F# computation expressions answer this question in the affirmative. Unlike the “do” notation in Haskell, computation expressions are not tied to a single kind of abstraction. They support a wide range of abstractions, depending on what operations are available. F# also provides greater syntactic flexibility leading to a more intuitive syntax, without resorting to full macro-based meta-programming.

We present computation expressions in a principled way, developing a type system that captures the semantics of the calculus. We demonstrate how computation expressions structure well-known abstractions including monoidal list comprehensions, monadic parsers, applicative formlets and asynchronous sequences based on monad transformers.

Preview

Unable to display preview. Download preview PDF.

Unable to display preview. Download preview PDF.

References

  1. 1.
    Bierman, G., Russo, C., Mainland, G., Meijer, E., Torgersen, M.: Pause ’n’ play: formalizing asynchronous C\(^\sharp\). In: Noble, J. (ed.) ECOOP 2012. LNCS, vol. 7313, pp. 233–257. Springer, Heidelberg (2012)CrossRefGoogle Scholar
  2. 2.
    Cooper, E., Lindley, S., Wadler, P., Yallop, J.: The essence of form abstraction. In: Ramalingam, G. (ed.) APLAS 2008. LNCS, vol. 5356, pp. 205–220. Springer, Heidelberg (2008)CrossRefGoogle Scholar
  3. 3.
    Filinski, A.: Representing layered monads. In: POPL, pp. 175–188 (1999)Google Scholar
  4. 4.
    Filinski, A.: Monads in action. In: POPL, pp. 483–494 (2010)Google Scholar
  5. 5.
    Giorgidze, G., Grust, T., Schweinsberg, N., Weijers, J.: Bringing back monad comprehensions. In: Haskell Symposium, pp. 13–22 (2011)Google Scholar
  6. 6.
    Harris, T., Marlow, S., Peyton-Jones, S., Herlihy, M.: Composable memory transactions. In: PPoPP, pp. 48–60 (2005)Google Scholar
  7. 7.
    HaskellWiki. Listt done right (2012), http://www.haskell.org/haskellwiki/ListT_done_right
  8. 8.
    HaskellWiki. Monadplus (2012), http://www.haskell.org/haskellwiki/MonadPlus
  9. 9.
    Krishnamurthi, S.: Artifact evaluation for software conferences (2012), http://cs.brown.edu/~sk/Memos/Conference-Artifact-Evaluation/
  10. 10.
    Liang, S., Hudak, P., Jones, M.: Monad transformers and modular interpreters. In: POPL (1995)Google Scholar
  11. 11.
    Lindley, S., Wadler, P., Yallop, J.: Idioms are oblivious, arrows are meticulous, monads are promiscuous. Electron. Notes Theor. Comput. Sci. 229(5) (March 2011)Google Scholar
  12. 12.
    Lüth, C., Ghani, N.: Proceedings of the Seventh ACM SIGPLAN International Conference on Functional Programming, ICFP, pp. 133–144 (2002)Google Scholar
  13. 13.
    Mcbride, C., Paterson, R.: Applicative programming with effects. J. Funct. Program. 18(1), 1–13 (2008)CrossRefMATHGoogle Scholar
  14. 14.
    Meijer, E., Beckman, B., Bierman, G.: LINQ: reconciling object, relations and XML in the.NET framework. In: SIGMOD, p. 706 (2006)Google Scholar
  15. 15.
    Paterson, R.: A new notation for arrows. In: ICFP (2001)Google Scholar
  16. 16.
    Petricek, T.: Programming with F# asynchronous sequences (2011), http://tomasp.net/blog/async-sequences.aspx
  17. 17.
    Plotkin, G., Pretnar, M.: Handlers of algebraic effects. In: Castagna, G. (ed.) ESOP 2009. LNCS, vol. 5502, pp. 80–94. Springer, Heidelberg (2009)CrossRefGoogle Scholar
  18. 18.
    Rompf, T., Maier, I., Odersky, M.: Implementing first-class polymorphic delimited continuations by a type-directed selective cps-transform. In: ICFP (2009)Google Scholar
  19. 19.
    Syme, D., Petricek, T., Lomov, D.: The F# asynchronous programming model. In: Rocha, R., Launchbury, J. (eds.) PADL 2011. LNCS, vol. 6539, pp. 175–189. Springer, Heidelberg (2011)CrossRefGoogle Scholar
  20. 20.
    The F# Software Foundation. F# language specification (2013)Google Scholar
  21. 21.
    Typesafe Inc. An asynchronous programming facility for scala (2013)Google Scholar
  22. 22.
    Wadler, P.: Monads for functional programming. In: Jeuring, J., Meijer, E. (eds.) AFP 1995. LNCS, vol. 925, pp. 24–52. Springer, Heidelberg (1995)CrossRefGoogle Scholar

Copyright information

© Springer International Publishing Switzerland 2014

Authors and Affiliations

  • Tomas Petricek
    • 1
  • Don Syme
    • 2
  1. 1.University of CambridgeUK
  2. 2.Microsoft Research CambridgeUK

Personalised recommendations