ArticleslgStudy

mathematics

Defunctionalization

Defunctionalization is a mathematics topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Defunctionalization rather than just read about it. In short: In programming languages, defunctionalization is a compile-time transformation which eliminates higher-order functions, replacing them by a single first-order apply function. The technique was first described by John C.

Key takeaways

  • Defunctionalization belongs to mathematics; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Defunctionalization to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Defunctionalization from memory before moving on to harder problems.

Reference excerpt

In programming languages, defunctionalization is a compile-time transformation which eliminates higher-order functions, replacing them by a single first-order apply function. The technique was first described by John C. Reynolds in his 1972 paper, "Definitional Interpreters for Higher-Order Programming Languages". Reynolds' observation was that a given program contains only finitely many function abstractions, so that each can be assigned and replaced by a unique identifier. Every function application within the program is then replaced by a call to the apply function with the function identifier as the first argument. The apply function's only job is to dispatch on this first argument, and then perform the instructions denoted by the function identifier on the remaining arguments. One complication to this basic idea is that function abstractions may reference free variables. In such situations, defunctionalization must be preceded by lambda lifting, so that any free variables of a function abstraction are passed as extra arguments to apply. In addition, if closures are supported as first-class values, it becomes necessary to represent these captured bindings by creating data structures. Instead of having a single apply function dispatch on all function abstractions in a program, various kinds of control-flow analysis (including simple distinctions based on arity or type signature) can be employed to determine which function(s) may be called at each function application site, and a specialized apply function may be referenced instead. Alternatively, the target language may support indirect calls through function pointers, which may be more efficient and extensible than a dispatch-based approach. Besides its use as a compilation technique for higher-order functional languages, defunctionalization has been studied (particularly by Olivier Danvy and collaborators) as a way of mechanically transforming interpreters into abstract machines. Defunctionalization is also related to the technique from object-oriented programming of representing functions by function objects (as an alternative to closures).

Example The following is a Haskell translation of an example due to Olivier Danvy. Consider the Tree datatype and the following program.

Defunctionalization replaces all higher-order functions (in this case, o is the only higher-order function) with a value of the Lam datatype. Instead of calling higher-order functions directly, it introduces an apply function that interprets the Lam datatype.

See also Closure conversion Partial evaluation

References Reynolds, John (August 1972). "Definitional Interpreters for Higher-Order Programming Languages". Proceedings of the ACM Annual Conference. Boston, Massachusetts. pp. 717–740. doi:10.1145/800194.805852. Danvy, Olivier; Nielsen, Lasse R. (2001). "Defunctionalization at Work" ( (PDF)). Proceedings of the ACM SIGPLAN Conference on Principles and Practice of Declarative Programming. pp. 162–174. doi:10.1145/773184.773202. Archived from the original on 2011-09-27. Retrieved 2011-08-24. (More comprehensive version: Technical Report BRICS-RS-01-23) Danvy, Olivier; Millikin, Kevin R. (June 2009). "Refunctionalization at Work". Science of Computer Programming. 74 (8): 534–549. doi:10.1016/j.scico.2007.10.007. (Also available as Technical Report BRICS-RS-07-7)

External links Defunctionalization (Programming Languages). Oxford University.

Worked examples

Example 1 — a first encounter with Defunctionalization

Start with the simplest possible case. Write down what Defunctionalization claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In mathematics, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Defunctionalization before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Defunctionalization ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Defunctionalization

In research
Defunctionalization appears in mathematics research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Defunctionalization in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Defunctionalization is common in secondary-school and first-year university syllabi. It links to neighbouring topics Implementation of functional programming languages, so understanding it makes those chapters shorter.
In everyday life
Look for Defunctionalization outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Defunctionalization” →

How to study Defunctionalization in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Defunctionalization means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Defunctionalization out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Defunctionalization in simple terms?

In programming languages, defunctionalization is a compile-time transformation which eliminates higher-order functions, replacing them by a single first-order apply function. The technique was first described by John C.

Why does Defunctionalization matter?

Because it connects several mathematics ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Defunctionalization?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Defunctionalization.

Tags

  • Implementation of functional programming languages

Keep exploring