130 likes | 234 Vues
This paper explores the integration of monadic structures with region-based memory management to enhance the management of object lifetimes in pure programming languages. By introducing a monadic sub-language, we bridge the two lines of research: region algebras and monadic effects. We present the design of a region monad, RGN, and demonstrate how it encapsulates region computations and memory allocation through efficient type constructs. Our work lays the foundation for sophisticated type-preserving translations and the encapsulation of imperative features within pure languages.
E N D
Monadic Regions Matthew Fluet Cornell University
Introduction • Draw together two lines of research • Region-based memory management • Regions delimit lifetimes of objects • new .e • Monadic encapsulation of effects • Embed imperative features in pure languages • runST :: .(s. ST s )
Introduction • Encode Tofte-Talpin region calculus in System F with monadic sub-language • Features • runST serves as inspiration • runRGN – encapsulate region computation • newRGN – encapsulate a single region • Sufficient polymorphism to encode region polymorphism
FRGN = System F + RGN monad • System F • Monadic (sub)-language • Monadic types and operations • RGN r – monadic region computations • RGNVar r – region allocated values • RGNHandle r – region handles
FRGN = System F + RGN monad • Create and read region allocated values allocRGNVar :: ,r. RGNHandle r RGN r (RGNVar r ) readRGNVar :: ,r. RGNVar r RGN r
FRGN = System F + RGN monad • Encapsulate and run a monadic computation runRGN :: .(r. RGNHandle r RGN r )
FRGN = System F + RGN monad • Encapsulate a region newRGN :: ,r.(s. RGNHandle s RGN s ) RGN r
FRGN = System F + RGN monad • Encapsulate a region newRGN :: ,r.(s. r s RGNHandle s RGN s ) RGN r α ≡ ,r,s. RGN r RGN s
Single Effect Calculus • LIFO stack of regions imposes a partial order on live (allocated) regions • Regions lower on the stack outlive regions higher on the stack • A single region can serve as a witness for a set of effects • Region appears as a single effect in place of the set
Translation • Type-preserving translation from Single Effect Calculus to FRGN new .e newRGN (.w.h. e)
Conclusion • Monadic encoding of effects applicable to region calculi • Trivial (syntactic) equality on types • Encapsulation within monad
FRGN = System F + RGN monad • Monadic unit and bind returnRGN :: ,r. RGN r thenRGN :: ,,r. RGN r ( RGN r ) RGN r