When a C programmer needs an efficient data structure for a particular problem, he or she can often simply look one up in any of a number of good textbooks or handbooks. Unfortunately, programmers in functional languages such as Standard ML or Haskell do not have this luxury. Although some data structures designed for imperative languages such as C can be quite easily adapted to a functional setting, most cannot, usually because they depend in crucial ways on assignments, which are disallowed, or at least discouraged, in functional languages. To address this imbalance, we describe several techniques for designing functional data structures, and numerous original data structures based on these techniques, including multiple variations of lists, queues, double-ended queues, and heaps, many supporting more exotic features such as random access or efficient catenation.
In addition, we expose the fundamental role of lazy evaluation in amortized functional data structures. Traditional methods of amortization break down when old versions of a data structure, not just the most recent, are available for further processing. This property is known as persistence, and is taken for granted in functional languages. On the surface, persistence and amortization appear to be incompatible, but we show how lazy evaluation can be used to resolve this conflict, yielding amortized data structures that are efficient even when used persistently. Turning this relationship between lazy evaluation and amortization around, the notion of amortization also provides the first practical techniques for analyzing the time requirements of non-trivial lazy programs.
Finally, our data structures offer numerous hints to programming language designers, illustrating the utility of combining strict and lazy evaluation in a single language, and providing non-trivial examples using polymorphic recursion and higher-order, recursive modules.
Cited By
- Das A, Deyoung H, Mordido A and Pfenning F (2022). Nested Session Types, ACM Transactions on Programming Languages and Systems, 44:3, (1-45), Online publication date: 30-Sep-2022.
- Martinez B, Viera M and Pardo A Just do it while compiling! Proceedings of the ACM SIGPLAN 2013 workshop on Partial evaluation and program manipulation, (77-86)
- Grzanek K and Cader A Protrace Proceedings of the 10th international conference on Artifical intelligence and soft computing: Part II, (468-475)
- Defoe D, LeGrand R and Cytron R On the connection between functional programming languages and real-time Java scoped memory Proceedings of the 5th international workshop on Java technologies for real-time and embedded systems, (73-82)
- Abel A Towards generic programming with sized types Proceedings of the 8th international conference on Mathematics of Program Construction, (10-28)
- Fiat A and Kaplan H (2003). Making data structures confluently persistent, Journal of Algorithms, 48:1, (16-58), Online publication date: 1-Aug-2003.
- Fiat A and Kaplan H Making data structures confluently persistent Proceedings of the twelfth annual ACM-SIAM symposium on Discrete algorithms, (537-546)
- Ganz S, Sabry A and Taha W (2001). Macros as multi-stage computations, ACM SIGPLAN Notices, 36:10, (74-85), Online publication date: 1-Oct-2001.
- Ganz S, Sabry A and Taha W Macros as multi-stage computations Proceedings of the sixth ACM SIGPLAN international conference on Functional programming, (74-85)
- Launchbury J, Lewis J and Cook B (1999). On embedding a microarchitectural design language within Haskell, ACM SIGPLAN Notices, 34:9, (60-69), Online publication date: 1-Sep-1999.
- Launchbury J, Lewis J and Cook B On embedding a microarchitectural design language within Haskell Proceedings of the fourth ACM SIGPLAN international conference on Functional programming, (60-69)
- Peyton Jones S, Shields M, Launchbury J and Tolmach A Bridging the gulf Proceedings of the 25th ACM SIGPLAN-SIGACT symposium on Principles of programming languages, (49-61)
- Brodal G Finger search trees with constant insertion time Proceedings of the ninth annual ACM-SIAM symposium on Discrete algorithms, (540-549)
- Okasaki C (1997). Catenable double-ended queues, ACM SIGPLAN Notices, 32:8, (66-74), Online publication date: 1-Aug-1997.
- Okasaki C Catenable double-ended queues Proceedings of the second ACM SIGPLAN international conference on Functional programming, (66-74)
Recommendations
Purely functional lazy nondeterministic programming
Dedicated to ICFP 2009Functional logic programming and probabilistic programming have demonstrated the broad benefits of combining laziness (nonstrict evaluation with sharing of the results) with nondeterminism. Yet these benefits are seldom enjoyed in functional programming ...
Lightweight semiformal time complexity analysis for purely functional data structures
POPL '08: Proceedings of the 35th annual ACM SIGPLAN-SIGACT symposium on Principles of programming languagesOkasaki and others have demonstrated how purely functional data structures that are efficient even in the presence of persistence can be constructed. To achieve good time bounds essential use is often made of laziness. The associated complexity analysis ...