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.
Supplemental Material
Available for Download
- 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 Scholar
Digital Library
- 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 Scholar
Digital Library
- Fluet, Matthew, Greg Morrisett, and Amal J. Ahmed. 2006. Linear regions are all you need. In ESOP, 7--21. Google Scholar
Digital Library
- 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 Scholar
Digital Library
- Grossman, Dan, Greg Morrisett, Trevor Jim, Michael Hicks, Yanling Wang, and James Cheney. Region-based memory management in Cyclone. In PLDI, 282--293. Google Scholar
Digital Library
- Igarashi, Atsushi, and Naoki Kobayashi. 2002. Resource usage analysis. In POPL, 331--342. New York: ACM Press. Google Scholar
Digital Library
- Kiselyov, Oleg. 2004. Heavy-weight implementation of region calculus. http://okmij.org/ftp/Haskell/regions.html#heavy-weight.Google Scholar
- Kiselyov, Oleg. 2007. Type improvement constraint, local functional dependencies, and a type-level typecase. http://okmij.org/ftp/Haskell/typecast.html.Google Scholar
- Kiselyov, Oleg, Ralf Lämmel, and Keean Schupke. 2004. Strongly typed heterogeneous collections. In Proc. ACM SIG-PLAN workshop on Haskell, 96--107. Google Scholar
Digital Library
- 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 Scholar
- Launchbury, John, and Simon L. Peyton Jones. 1995. State in Haskell. Lisp and Symbolic Computation 8(4):293--341. Google Scholar
Digital Library
- Launchbury, John, and Amr Sabry. 1997. Monadic State: Axiomatization and Type Safety. In ICFP, 227--238. Google Scholar
Digital Library
- Miller, Dale, and Alwen Tiu. 2005. A proof theory for generic judgments. ACM Trans. Comput. Log. 6(4):749--783. Google Scholar
Digital Library
- Moggi, Eugenio, and Amr Sabry. 2001. Monadic encapsulation of effects: A revised approach (extended version). Journal of Functional Programming 11(6):591--627. Google Scholar
Digital Library
- Pitts, Andrew M. 2003. Nominal logic, a first order theory of names and binding. Inf. Comput. 186(2):165--193. Google Scholar
Digital Library
- 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 Scholar
Digital Library
- 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 Scholar
- Tofte, Mads, and Jean-Pierre Talpin. 1997. Region-based memory management. Inf. Comput. 132(2):109--176. Google Scholar
Digital Library
- 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 Scholar
Digital Library
Index Terms
Lightweight monadic regions
Recommendations
Lightweight monadic regions
HASKELL '08We 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 ...
Monadic regions
ICFP '04Region-based type systems provide programmer control over memory management without sacrificing type-safety. However, the type systems for region-based languages, such as the ML-Kit or Cyclone, are relatively complicated, so proving their soundness is ...
Recursive type generativity
Proceedings of the tenth ACM SIGPLAN international conference on Functional programmingExistential types provide a simple and elegant foundation for understanding generative abstract data types, of the kind supported by the Standard ML module system. However, in attempting to extend ML with support for recursive modules, we have found ...







Comments