skip to main content
10.1145/1411286.1411288acmconferencesArticle/Chapter ViewAbstractPublication PagesicfpConference Proceedingsconference-collections
research-article

Lightweight monadic regions

Published:25 September 2008Publication History

ABSTRACT

We present Haskell libraries that statically ensure the safe use of resources such as file handles. We statically prevent accessing an already closed handle or forgetting to close it. The libraries can be trivially extended to other resources such as database connections and graphic contexts.

Because file handles and similar resources are scarce, we want to not just assure their safe use but further deallocate them soon after they are no longer needed. Relying on Fluet and Morrisett's [4] calculus of nested regions, we contribute a novel, improved, and extended implementation of the calculus in Haskell, with file handles as resources.

Our library supports region polymorphism and implicit region subtyping, along with higher-order functions, mutable state, recursion, and run-time exceptions. A program may allocate arbitrarily many resources and dispose of them in any order, not necessarily LIFO. Region annotations are part of an expression's inferred type.

Our new Haskell encoding of monadic regions as monad transformers needs no witness terms. It assures timely deallocation even when resources have markedly different lifetimes and the identity of the longest-living resource is determined only dynamically.

For contrast, we also implement a Haskell library for manual resource management, where deallocation is explicit and safety is assured by a form of linear types. We implement the linear typing in Haskell with the help of phantom types and a parameterized monad to statically track the type-state of resources.

Skip Supplemental Material Section

Supplemental Material

Video

References

  1. Atkey, Robert. 2006. Parameterised notions of computation. In MSFP 2006: Workshop on mathematically structured functional programming, ed. Conor McBride and Tarmo Uustalu. Electronic Workshops in Computing, British Computer Soci ety. Google ScholarGoogle ScholarDigital LibraryDigital Library
  2. Filinski, Andrzej. 1999. Representing layered monads. In POPL '99: Conference record of the annual ACM symposium on principles of programming languages, 175--188. New York: ACM Press. Google ScholarGoogle ScholarDigital LibraryDigital Library
  3. Fluet, Matthew, Greg Morrisett, and Amal J. Ahmed. 2006. Linear regions are all you need. In ESOP, 7--21. Google ScholarGoogle ScholarDigital LibraryDigital Library
  4. Fluet, Matthew, and J. Gregory Morrisett. 2004. Monadic regions. In ICFP '04: Proceedings of the ACM international conference on functional programming. New York: ACM Press. Google ScholarGoogle ScholarDigital LibraryDigital Library
  5. Grossman, Dan, Greg Morrisett, Trevor Jim, Michael Hicks, Yanling Wang, and James Cheney. Region-based memory management in Cyclone. In PLDI, 282--293. Google ScholarGoogle ScholarDigital LibraryDigital Library
  6. Igarashi, Atsushi, and Naoki Kobayashi. 2002. Resource usage analysis. In POPL, 331--342. New York: ACM Press. Google ScholarGoogle ScholarDigital LibraryDigital Library
  7. Kiselyov, Oleg. 2004. Heavy-weight implementation of region calculus. http://okmij.org/ftp/Haskell/regions.html#heavy-weight.Google ScholarGoogle Scholar
  8. Kiselyov, Oleg. 2007. Type improvement constraint, local functional dependencies, and a type-level typecase. http://okmij.org/ftp/Haskell/typecast.html.Google ScholarGoogle Scholar
  9. Kiselyov, Oleg, Ralf Lämmel, and Keean Schupke. 2004. Strongly typed heterogeneous collections. In Proc. ACM SIG-PLAN workshop on Haskell, 96--107. Google ScholarGoogle ScholarDigital LibraryDigital Library
  10. Kiselyov, Oleg, and Chung-chieh Shan. 2007. Lightweight static resources: Sexy types for embedded and systems programming. In Draft proceedings of TFP 2007: 6th symposium on trends in functional programming, ed. Marco T. Morazán and Henrik Nilsson. Tech. Rep. TR-SHU-CS-2007-04-1, Department of Mathematics and Computer Science, Seton Hall University.Google ScholarGoogle Scholar
  11. Launchbury, John, and Simon L. Peyton Jones. 1995. State in Haskell. Lisp and Symbolic Computation 8(4):293--341. Google ScholarGoogle ScholarDigital LibraryDigital Library
  12. Launchbury, John, and Amr Sabry. 1997. Monadic State: Axiomatization and Type Safety. In ICFP, 227--238. Google ScholarGoogle ScholarDigital LibraryDigital Library
  13. Miller, Dale, and Alwen Tiu. 2005. A proof theory for generic judgments. ACM Trans. Comput. Log. 6(4):749--783. Google ScholarGoogle ScholarDigital LibraryDigital Library
  14. Moggi, Eugenio, and Amr Sabry. 2001. Monadic encapsulation of effects: A revised approach (extended version). Journal of Functional Programming 11(6):591--627. Google ScholarGoogle ScholarDigital LibraryDigital Library
  15. Pitts, Andrew M. 2003. Nominal logic, a first order theory of names and binding. Inf. Comput. 186(2):165--193. Google ScholarGoogle ScholarDigital LibraryDigital Library
  16. Strom, Robert E., and Daniel M. Yellin. 1993. Extending typestate checking using conditional liveness analysis. IEEE Transactions on Software Engineering 19(5):478--485. Google ScholarGoogle ScholarDigital LibraryDigital Library
  17. Tofte, Mads, Lars Birkedal, Martin Elsman, Niels Hallenberg, Tommy Højfeld Olesen, and Peter Sestoft. 2006. Programming with regions in the MLKit (revised for version 4.3.0). Tech. Rep., IT University of Copenhagen, Denmark.Google ScholarGoogle Scholar
  18. Tofte, Mads, and Jean-Pierre Talpin. 1997. Region-based memory management. Inf. Comput. 132(2):109--176. Google ScholarGoogle ScholarDigital LibraryDigital Library
  19. Walker, David, Karl Crary, and J. Gregory Morrisett. 2000. Typed memory management via static capabilities. ACM Trans. Program. Lang. Syst. 22(4):701--771. Google ScholarGoogle ScholarDigital LibraryDigital Library

Index Terms

  1. Lightweight monadic regions

      Recommendations

      Comments

      Login options

      Check if you have access through your login credentials or your institution to get full access on this article.

      Sign in

      PDF Format

      View or Download as a PDF file.

      PDF

      eReader

      View online with eReader.

      eReader
      About Cookies On This Site

      We use cookies to ensure that we give you the best experience on our website.

      Learn more

      Got it!