Opportunistically Parallel Lambda Calculus

Stephen Mell, Κωνσταντίνος Καλλάς, Steve Zdancewic, Osbert Bastani

TL;DR

This paper introduces OPAL, a new scripting language that automatically parallelizes and streams external calls (like LLM API requests) without requiring explicit programmer intervention. It achieves significant speedups by intelligently dispatching independent calls and processing partial results, making complex LLM workflows much faster and more efficient.

What Problem Does This Paper Address?

Modern applications increasingly rely on "glue" scripts to orchestrate calls to external services, such as large language models (LLMs) or native libraries. The performance bottleneck in these scripts often lies not in the script's logic itself, but in the slow, opaque external calls. Traditional optimization techniques struggle here because these external calls can be time-consuming and their internal workings are unknown to the scripting language. While developers can manually introduce parallelism and streaming, this often leads to complex, error-prone code, undermining the simplicity of scripting languages. The paper addresses the challenge of automatically exploiting parallelism and streaming opportunities in such scripts, especially those involving LLMs, which are notorious for their long execution times and streaming capabilities.

Methods

The core of the solution is a novel lambda calculus called λº (lambda-zero) and an opportunistic evaluation strategy.

  • λº Calculus: This calculus explicitly models external calls as first-class citizens. It uses a "let-binding" style (similar to Abstract Normal Form) and introduces "task placeholders" for ongoing external calls.
  • Opportunistic Evaluation Strategy: Unlike strict (call-by-value) or lazy evaluation, this strategy aims to execute any available step, regardless of order, as long as it doesn't violate dependencies. When an external call is encountered, it's dispatched (becoming a task placeholder), and the program continues executing other available internal computations or even other external calls. This allows for parallel execution and partial results to be processed as they become available (streaming).
  • OPAL Language: Built on λº, OPAL adds standard control flow constructs and uses Church encodings for data structures (like lists) and control flow. This allows these constructs to automatically benefit from the opportunistic evaluation. Church encodings are particularly useful for representing "partial data," enabling streaming of results that aren't fully computed yet.
  • Implementation: OPAL is implemented as a Python framework, allowing users to write scripts using a subset of Python syntax and designate Python functions (including ML libraries) as external calls.

Datasets

The paper does not discuss training datasets. For evaluation, it uses five benchmark programs representative of real-world LLM scripting tasks:

  • CityExcursions: The motivating example from the paper, involving fetching cities and then excursions for each.
  • TextToSpeech: Concatenates LLM calls with an external text-to-speech tool.
  • FactCheck: Implements a fact-checker using LLMs and a Wikipedia lookup tool.
  • TreeofThoughts: An LLM reasoning approach that treats problem-solving as a graph search.
  • JSONDec: Uses LLMs with constrained decoding to enforce JSON output.

These benchmarks were run using a record-replay system to mitigate timing variance from external APIs.

Key Insights and Ideas

  • External Calls as First-Class Citizens: Explicitly modeling external calls within the calculus is crucial for enabling automatic parallelization and streaming.
  • Opportunistic Evaluation is Key: The core innovation is an evaluation strategy that doesn't wait unnecessarily. By dispatching external calls as soon as possible and processing partial results, it unlocks parallelism that strict or lazy evaluation miss.
  • Church Encodings for Streaming: Using Church encodings for data structures like lists provides a natural way to represent and manipulate "partial data," which is essential for effective streaming of LLM outputs.
  • Automatic Parallelism Without Code Changes: The most significant benefit is that developers can write scripts as if they were sequential, and the runtime automatically handles parallelization and streaming, greatly simplifying development.
  • LLM Scripts as a Prime Target: The inherent latency and streaming capabilities of LLMs make them an ideal domain for demonstrating the benefits of this approach.

Key Results

  • Significant Speedups: OPAL achieved substantial improvements over standard sequential Python:
  • Latency: Up to 12.7x improvement.
  • Running Time: Up to 6.2x improvement.
  • Competitive with Manual Optimization: OPAL's performance was very close to hand-tuned, manually optimized asynchronous Rust implementations, with only a small overhead (1.3% to 18.5%).
  • Tree-of-Thoughts Improvement: For the Tree-of-Thoughts benchmark, OPAL achieved a 6.2x performance improvement over the authors' original Python implementation.
  • Comparison to Baselines: OPAL generally outperformed or was competitive with other frameworks like bash, PaSh, Flink, and SGLang, especially in scenarios requiring fine-grained parallelism and streaming. Flink, for instance, struggled with "streams of streams" scenarios that OPAL handled well.

Limitations and Future Work

  • Scope of Opportunistic Evaluation: The paper notes that OPAL could be "more opportunistic." For example, it doesn't automatically parallelize if b then f(x) else f(y) as effectively as f(if b then x else y), because the former requires b to be evaluated first. Expanding this scope is a future direction.
  • Frontend Support: OPAL currently has limited frontend support for common Python constructs like loops, requiring them to be encoded using functional folds. Compiling a larger subset of Python would improve usability.
  • Sequencing Correctness: While OPAL uses data dependencies to enforce sequencing for external calls (like printing), the correctness of this sequencing isn't automatically checked. The authors suggest adding types and a notion of linearity to improve this.
  • Performance Gap with Manual Implementations: While OPAL is close to manual Rust, there's still a gap. Improving streaming performance, particularly by developing more efficient partial value representations beyond Church encodings for strings, is a key area for future work.

Summary updated

BibTeX
@inproceedings{mell2025opportunistically,
  author = {Stephen Mell and Κωνσταντίνος Καλλάς and Steve Zdancewic and Osbert Bastani},
  title = {{Opportunistically Parallel Lambda Calculus}},
  booktitle = {Proceedings of the ACM on Programming Languages},
  year = {2025},
  doi = {10.1145/3763143},
}
Figure