In-Context Universal Approximation, Compositional Generalization, and Algorithm Emulation

Jerry Yao-Chieh Hu, Hong-Yu Chen, Po-Chiao Lin, Maojiang Su, Han Liu

Introduction

This paper establishes a profound theoretical foundation for Large Language Models: a single, frozen-weight Transformer can approximate any continuous sequence-to-sequence function simply by changing its prompt. By treating prompts as "programs" and the Transformer as an "interpreter," the authors demonstrate that models can not only execute specific algorithms in-context but also assemble complex logic by chaining simpler prompt-based subroutines together.

The Problem: Beyond Recall and Implicit Training

In-Context Learning (ICL) is the mechanism that allows Large Language Models (LLMs) to solve novel tasks without any weight updates. Traditionally, researchers have explained this phenomenon through two primary lenses:

  1. Parametric Recall: The model recognizes a task it encountered during training and activates a pre-learned predictor.
  2. Amortized Inference: The model treats the prompt as a dataset and implicitly runs a learning algorithm (such as Gradient Descent) during its forward pass.

However, these theories fall short of explaining the "prompt-programmed" regime—where a user provides a brand-new procedure or a set of rules, and the model executes them flawlessly. While existing proofs for this capability often rely on simplified versions of attention (like linear or hardmax attention), this paper tackles the challenge of proving that standard softmax attention Transformers are universal computers capable of emulating other algorithms and composing them on the fly using only the prompt.

Methodology: In-Context Emulation

Rather than proving universality from scratch, the authors employ a strategy called In-Context Emulation. They demonstrate that a fixed-weight Transformer can "simulate" other known universal approximators.

  • Weight Encoding: The authors develop a method to serialize the weights of a "target" neural network (the algorithm to be executed) directly into the prompt. This is achieved using a "Token-Indexed Vector Encoding" that allows the Transformer's attention heads to recover linear forms of the target weights.
  • The Emulator: They construct a fixed set of weights ($\theta^\star$) for a Transformer that acts as a program interpreter. This "Emulator Transformer" uses its attention layers to read the encoded weights from the prompt and apply them to the input data.
  • Two Routes to Universality: The paper provides two constructive proofs for this emulation:
  • Contextual Mapping: Emulating a Transformer consisting of Feed-Forward (FFN) and Attention layers.
  • Attention-as-Selector: Emulating an attention-only surrogate that acts as an interpolator.

Compositional Generalization

A major breakthrough in the paper is the formalization of modular assembly. The "Composition Theorem" suggests that LLMs exhibit "compositional generalization" because they can treat prompt blocks as reusable modules. If a model knows how to execute functions $f$ and $g$ individually, it can be prompted to execute $g(f(x))$ without needing new training.

The authors establish two mechanisms to chain these functions:

  • One-Pass: A deep model executes a sequence of instructions in a single forward pass.
  • Multi-Pass: The same model is re-prompted iteratively, where the output of one step becomes the input for the next.

Experimental Validation

While primarily a theoretical paper, the authors include a proof-of-concept experiment to validate their mathematical claims. They created a library of 50 tokenwise 2-layer ReLU MLPs as primitive functions. The task involved a 4-function composition (e.g., $f_4 \circ f_3 \circ f_2 \circ f_1(X)$).

By testing the model on held-out composition orders—combinations the model never saw during training—they ensured the model was genuinely "reading the program" rather than merely memorizing sequences. The model achieved low Mean Squared Error (MSE) on these held-out tasks. Crucially, the error decreased as the number of attention heads increased, confirming that model complexity directly limits the "resolution" of the emulated program.

Key Results

  • Universal Approximation (Theorems 3.1 & 3.2): For any $L$-Lipschitz function and any desired accuracy $\epsilon$, there exists a prompt $P_f$ that makes a fixed Transformer $T_{\theta^\star}$ approximate that function.
  • Compositionality (Theorems 4.1 & 4.2): An $8m$-layer Transformer can execute an $m$-fold composition in one pass, or a fixed 8-layer model can achieve the same via $m$ iterative calls.
  • Softmax is Sufficient: Unlike previous work that required "hardmax" attention to make the math work, this paper proves that standard, smooth softmax attention can achieve arbitrary precision if the "inverse temperature" (sharpness) is handled correctly.

Limitations and Future Directions

Despite these strong theoretical guarantees, the framework has limitations:

  • The Curse of Dimensionality: The proofs indicate that the required embedding dimension and head count grow exponentially with the granularity needed for the approximation. Emulating highly complex functions requires massive models.
  • Construction vs. Learning: The paper provides a constructive proof (proving the weights exist), but it does not guarantee that standard pretraining (like next-token prediction on internet text) will naturally "find" these specific emulator weights.

Future work will explore extending this framework to Chain-of-Thought (CoT) settings. By allowing the model to use intermediate tokens as a "scratchpad," its computational power could be further increased, enabling the emulation of even more complex algorithms.

Summary updated

BibTeX
@inproceedings{hu2026context,
  author = {Jerry Yao-Chieh Hu and Hong-Yu Chen and Po-Chiao Lin and Maojiang Su and Han Liu},
  title = {{In-Context Universal Approximation, Compositional Generalization, and Algorithm Emulation}},
  booktitle = {International Conference on Machine Learning},
  year = {2026},
  url = {https://openreview.net/forum?id=ZK0U30zmDY},
}
Figure