Table of Contents
Fetching ...

Opportunistically Parallel Lambda Calculus

Stephen Mell, Konstantinos Kallas, Steve Zdancewic, Osbert Bastani

TL;DR

This work tackles the inefficiency of glue scripts that perform many external calls by introducing opportunistic evaluation, a confluence-preserving, out-of-order execution model based on a core calculus λ^O. Building Opal atop λ^O, it enables automatic parallelization and streaming of external calls through Church-encoded data structures and a nondeterministic yet convergent operational semantics. The paper contributes a formal calculus (λ^O) with dispatch/resolution semantics, an Opal language that supports partial data and streaming, and a practical Python-based implementation evaluated on real-world LLM scripts, achieving substantial latency and running-time improvements over sequential baselines and comparable performance to hand-optimized Rust. Key findings include up to 12.7x latency and 6.2x total-time improvements versus sequential Python, with modest overhead relative to optimized implementations, and strong confluence guarantees ensuring programmer intent is preserved. The work suggests broad practical impact for automating parallelism in scripting, particularly for API- and LLM-driven workloads, while identifying avenues for improving streaming efficiency and sequencing guarantees in future work.

Abstract

Scripting languages are widely used to compose external calls such as native libraries and network services. In such scripts, execution time is often dominated by waiting for these external calls, rendering traditional single-language optimizations ineffective. To address this, we propose a novel opportunistic evaluation strategy for scripting languages based on a core lambda calculus that automatically dispatches independent external calls in parallel and streams their results. We prove that our approach is confluent, ensuring that it preserves the programmer's original intent, and that it eventually executes every external call. We implement this approach in a scripting language called Opal. We demonstrate the versatility and performance of Opal, focusing on programs that invoke heavy external computation through the use of large language models (LLMs) and other APIs. Across five scripts, we compare to several state-of-the-art baselines and show that opportunistic evaluation improves total running time (up to $6.2\times$) and latency (up to $12.7\times$) compared to standard sequential Python, while performing very close (between $1.3\%$ and $18.5\%$ running time overhead) to hand-tuned manually optimized asynchronous Rust. For Tree-of-Thoughts, a prominent LLM reasoning approach, we achieve a $6.2\times$ performance improvement over the authors' own implementation.

Opportunistically Parallel Lambda Calculus

TL;DR

This work tackles the inefficiency of glue scripts that perform many external calls by introducing opportunistic evaluation, a confluence-preserving, out-of-order execution model based on a core calculus λ^O. Building Opal atop λ^O, it enables automatic parallelization and streaming of external calls through Church-encoded data structures and a nondeterministic yet convergent operational semantics. The paper contributes a formal calculus (λ^O) with dispatch/resolution semantics, an Opal language that supports partial data and streaming, and a practical Python-based implementation evaluated on real-world LLM scripts, achieving substantial latency and running-time improvements over sequential baselines and comparable performance to hand-optimized Rust. Key findings include up to 12.7x latency and 6.2x total-time improvements versus sequential Python, with modest overhead relative to optimized implementations, and strong confluence guarantees ensuring programmer intent is preserved. The work suggests broad practical impact for automating parallelism in scripting, particularly for API- and LLM-driven workloads, while identifying avenues for improving streaming efficiency and sequencing guarantees in future work.

Abstract

Scripting languages are widely used to compose external calls such as native libraries and network services. In such scripts, execution time is often dominated by waiting for these external calls, rendering traditional single-language optimizations ineffective. To address this, we propose a novel opportunistic evaluation strategy for scripting languages based on a core lambda calculus that automatically dispatches independent external calls in parallel and streams their results. We prove that our approach is confluent, ensuring that it preserves the programmer's original intent, and that it eventually executes every external call. We implement this approach in a scripting language called Opal. We demonstrate the versatility and performance of Opal, focusing on programs that invoke heavy external computation through the use of large language models (LLMs) and other APIs. Across five scripts, we compare to several state-of-the-art baselines and show that opportunistic evaluation improves total running time (up to ) and latency (up to ) compared to standard sequential Python, while performing very close (between and running time overhead) to hand-tuned manually optimized asynchronous Rust. For Tree-of-Thoughts, a prominent LLM reasoning approach, we achieve a performance improvement over the authors' own implementation.
Paper Structure (57 sections, 8 theorems, 3 equations, 11 figures, 1 table)

This paper contains 57 sections, 8 theorems, 3 equations, 11 figures, 1 table.

Key Result

proposition 1

$p_0 \Rightarrow \ldots \Rightarrow p_n$ iff there exists $T$ such that $p_0 \Rightarrow_T \ldots \Rightarrow_T p_n$.

Figures (11)

  • Figure 1: A conceptual demonstration of how our approach achieves automatic parallelization and streaming for the motivating example program. Our approach is based on a rewrite-syle operational semantics, involving three kinds of steps: reduction steps are ordinary rewrites, internal to the term, as in lambda calculus; dispatch steps occur when an external call is made, replacing the call with a placeholder TASK (function $\Rightarrow$ task); resolution steps occur when an external call returns and the TASK is replaced by an expression (task $\Rightarrow$ expression). The captions indicate what step led to that term. The initial term is shown in (a) and the final term in (l). Prints that have been performed are shown with a comment (#).
  • Figure 2: The grammar of $\lambda^O$ (a). The motivating example from Section \ref{['sec:motivating']}, in the formal grammar of $\lambda^O$ (b) and in sugared form (c). The example is well-formed in contexts containing cities_in, excursion_in, fold, print, and stdout.
  • Figure 3: The grammars for statement labels $\ell$, labeled expressions $p$, and evaluation contexts $E$. $i$ ranges over natural numbers.
  • Figure 4: The operations of replacing the hole in an evaluation context $E$ with a single statement $E\left\lbrack{\cdot}\right\rbrack$ (a), and with a list of statements $E\left\lbrack\space\left\lbrack{\cdot}\right\rbrack\space\right\rbrack$ (b).
  • Figure 5: The operational semantics of $\lambda^O$, ${p} \Rightarrow_{} {p'}$, stepping from $p$ to $p'$, is defined as the union of ${} \Rightarrow^S_{} {}$ (a), ${} \Rightarrow^D_{} {}$ (b), and ${} \Rightarrow^R_{} {}$ (c). We write ${p} \Rightarrow_{}^{\ell} {p'}$ if $p = E\left\lbrack{s}\right\rbrack$, $p' = E\left\lbrack\space\left\lbrack{\vec{s}}\right\rbrack\space\right\rbrack$, and $\ell = \operatorname{ident}({E})$, i.e. $(\ell : []) \in E$.
  • ...and 6 more figures

Theorems & Definitions (8)

  • proposition 1
  • proposition 2: Determinacy
  • proposition 3: Preservation
  • proposition 4: Statement Replacement Commutes
  • proposition 5: Strong Confluence
  • proposition 6: Fairness
  • proposition 7
  • proposition 8: Soundness