<p><a href="https://en.wikipedia.org/wiki/Defunctionalization" rel="nofollow noreferrer">Defunctionalisation</a> refers to program transformations which replace first-class functions and closures with conventional data structures (e.g. structs and enums), so that higher-order functions become regular functions.</p>
<p>What are the use-cases for this technique in a compiler?</p>

<compilers><program-transformation>
What is defunctionalisation useful for?
<p>There was a great talk and accompanying post on exactly this topic by Jimmy Koppel:</p>
<ul>
<li><a href="https://www.pathsensitive.com/2019/07/the-best-refactoring-youve-never-heard.html" rel="noreferrer">The Best Refactoring You've Never Heard Of</a></li>
<li><a href="https://blog.sigplan.org/2019/12/30/defunctionalization-everybody-does-it-nobody-talks-about-it/" rel="noreferrer">Defunctionalization: Everybody Does It, Nobody Talks About It</a></li>
</ul>
<p>In a nutshell, you replace higher-order data (higher-order functions) with first-order data, thus you get all the usual benefits of first-order data:</p>
<ul>
<li>takes up less space (or can be compressed)</li>
<li>easier to optimize</li>
<li>can be serialized, and thus stored on a disk or sent over a network</li>
</ul>
<p>The problem being in general case defunctionalization requires a whole-program analysis, which is extremely slow for programs of considerable size. But it can work in cases where all the functions involved are encapsulated in a single module.</p>
<p>As Koppel points out, one of the most useful use-cases for defunctionalization is in conjunction with <a href="https://en.wikipedia.org/wiki/Continuation-passing_style" rel="noreferrer">continuation-passing style</a>. With both transformations together a compiler can produce (effectively) a <a href="https://en.wikipedia.org/wiki/Finite-state_machine" rel="noreferrer">state machine</a>, which is useful for iterators/generators, async/await, coroutines and similar constructs.</p>

CC BY-SA 4.0
2023-05-24T06:44:33.317Z
2023-05-24T09:59:26.870Z
2023-05-24T09:59:26.870Z
Alex Chichigin
# What is defunctionalisation useful for?
Posted by: username_0 on 2023-05-23
[Defunctionalisation](https://en.wikipedia.org/wiki/Defunctionalization) refers to program transformations which replace first-class functions and closures with conventional data structures (e.g. structs and enums), so that higher-order functions become regular functions.

What are the use-cases for this technique in a compiler?
**Tags:** compilers program-transformation
---
## 1 answers
### Answer by username_1 on 2023-05-24. Score: 8 | Accepted answer
There was a great talk and accompanying post on exactly this topic by Jimmy Koppel:

* [The Best Refactoring You've Never Heard Of](https://www.pathsensitive.com/2019/07/the-best-refactoring-youve-never-heard.html)
* [Defunctionalization: Everybody Does It, Nobody Talks About It](https://blog.sigplan.org/2019/12/30/defunctionalization-everybody-does-it-nobody-talks-about-it/)

In a nutshell, you replace higher-order data (higher-order functions) with first-order data, thus you get all the usual benefits of first-order data:

* takes up less space (or can be compressed)
* easier to optimize
* can be serialized, and thus stored on a disk or sent over a network

The problem being in general case defunctionalization requires a whole-program analysis, which is extremely slow for programs of considerable size. But it can work in cases where all the functions involved are encapsulated in a single module.

As Koppel points out, one of the most useful use-cases for defunctionalization is in conjunction with [continuation-passing style](https://en.wikipedia.org/wiki/Continuation-passing_style). With both transformations together a compiler can produce (effectively) a [state machine](https://en.wikipedia.org/wiki/Finite-state_machine), which is useful for iterators/generators, async/await, coroutines and similar constructs.
---
