Abstract
Many useful programming constructions can be expressed as monads. Examples include probabilistic modeling, functional reactive programming, parsing, and information flow tracking, not to mention effectful functionality like state and I/O. In this paper, we present a type-based rewriting algorithm to make programming with arbitrary monads as easy as using ML's built-in support for state and I/O. Developers write programs using monadic values of type m τ as if they were of type τ, and our algorithm inserts the necessary binds, units, and monad-to-monad morphisms so that the program type checks. Our algorithm, based on Jones' qualified types, produces principal types. But principal types are sometimes problematic: the program's semantics could depend on the choice of instantiation when more than one instantiation is valid. In such situations we are able to simplify the types to remove any ambiguity but without adversely affecting typability; thus we can accept strictly more programs. Moreover, we have proved that this simplification is efficient (linear in the number of constraints) and coherent: while our algorithm induces a particular rewriting, all related rewritings will have the same semantics. We have implemented our approach for a core functional language and applied it successfully to simple examples from the domains listed above, which are used as illustrations throughout the paper.
Supplemental Material
- M. Abadi, A. Banerjee, N. Heintze, and J. G. Riecke. A core calculus of dependency. In POPL, volume 26, pages 147--160, 1999. Google Scholar
Digital Library
- Nick Benton and Andrew Kennedy. Monads, effects and transformations. In Electronic Notes in Theoretical Computer Science, 1999.Google Scholar
Cross Ref
- Greg Cooper and Shriram Krishnamurthi. Embedding dynamic dataflow in a call-by-value language. In ESOP, 2006. Google Scholar
Digital Library
- K. Crary, A. Kliger, and F. Pfenning. A monadic analysis of information flow security with mutable state. Journal of functional programming, 15(02):249--291, 2005. Google Scholar
Digital Library
- L. Damas and R. Milner. Principal type-schemes for functional programs. In POPL, pages 207--212, 1982. Google Scholar
Digital Library
- D. E. Denning. A lattice model of secure information flow. Communications of the ACM, 19(5):236--243, 1976. Google Scholar
Digital Library
- D. Devriese and F. Piessens. Information flow enforcement in monadic libraries. In TLDI, pages 59--72, 2011. Google Scholar
Digital Library
- Conal Elliott and Paul Hudak. Functional reactive animation. In ICFP, pages 263---273, 1997. Google Scholar
Digital Library
- A. Filinski. Representing layered monads. In POPL, pages 175--188, 1999. Google Scholar
Digital Library
- A. Filinski. Monads in action. In POPL, pages 483--494, 2010. Google Scholar
Digital Library
- Andrzej Filinski. Representing monads. In POPL, 1994. Google Scholar
Digital Library
- J. A. Goguen and J. Meseguer. Security policy and security models. In Symposium on Security and Privacy, pages 11--20, 1982.Google Scholar
Cross Ref
- Graham Hutton and Erik Meijer. Monadic Parsing in Haskell. JFP, 8(4), 1998. Google Scholar
Digital Library
- Mark P. Jones. A theory of qualified types. In ESOP, 1992. Google Scholar
Digital Library
- Mark P. Jones. Coherence for qualified types. Technical Report YALEU/DCS/RR-989, Yale University, September 1993.Google Scholar
- Mark P. Jones. Simplifying and Improving Qualified Types. Technical Report YALEU/DCS/RR-1040, Yale University, June 1994.Google Scholar
- Mark P. Jones and Luc Duponcheel. Composing monads. Technical Report YALEU/DCS/RR-1004, Yale University, 1993.Google Scholar
- Oleg Kiselyov and Chung chieh Shan. Embedded probabilistic programming. In DSL, 2009. Google Scholar
Digital Library
- P. Li and S. Zdancewic. Encoding information flow in Haskell. In CSFW, pages 16--27, 2006. Google Scholar
Digital Library
- Z. Luo. Coercions in a polymorphic type system. MSCS, 18(4), 2008. Google Scholar
Digital Library
- Z. Luo and R. Kießling. Coercions in Hindley-Milner systems. In Proc. of Types, 2004.Google Scholar
- Eugenio Moggi. Computational lambda-calculus and monads. In LICS, 1989. Google Scholar
Digital Library
- Judea Pearl. Embracing causality in default reasoning (research note). Artificial Intelligence, 35(2):259---271, 1988. Google Scholar
Digital Library
- F. Pottier and V. Simonet. Information flow inference for ML. TOPLAS, 25(1):117--158, 2003. Google Scholar
Digital Library
- Norman Ramsey and Avi Pfeffer. Stochastic lambda calculus and monads of probability distributions. In POPL, pages 154--165, 2002. Google Scholar
Digital Library
- Alejandro Russo, Koen Claessen, and John Hughes. A library for light-weight information-flow security in haskell. In Haskell, 2008. Google Scholar
Digital Library
- Nikhil Swamy, Nataliya Guts, Daan Leijen, and Michael Hicks. Lightweight monadic programming in ML. Technical Report MSR-TR-2011-039, Microsoft Research, May 2011.Google Scholar
- Nikhil Swamy, Michael Hicks, and Gavin M. Bierman. A theory of typed coercions and its applications. In ICFP, 2009. Google Scholar
Digital Library
- Philip Wadler. The essence of functional programming. In POPL, 1992. Google Scholar
Digital Library
Index Terms
Lightweight monadic programming in ML
Recommendations
Lightweight monadic programming in ML
ICFP '11: Proceedings of the 16th ACM SIGPLAN international conference on Functional programmingMany useful programming constructions can be expressed as monads. Examples include probabilistic modeling, functional reactive programming, parsing, and information flow tracking, not to mention effectful functionality like state and I/O. In this paper, ...
On the power of coercion abstraction
POPL '12Erasable coercions in System F-eta, also known as retyping functions, are well-typed eta-expansions of the identity. They may change the type of terms without changing their behavior and can thus be erased before reduction. Coercions in F-eta can model ...
On the power of coercion abstraction
POPL '12: Proceedings of the 39th annual ACM SIGPLAN-SIGACT symposium on Principles of programming languagesErasable coercions in System F-eta, also known as retyping functions, are well-typed eta-expansions of the identity. They may change the type of terms without changing their behavior and can thus be erased before reduction. Coercions in F-eta can model ...







Comments