{"id":964929,"date":"2023-09-12T09:55:00","date_gmt":"2023-09-12T16:55:00","guid":{"rendered":"https:\/\/www.microsoft.com\/en-us\/research\/?p=964929"},"modified":"2023-09-12T09:58:07","modified_gmt":"2023-09-12T16:58:07","slug":"fp2-fully-in-place-functional-programming-provides-memory-reuse-for-pure-functional-programs","status":"publish","type":"post","link":"https:\/\/www.microsoft.com\/en-us\/research\/blog\/fp2-fully-in-place-functional-programming-provides-memory-reuse-for-pure-functional-programs\/","title":{"rendered":"FP2: Fully In-Place Functional Programming provides memory reuse for pure functional programs\u00a0"},"content":{"rendered":"\n<p class=\"has-text-align-center h6\"><em>This research paper was presented at the <\/em><a class=\"msr-external-link glyph-append glyph-append-open-in-new-tab glyph-append-xsmall\" href=\"https:\/\/icfp23.sigplan.org\/\" target=\"_blank\" rel=\"noopener noreferrer\"><em>28<sup>th<\/sup> ACM SIGPLAN International Conference on Functional Programming<\/em><span class=\"sr-only\"> (opens in new tab)<\/span><\/a><em> (ICFP), a premier forum for discussing design, implementations, principles, and uses of functional programming.<\/em><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1400\" height=\"788\" src=\"https:\/\/www.microsoft.com\/en-us\/research\/wp-content\/uploads\/2023\/09\/ICFP23-BlogHeroFeature-1400x788-1.jpg\" alt=\"FP2: Fully In-Place Functional Programming; ICFP 2023\" class=\"wp-image-964953\" srcset=\"https:\/\/www.microsoft.com\/en-us\/research\/wp-content\/uploads\/2023\/09\/ICFP23-BlogHeroFeature-1400x788-1.jpg 1400w, https:\/\/www.microsoft.com\/en-us\/research\/wp-content\/uploads\/2023\/09\/ICFP23-BlogHeroFeature-1400x788-1-300x169.jpg 300w, https:\/\/www.microsoft.com\/en-us\/research\/wp-content\/uploads\/2023\/09\/ICFP23-BlogHeroFeature-1400x788-1-1024x576.jpg 1024w, https:\/\/www.microsoft.com\/en-us\/research\/wp-content\/uploads\/2023\/09\/ICFP23-BlogHeroFeature-1400x788-1-768x432.jpg 768w, https:\/\/www.microsoft.com\/en-us\/research\/wp-content\/uploads\/2023\/09\/ICFP23-BlogHeroFeature-1400x788-1-1066x600.jpg 1066w, https:\/\/www.microsoft.com\/en-us\/research\/wp-content\/uploads\/2023\/09\/ICFP23-BlogHeroFeature-1400x788-1-655x368.jpg 655w, https:\/\/www.microsoft.com\/en-us\/research\/wp-content\/uploads\/2023\/09\/ICFP23-BlogHeroFeature-1400x788-1-343x193.jpg 343w, https:\/\/www.microsoft.com\/en-us\/research\/wp-content\/uploads\/2023\/09\/ICFP23-BlogHeroFeature-1400x788-1-240x135.jpg 240w, https:\/\/www.microsoft.com\/en-us\/research\/wp-content\/uploads\/2023\/09\/ICFP23-BlogHeroFeature-1400x788-1-640x360.jpg 640w, https:\/\/www.microsoft.com\/en-us\/research\/wp-content\/uploads\/2023\/09\/ICFP23-BlogHeroFeature-1400x788-1-960x540.jpg 960w, https:\/\/www.microsoft.com\/en-us\/research\/wp-content\/uploads\/2023\/09\/ICFP23-BlogHeroFeature-1400x788-1-1280x720.jpg 1280w\" sizes=\"auto, (max-width: 1400px) 100vw, 1400px\" \/><\/figure>\n\n\n\n<p>Functional programming languages offer a host of advantages, such as <a class=\"msr-external-link glyph-append glyph-append-open-in-new-tab glyph-append-xsmall\" rel=\"noopener noreferrer\" target=\"_blank\" href=\"https:\/\/www.cse.chalmers.se\/~rjmh\/Papers\/whyfp.pdf\">ensuring memory safety<span class=\"sr-only\"> (opens in new tab)<\/span><\/a> and eliminating arbitrary side effects. This enables systematic analysis and compositional program construction, facilitating development of scalable and complex software systems. However, a drawback of functional programming is its tendency to liberally allocate new memory. We believe this characteristic has impeded widespread adoption in performance-critical domains. How can we overcome this limitation and harness the benefits of functional programming while maintaining efficient memory usage?&nbsp;<\/p>\n\n\n\n<p>To illustrate the issue, let\u2019s examine the well-known functional program to reverse a list in linear time using an accumulating parameter:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img decoding=\"async\" src=\"https:\/\/www.microsoft.com\/en-us\/research\/wp-content\/uploads\/2023\/09\/FiP_reverse-list-code-block-Koka.png\" alt=\"FP2: Fully In-Place Functional Programming - reverse list code in Koka\" class=\"wp-image-967419\" style=\"width:450px\" width=\"450\" srcset=\"https:\/\/www.microsoft.com\/en-us\/research\/wp-content\/uploads\/2023\/09\/FiP_reverse-list-code-block-Koka.png 751w, https:\/\/www.microsoft.com\/en-us\/research\/wp-content\/uploads\/2023\/09\/FiP_reverse-list-code-block-Koka-300x102.png 300w, https:\/\/www.microsoft.com\/en-us\/research\/wp-content\/uploads\/2023\/09\/FiP_reverse-list-code-block-Koka-240x81.png 240w\" sizes=\"(max-width: 751px) 100vw, 751px\" \/><\/figure>\n\n\n\n<p>The reversal function is written in <a class=\"msr-external-link glyph-append glyph-append-open-in-new-tab glyph-append-xsmall\" href=\"https:\/\/koka-lang.github.io\/\" target=\"_blank\" rel=\"noopener noreferrer\">Koka<span class=\"sr-only\"> (opens in new tab)<\/span><\/a>, a functional language developed at Microsoft that implements the techniques described in this blog post. Here, a list is either empty (as <code><font color=\"#800000\">Nil<\/font><\/code>) or non-empty as a <code><font color=\"#800000\">Cons<\/font>(head,tail)<\/code> node, and contains the first element as the head and the rest of the list as the <code>tail<\/code>.&nbsp;<\/p>\n\n\n\n<p>In most functional languages, reversing a list this way allocates a fresh result list in the heap, where a list of integers from 1 to 10 is reversed, as shown in Figure 1.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large is-resized\"><img decoding=\"async\" src=\"https:\/\/www.microsoft.com\/en-us\/research\/wp-content\/uploads\/2023\/09\/fip-blog-fig2-1024x950.png\" alt=\"FP2: Fully In-Place Functional Programming; Fig 1- This illustration shows two single-linked lists. The first single-linked list contains the numbers 6 to 10 and is pointed to by \"xs.\" The second single-linked list contains the numbers 5 to 1 and is pointed to by \"acc.\"\" class=\"wp-image-964947\" style=\"width:400px\" width=\"400\" srcset=\"https:\/\/www.microsoft.com\/en-us\/research\/wp-content\/uploads\/2023\/09\/fip-blog-fig2-1024x950.png 1024w, https:\/\/www.microsoft.com\/en-us\/research\/wp-content\/uploads\/2023\/09\/fip-blog-fig2-300x278.png 300w, https:\/\/www.microsoft.com\/en-us\/research\/wp-content\/uploads\/2023\/09\/fip-blog-fig2-768x712.png 768w, https:\/\/www.microsoft.com\/en-us\/research\/wp-content\/uploads\/2023\/09\/fip-blog-fig2-194x180.png 194w, https:\/\/www.microsoft.com\/en-us\/research\/wp-content\/uploads\/2023\/09\/fip-blog-fig2.png 1065w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">Figure 1: The list [1..5] has already been reversed into acc, but we still must reverse the list [6..10]. <\/figcaption><\/figure>\n\n\n\n<p>As the list <code>xs<\/code> is non-empty, we add its first element to our accumulating <code>acc<\/code> parameter before recursing on the rest of the list <code>xx<\/code>. As shown in Figure 2, this step allocates a new <code><font color=\"#800000\">Cons<\/font><\/code> cell but also leaves the <code><font color=\"#800000\">Cons<\/font><\/code> cell of <code>xs<\/code> to be garbage collected. This is rather wasteful.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large is-resized\"><img decoding=\"async\" src=\"https:\/\/www.microsoft.com\/en-us\/research\/wp-content\/uploads\/2023\/09\/fip-blog-fig3-1024x978.png\" alt=\"FP2: Fully In-Place Functional Programming; Fig 3- This illustration depicts two single-linked lists. The first single-linked list contains the numbers 7 to 10 and is pointed to by \"xs.\" Above the list, there is the list element containing the number 6, which is now up for deletion and is red. The second single-linked list contains the numbers 6 to 1 and is pointed to by \"acc.\" The list element containing 6 is newly allocated and is green.\" class=\"wp-image-964950\" style=\"width:400px\" width=\"400\" srcset=\"https:\/\/www.microsoft.com\/en-us\/research\/wp-content\/uploads\/2023\/09\/fip-blog-fig3-1024x978.png 1024w, https:\/\/www.microsoft.com\/en-us\/research\/wp-content\/uploads\/2023\/09\/fip-blog-fig3-300x287.png 300w, https:\/\/www.microsoft.com\/en-us\/research\/wp-content\/uploads\/2023\/09\/fip-blog-fig3-768x734.png 768w, https:\/\/www.microsoft.com\/en-us\/research\/wp-content\/uploads\/2023\/09\/fip-blog-fig3-188x180.png 188w, https:\/\/www.microsoft.com\/en-us\/research\/wp-content\/uploads\/2023\/09\/fip-blog-fig3.png 1048w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">Figure 2: The lists after one step of recursion. The top <code><font color=\"#800000\">Cons<\/font><\/code> cell on the left has become garbage, while the top <code><font color=\"#800000\">Cons<\/font><\/code> cell on the right is freshly allocated.<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"fully-in-place-functional-programming-avoids-allocation\">Fully in-place functional programming avoids allocation&nbsp;<\/h2>\n\n\n\n<p>Recent developments have made it possible to avoid such allocations. In particular, by using a compiler-guided reference counting algorithm called <a href=\"https:\/\/www.microsoft.com\/en-us\/research\/publication\/perceus-garbage-free-reference-counting-with-reuse-2\/\">Perceus<\/a>, we can reuse objects in place whenever the objects are uniquely referenced at runtime. With such reuse, the reverse function can reverse a unique input list xs <em>in-place <\/em>without allocating any fresh <code><font color=\"#800000\">Cons<\/font><\/code> nodes, essentially switching the tail pointers of xs in-place. However, the dynamic nature of this form of reuse makes it hard to predict its application at runtime.\u00a0\u00a0<\/p>\n\n\n\n<p>In our paper, \u201c<a href=\"https:\/\/www.microsoft.com\/en-us\/research\/publication\/fp2-fully-in-place-functional-programming-2\/\">FP<sup>2<\/sup>: Fully in-Place Functional Programming<\/a>,\u201d which we\u2019re presenting at <a class=\"msr-external-link glyph-append glyph-append-open-in-new-tab glyph-append-xsmall\" href=\"https:\/\/icfp23.sigplan.org\/\" target=\"_blank\" rel=\"noopener noreferrer\">ICFP 2023<span class=\"sr-only\"> (opens in new tab)<\/span><\/a>, we describe the new <code><font color=\"#0000FF\">fip<\/font><\/code> keyword. It statically checks that programs like the accumulating reverse function can execute <em>in-place<\/em>, that is, using constant stack space without needing any heap allocation as long as the arguments are unique.<\/p>\n\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n\t<div class=\"border-bottom border-top border-gray-300 mt-5 mb-5 msr-promo text-center text-md-left alignwide\" data-bi-aN=\"promo\" data-bi-id=\"1144027\">\n\t\t\n\n\t\t<p class=\"msr-promo__label text-gray-800 text-center text-uppercase\">\n\t\t<span class=\"px-4 bg-white display-inline-block font-weight-semibold small\">PODCAST SERIES<\/span>\n\t<\/p>\n\t\n\t<div class=\"row pt-3 pb-4 align-items-center\">\n\t\t\t\t\t\t<div class=\"msr-promo__media col-12 col-md-5\">\n\t\t\t\t<a class=\"bg-gray-300 display-block\" href=\"https:\/\/www.microsoft.com\/en-us\/research\/story\/ai-testing-and-evaluation-learnings-from-science-and-industry\/\" aria-label=\"AI Testing and Evaluation: Learnings from Science and Industry\" data-bi-cN=\"AI Testing and Evaluation: Learnings from Science and Industry\" target=\"_blank\">\n\t\t\t\t\t<img decoding=\"async\" class=\"w-100 display-block\" src=\"https:\/\/www.microsoft.com\/en-us\/research\/wp-content\/uploads\/2025\/06\/EP2-AI-TE_Hero_Feature_River_No_Text_1400x788.jpg\" alt=\"Illustrated headshots of Daniel Carpenter, Timo Minssen, Chad Atalla, and Kathleen Sullivan for the Microsoft Research Podcast\" \/>\n\t\t\t\t<\/a>\n\t\t\t<\/div>\n\t\t\t\n\t\t\t<div class=\"msr-promo__content p-3 px-5 col-12 col-md\">\n\n\t\t\t\t\t\t\t\t\t<h2 class=\"h4\">AI Testing and Evaluation: Learnings from Science and Industry<\/h2>\n\t\t\t\t\n\t\t\t\t\t\t\t\t<p id=\"ai-testing-and-evaluation-learnings-from-science-and-industry\" class=\"large\">Discover how Microsoft is learning from other domains to advance evaluation and testing as a pillar of AI governance.<\/p>\n\t\t\t\t\n\t\t\t\t\t\t\t\t<div class=\"wp-block-buttons justify-content-center justify-content-md-start\">\n\t\t\t\t\t<div class=\"wp-block-button\">\n\t\t\t\t\t\t<a href=\"https:\/\/www.microsoft.com\/en-us\/research\/story\/ai-testing-and-evaluation-learnings-from-science-and-industry\/\" aria-describedby=\"ai-testing-and-evaluation-learnings-from-science-and-industry\" class=\"btn btn-brand glyph-append glyph-append-chevron-right\" data-bi-cN=\"AI Testing and Evaluation: Learnings from Science and Industry\" target=\"_blank\">\n\t\t\t\t\t\t\tListen now\t\t\t\t\t\t<\/a>\n\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t\t\t<\/div><!--\/.msr-promo__content-->\n\t<\/div><!--\/.msr-promo__inner-wrap-->\n\t<\/div><!--\/.msr-promo-->\n\t\n\n\n<h2 class=\"wp-block-heading\" id=\"tree-traversals-and-zippers\">Tree traversals and zippers<\/h2>\n\n\n\n<p>In fact, many familiar functions and algorithms satisfy our fully in-place criteria. For example, consider a binary tree with all the values at the leaves:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img decoding=\"async\" src=\"https:\/\/www.microsoft.com\/en-us\/research\/wp-content\/uploads\/2023\/09\/FiP_binary-tree-code-block-Koka.png\" alt=\"FP2: Fully In-Place Functional Programming - binary tree code in Koka\" class=\"wp-image-967398\" style=\"width:300px\" width=\"300\" srcset=\"https:\/\/www.microsoft.com\/en-us\/research\/wp-content\/uploads\/2023\/09\/FiP_binary-tree-code-block-Koka.png 501w, https:\/\/www.microsoft.com\/en-us\/research\/wp-content\/uploads\/2023\/09\/FiP_binary-tree-code-block-Koka-300x67.png 300w, https:\/\/www.microsoft.com\/en-us\/research\/wp-content\/uploads\/2023\/09\/FiP_binary-tree-code-block-Koka-240x54.png 240w\" sizes=\"(max-width: 501px) 100vw, 501px\" \/><\/figure>\n\n\n\n<p>Now, suppose that we want to navigate through this tree, moving up and down in search of a particular element. You might add parent pointers, but in a functional language, there is an alternative solution originally proposed by G\u00e9rard Huet known as the <em><a class=\"msr-external-link glyph-append glyph-append-open-in-new-tab glyph-append-xsmall\" href=\"https:\/\/www.cambridge.org\/core\/journals\/journal-of-functional-programming\/article\/zipper\/0C058890B8A9B588F26E6D68CF0CE204\" target=\"_blank\" rel=\"noopener noreferrer\">zipper<span class=\"sr-only\"> (opens in new tab)<\/span><\/a><\/em>:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img decoding=\"async\" src=\"https:\/\/www.microsoft.com\/en-us\/research\/wp-content\/uploads\/2023\/09\/FiP_Zipper-code-block-Koka.png\" alt=\"FP2: Fully In-Place Functional Programming - Zipper code in Koka\" class=\"wp-image-967401\" style=\"width:300px\" width=\"300\" srcset=\"https:\/\/www.microsoft.com\/en-us\/research\/wp-content\/uploads\/2023\/09\/FiP_Zipper-code-block-Koka.png 511w, https:\/\/www.microsoft.com\/en-us\/research\/wp-content\/uploads\/2023\/09\/FiP_Zipper-code-block-Koka-300x89.png 300w, https:\/\/www.microsoft.com\/en-us\/research\/wp-content\/uploads\/2023\/09\/FiP_Zipper-code-block-Koka-240x71.png 240w\" sizes=\"(max-width: 511px) 100vw, 511px\" \/><\/figure>\n\n\n\n<div class=\"annotations \" data-bi-aN=\"margin-callout\">\n\t<article class=\"annotations__list card depth-16 bg-body p-4 annotations__list--right\">\n\t\t<div class=\"annotations__list-item\">\n\t\t\t\t\t\t<span class=\"annotations__type d-block text-uppercase font-weight-semibold text-neutral-300 small\">Tool<\/span>\n\t\t\t<a href=\"https:\/\/github.com\/koka-lang\/koka\" data-bi-cN=\"Koka\" data-external-link=\"false\" data-bi-aN=\"margin-callout\" data-bi-type=\"annotated-link\" class=\"annotations__link font-weight-semibold text-decoration-none\"><span>Koka<\/span>&nbsp;<span class=\"glyph-in-link glyph-append glyph-append-chevron-right\" aria-hidden=\"true\"><\/span><\/a>\t\t\t\t\t<\/div>\n\t<\/article>\n<\/div>\n\n\n\n<p>The zipper stores subtrees along the path from the current node up to the root node. We can define operations on pairs consisting of this type of zipper and the current tree, enabling seamless movement through the tree. For example, the following function uses the zipper to move the focus to the left subtree:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img decoding=\"async\" src=\"https:\/\/www.microsoft.com\/en-us\/research\/wp-content\/uploads\/2023\/09\/FiP_left-subtree-code-block-Koka.png\" alt=\"FP2: Fully In-Place Functional Programming - focus on left subtree code in Koka\" class=\"wp-image-967395\" style=\"width:480px\" width=\"480\" srcset=\"https:\/\/www.microsoft.com\/en-us\/research\/wp-content\/uploads\/2023\/09\/FiP_left-subtree-code-block-Koka.png 843w, https:\/\/www.microsoft.com\/en-us\/research\/wp-content\/uploads\/2023\/09\/FiP_left-subtree-code-block-Koka-300x53.png 300w, https:\/\/www.microsoft.com\/en-us\/research\/wp-content\/uploads\/2023\/09\/FiP_left-subtree-code-block-Koka-768x137.png 768w, https:\/\/www.microsoft.com\/en-us\/research\/wp-content\/uploads\/2023\/09\/FiP_left-subtree-code-block-Koka-240x43.png 240w\" sizes=\"(max-width: 843px) 100vw, 843px\" \/><\/figure>\n\n\n\n<p>Here, we move to the left subtree of the current node (if it exists) and extend the zipper data type accordingly. In his 1997, Huet already observed that such zipper operations could be implemented in place:<\/p>\n\n\n\n<figure class=\"wp-block-pullquote\"><blockquote><p><em>Efficient destructive algorithms on binary trees may be programmed with these completely applicative primitives, which all use constant time, since they all reduce to local pointer manipulation<\/em>.<\/p><\/blockquote><\/figure>\n\n\n\n<p>In Koka, we can now make Huet\u2019s intuition precise, where the <code><font color=\"#0000FF\">fip<\/font><\/code> keyword guarantees that <code>left<\/code> is in place.\u00a0On closer examination, this might be surprising. While the list reversal example reused a <code><font color=\"#800000\">Cons<\/font><\/code> node, here it seems like we may need to garbage collect a <code><font color=\"#800000\">Bin<\/font><\/code> constructor and allocate a new <code><font color=\"#800000\">BinL<\/font><\/code> constructor. Nonetheless, because both constructors have two fields, the previous <code><font color=\"#800000\">Bin<\/font><\/code> memory location can still be reused (only updating the constructor tag). Our <a href=\"https:\/\/www.microsoft.com\/en-us\/research\/publication\/fp2-fully-in-place-functional-programming-2\/\" target=\"_blank\" rel=\"noreferrer noopener\">paper<\/a> provides the analysis details that enable this, rooted in the concept of &#8220;reuse credits.&#8221;<\/p>\n\n\n\n<p>Now, suppose we want to update all the values stored in a tree. Using a zipper, we can do this fully in place. While traversing, the zipper stores input tree fragments in order, using <code><font color=\"#800000\">BinL<\/font><\/code> for unvisited and <code><font color=\"#800000\">BinR<\/font><\/code> for visited subtrees. Reusing the zipper nodes allows in-order tree mapping without heap or stack usage. The tree map function starts by descending to the leftmost leaf, accumulating unvisited subtrees in <code><font color=\"#800000\">BinL<\/font><\/code>. Once we hit the leftmost leaf, we apply the argument function f and work our way back up, recursively processing any unvisited subtrees, as shown in Figure 3.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img decoding=\"async\" src=\"https:\/\/www.microsoft.com\/en-us\/research\/wp-content\/uploads\/2023\/09\/FiP_unvisited-subtrees-code-block-Koka.png\" alt=\"FP2: Fully In-Place Functional Programming - unvisited subtrees code in Koka\" class=\"wp-image-967392\" style=\"width:540px\" width=\"540\" srcset=\"https:\/\/www.microsoft.com\/en-us\/research\/wp-content\/uploads\/2023\/09\/FiP_unvisited-subtrees-code-block-Koka.png 880w, https:\/\/www.microsoft.com\/en-us\/research\/wp-content\/uploads\/2023\/09\/FiP_unvisited-subtrees-code-block-Koka-300x207.png 300w, https:\/\/www.microsoft.com\/en-us\/research\/wp-content\/uploads\/2023\/09\/FiP_unvisited-subtrees-code-block-Koka-768x530.png 768w, https:\/\/www.microsoft.com\/en-us\/research\/wp-content\/uploads\/2023\/09\/FiP_unvisited-subtrees-code-block-Koka-240x166.png 240w\" sizes=\"(max-width: 880px) 100vw, 880px\" \/><\/figure>\n\n\n\n<p>The mutually tail-recursive app and down functions are fully in place. Each matched <code><font color=\"#800000\">Bin<\/font><\/code> pairs with <code><font color=\"#800000\">BinL<\/font><\/code>, and each <code><font color=\"#800000\">BinL<\/font><\/code> with <code><font color=\"#800000\">BinR<\/font><\/code>, ultimately leading to <code><font color=\"#800000\">BinR<\/font><\/code> pairing with <code><font color=\"#800000\">Bin<\/font><\/code>. The definition of <code>tmap<\/code> may seem somewhat complex, but it is much simpler than its iterative imperative counterpart that uses direct pointer reversal.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large is-resized\"><img decoding=\"async\" src=\"https:\/\/www.microsoft.com\/en-us\/research\/wp-content\/uploads\/2023\/09\/fip-blog-fig1-1024x572.png\" alt=\"FP2: Fully In-Place Functional Programming; Fig 3- An illustration of a binary search tree, where the search path has been pointer-reversed. There are five nodes in total: three leaf nodes and two internal nodes. The first leaf node is the left child of the root and has already been visited. The root node is marked as \"BinR\" since our method descended into its right subtree. The root does not point to a right child, but instead its former right child points up to the root. This former child is marked \"BinL\" since our search descended into its left child, and its right child is an unvisited leaf. At this state of the program, our accumulator \"acc\" is the former right child of the root, and our current element \"xs\" is the former left child of the former right child of the root.\" class=\"wp-image-964944\" style=\"width:400px\" width=\"400\" srcset=\"https:\/\/www.microsoft.com\/en-us\/research\/wp-content\/uploads\/2023\/09\/fip-blog-fig1-1024x572.png 1024w, https:\/\/www.microsoft.com\/en-us\/research\/wp-content\/uploads\/2023\/09\/fip-blog-fig1-300x168.png 300w, https:\/\/www.microsoft.com\/en-us\/research\/wp-content\/uploads\/2023\/09\/fip-blog-fig1-768x429.png 768w, https:\/\/www.microsoft.com\/en-us\/research\/wp-content\/uploads\/2023\/09\/fip-blog-fig1-343x193.png 343w, https:\/\/www.microsoft.com\/en-us\/research\/wp-content\/uploads\/2023\/09\/fip-blog-fig1-240x134.png 240w, https:\/\/www.microsoft.com\/en-us\/research\/wp-content\/uploads\/2023\/09\/fip-blog-fig1.png 1502w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">Figure 3: The program after visiting the leaf containing f(2) on the given tree. The pointers in the zipper are reversed.<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"perspectives-and-further-reading\">Perspectives and further reading<\/h2>\n\n\n\n<p>Koka\u2019s new <code><font color=\"#0000FF\">fip<\/font><\/code> keyword ensures that certain functions do not allocate and only use constant stack space, offering efficient and secure code execution akin to static linear types or Rust\u2019s borrow checker. This introduces a new paradigm for writing programs that are purely functional but can still execute in place. We consider this new technique to be a significant milestone on the path toward using high-level functional programming to develop robust software that delivers both competitive and predictable performance.&nbsp;<\/p>\n\n\n\n<p>To learn about fully in-place functional programming and the Koka language, start at the <a class=\"msr-external-link glyph-append glyph-append-open-in-new-tab glyph-append-xsmall\" href=\"https:\/\/koka-lang.github.io\/koka\/doc\/index.html\" target=\"_blank\" rel=\"noopener noreferrer\">Koka homepage<span class=\"sr-only\"> (opens in new tab)<\/span><\/a>. Koka implements a variety of innovative language features, including algebraic effect handlers and first-class constructor contexts. We encourage readers to continue exploring and experimenting with fully in-place programming. For example, try implementing <a class=\"msr-external-link glyph-append glyph-append-open-in-new-tab glyph-append-xsmall\" href=\"https:\/\/en.wikipedia.org\/wiki\/Skew_heap\" target=\"_blank\" rel=\"noopener noreferrer\">skew binary heaps<span class=\"sr-only\"> (opens in new tab)<\/span><\/a> in Koka. Can you demonstrate fully in-place heap union?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This research paper was presented at the 28th ACM SIGPLAN International Conference on Functional Programming (opens in new tab) (ICFP), a premier forum for discussing design, implementations, principles, and uses of functional programming. Functional programming languages offer a host of advantages, such as ensuring memory safety (opens in new tab) and eliminating arbitrary side effects. [&hellip;]<\/p>\n","protected":false},"author":42735,"featured_media":964953,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"msr-url-field":"","msr-podcast-episode":"","msrModifiedDate":"","msrModifiedDateEnabled":false,"ep_exclude_from_search":false,"_classifai_error":"","msr-author-ordering":null,"msr_hide_image_in_river":0,"footnotes":""},"categories":[1],"tags":[],"research-area":[13560],"msr-region":[],"msr-event-type":[],"msr-locale":[268875],"msr-post-option":[],"msr-impact-theme":[264846],"msr-promo-type":[],"msr-podcast-series":[],"class_list":["post-964929","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-research-blog","msr-research-area-programming-languages-software-engineering","msr-locale-en_us"],"msr_event_details":{"start":"","end":"","location":""},"podcast_url":"","podcast_episode":"","msr_research_lab":[],"msr_impact_theme":["Computing foundations"],"related-publications":[],"related-downloads":[],"related-videos":[],"related-academic-programs":[],"related-groups":[],"related-projects":[],"related-events":[958992],"related-researchers":[{"type":"guest","value":"anton-lorenzen","user_id":"965007","display_name":"Anton Lorenzen","author_link":"Anton Lorenzen","is_active":true,"last_first":"Lorenzen, Anton","people_section":0,"alias":"anton-lorenzen"},{"type":"user_nicename","value":"Daan Leijen","user_id":31497,"display_name":"Daan Leijen","author_link":"<a href=\"https:\/\/www.microsoft.com\/en-us\/research\/people\/daan\/\" aria-label=\"Visit the profile page for Daan Leijen\">Daan Leijen<\/a>","is_active":false,"last_first":"Leijen, Daan","people_section":0,"alias":"daan"},{"type":"guest","value":"wouter-swierstra","user_id":"965010","display_name":"Wouter Swierstra","author_link":"Wouter Swierstra","is_active":true,"last_first":"Swierstra, Wouter","people_section":0,"alias":"wouter-swierstra"}],"msr_type":"Post","featured_image_thumbnail":"<img width=\"960\" height=\"540\" src=\"https:\/\/www.microsoft.com\/en-us\/research\/wp-content\/uploads\/2023\/09\/ICFP23-BlogHeroFeature-1400x788-1-960x540.jpg\" class=\"img-object-cover\" alt=\"FP2: Fully In-Place Functional Programming; ICFP 2023\" decoding=\"async\" loading=\"lazy\" srcset=\"https:\/\/www.microsoft.com\/en-us\/research\/wp-content\/uploads\/2023\/09\/ICFP23-BlogHeroFeature-1400x788-1-960x540.jpg 960w, https:\/\/www.microsoft.com\/en-us\/research\/wp-content\/uploads\/2023\/09\/ICFP23-BlogHeroFeature-1400x788-1-300x169.jpg 300w, https:\/\/www.microsoft.com\/en-us\/research\/wp-content\/uploads\/2023\/09\/ICFP23-BlogHeroFeature-1400x788-1-1024x576.jpg 1024w, https:\/\/www.microsoft.com\/en-us\/research\/wp-content\/uploads\/2023\/09\/ICFP23-BlogHeroFeature-1400x788-1-768x432.jpg 768w, https:\/\/www.microsoft.com\/en-us\/research\/wp-content\/uploads\/2023\/09\/ICFP23-BlogHeroFeature-1400x788-1-1066x600.jpg 1066w, https:\/\/www.microsoft.com\/en-us\/research\/wp-content\/uploads\/2023\/09\/ICFP23-BlogHeroFeature-1400x788-1-655x368.jpg 655w, https:\/\/www.microsoft.com\/en-us\/research\/wp-content\/uploads\/2023\/09\/ICFP23-BlogHeroFeature-1400x788-1-343x193.jpg 343w, https:\/\/www.microsoft.com\/en-us\/research\/wp-content\/uploads\/2023\/09\/ICFP23-BlogHeroFeature-1400x788-1-240x135.jpg 240w, https:\/\/www.microsoft.com\/en-us\/research\/wp-content\/uploads\/2023\/09\/ICFP23-BlogHeroFeature-1400x788-1-640x360.jpg 640w, https:\/\/www.microsoft.com\/en-us\/research\/wp-content\/uploads\/2023\/09\/ICFP23-BlogHeroFeature-1400x788-1-1280x720.jpg 1280w, https:\/\/www.microsoft.com\/en-us\/research\/wp-content\/uploads\/2023\/09\/ICFP23-BlogHeroFeature-1400x788-1.jpg 1400w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/>","byline":"Anton Lorenzen, <a href=\"https:\/\/www.microsoft.com\/en-us\/research\/people\/daan\/\" title=\"Go to researcher profile for Daan Leijen\" aria-label=\"Go to researcher profile for Daan Leijen\" data-bi-type=\"byline author\" data-bi-cN=\"Daan Leijen\">Daan Leijen<\/a>, and Wouter Swierstra","formattedDate":"September 12, 2023","formattedExcerpt":"This research paper was presented at the 28th ACM SIGPLAN International Conference on Functional Programming (opens in new tab) (ICFP), a premier forum for discussing design, implementations, principles, and uses of functional programming. Functional programming languages offer a host of advantages, such as ensuring memory&hellip;","locale":{"slug":"en_us","name":"English","native":"","english":"English"},"_links":{"self":[{"href":"https:\/\/www.microsoft.com\/en-us\/research\/wp-json\/wp\/v2\/posts\/964929","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.microsoft.com\/en-us\/research\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.microsoft.com\/en-us\/research\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.microsoft.com\/en-us\/research\/wp-json\/wp\/v2\/users\/42735"}],"replies":[{"embeddable":true,"href":"https:\/\/www.microsoft.com\/en-us\/research\/wp-json\/wp\/v2\/comments?post=964929"}],"version-history":[{"count":70,"href":"https:\/\/www.microsoft.com\/en-us\/research\/wp-json\/wp\/v2\/posts\/964929\/revisions"}],"predecessor-version":[{"id":967500,"href":"https:\/\/www.microsoft.com\/en-us\/research\/wp-json\/wp\/v2\/posts\/964929\/revisions\/967500"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.microsoft.com\/en-us\/research\/wp-json\/wp\/v2\/media\/964953"}],"wp:attachment":[{"href":"https:\/\/www.microsoft.com\/en-us\/research\/wp-json\/wp\/v2\/media?parent=964929"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.microsoft.com\/en-us\/research\/wp-json\/wp\/v2\/categories?post=964929"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.microsoft.com\/en-us\/research\/wp-json\/wp\/v2\/tags?post=964929"},{"taxonomy":"msr-research-area","embeddable":true,"href":"https:\/\/www.microsoft.com\/en-us\/research\/wp-json\/wp\/v2\/research-area?post=964929"},{"taxonomy":"msr-region","embeddable":true,"href":"https:\/\/www.microsoft.com\/en-us\/research\/wp-json\/wp\/v2\/msr-region?post=964929"},{"taxonomy":"msr-event-type","embeddable":true,"href":"https:\/\/www.microsoft.com\/en-us\/research\/wp-json\/wp\/v2\/msr-event-type?post=964929"},{"taxonomy":"msr-locale","embeddable":true,"href":"https:\/\/www.microsoft.com\/en-us\/research\/wp-json\/wp\/v2\/msr-locale?post=964929"},{"taxonomy":"msr-post-option","embeddable":true,"href":"https:\/\/www.microsoft.com\/en-us\/research\/wp-json\/wp\/v2\/msr-post-option?post=964929"},{"taxonomy":"msr-impact-theme","embeddable":true,"href":"https:\/\/www.microsoft.com\/en-us\/research\/wp-json\/wp\/v2\/msr-impact-theme?post=964929"},{"taxonomy":"msr-promo-type","embeddable":true,"href":"https:\/\/www.microsoft.com\/en-us\/research\/wp-json\/wp\/v2\/msr-promo-type?post=964929"},{"taxonomy":"msr-podcast-series","embeddable":true,"href":"https:\/\/www.microsoft.com\/en-us\/research\/wp-json\/wp\/v2\/msr-podcast-series?post=964929"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}