Papers
Topics
Authors
Recent
Search
2000 character limit reached

Optimizing Optimizations, Declaratively: Optimizing the Higher-Order Functions in Mathematical Optimization with egglog

Published 18 May 2026 in cs.PL, cs.MS, and math.OC | (2605.17884v1)

Abstract: We present two applications of egglog to mathematical optimization in JijModeling 2, a mathematical modeller whose internal representation is based on simply typed $λ$-calculus. First, we use egglog to improve $\LaTeX$ output for mathematical models expressed with higher-order functions. Python comprehensions are desugared into stream operations such as $\textsf{map}$, $\textsf{flat_map}$, and $\textsf{filter}$; emitting these terms directly produces unnatural mathematical notation. We reconstruct comprehension syntax by \emph{ensugaring} higher-order terms and use equality saturation with a custom cost model to minimize temporary variable rebindings. Second, we use egglog as a declarative engine for \emph{constraint detection}, extending the previous egg-based approach presented at EGRAPHS '25. Egglog's datalog-style rules let us express multi-step detection logic directly, without external Rust orchestration code. We encode parametrized constraints using \emph{Henkin-like constants} and propagate side conditions on subterms and indices through egglog facts. Finally, we show that the same ensugaring procedure also reduces large domain-set conditions before saturation, turning a problematic detection case from minutes or nontermination into a few seconds. Through these topics, we want to provide an example of an industrial application of egglog, demonstrate the trick to propagate the constraints using the ideas from mathematical logic, and show the importance of optimizing \emph{premises} of egglog rules to get practical performance in egglog programs.

Authors (1)

Summary

  • The paper introduces a declarative optimization framework that leverages egglog to enhance symbolic reasoning and generate human-readable LaTeX from higher-order models.
  • It details an ensugaring transformation that refactors complex Python-derived abstract syntax trees into canonical mathematical notation using equality saturation and custom cost models.
  • The study demonstrates efficient, automated constraint detection in parameterized model families, achieving orders-of-magnitude runtime improvements and streamlined solver selection.

Declarative Optimization of Higher-Order Mathematical Models with Egglog

Overview

"Optimizing Optimizations, Declaratively: Optimizing the Higher-Order Functions in Mathematical Optimization with egglog" (2605.17884) presents a systematic account of leveraging the egglog system as a declarative, equality saturation-based engine for two core challenges in a modern, λ\lambda-calculus-based mathematical modeling tool (JijModeling 2): (1) reconstructing readable \LaTeX{} notation from higher-order internal representations originating from Python comprehensions, and (2) declaratively detecting constraint patterns within parameterized model families to facilitate automated solver selection and presolving. The work elaborates on the representation choices, transformation pipeline, and computational consequences of applying equality saturation in this context. The practical significance is established through analyses of numerical performance improvements, and the theoretical aspects invite further work in formalizing correctness and extending integration with richer logical analysis.

Higher-Order Syntax and Egglog Representation

JijModeling 2 internalizes mathematical programming constructs as abstract syntax trees rooted in a simply typed λ\lambda-calculus, supporting higher-order constructs such as λ\lambda-abstractions and bound variable handling. The translation into egglog is near-direct at the datalog AST level, using a locally named representation for variables (with de Bruijn-like offsets and unique nonces), sidestepping full α\alpha- or β\beta-equivalence, which is sufficient for practical program analyses. This representation enables symbolic manipulation prior to data instantiation, allowing optimizations and analyses that scale with the symbolic problem size rather than with concrete instantiations, which can be orders of magnitude larger.

Ensugaring for Human-Readable \LaTeX{} Output

The presence of higher-order constructs—resulting from systematic desugaring of Python comprehensions into chains of functional stream operators—hinders the production of readable LaTeX output when directly pretty-printed from the AST. The authors introduce an "ensugaring" transformation, which, via egglog rules, reconstructs classical mathematical comprehension syntax from arbitrary nestings of map\mathsf{map}, flat_map\mathsf{flat\_map}, and filter\mathsf{filter}. An optimal form (with minimal rebinding and temporary variable introductions) is chosen using custom cost models in equality saturation, extracting the canonical, user-friendly presentation.

Practically, this approach achieves sub-5-second transformation times for nontrivial problem instances (e.g., 50 variables, 30 constraints), with cost-based extraction favoring minimal intermediate bindings and lexicographically minimal variable indices. Further database functionality in egglog is used to postprocess summation ranges and integrate user-specified variable presentations, enhancing output clarity.

Declarative Constraint Detection with Side-Condition Propagation

The second major application concerns the declarative detection of constraint classes (especially those spanning parameterized variable families, such as SOS1 constraints) inside higher-order model specifications. Egglog's datalog rules permit the propagation and combination of symbolic facts and conditions using named bound variables ("Henkin-like" constants) as domain witnesses for parameterized constraints. This enables multi-step pattern detection (e.g., detecting SOS1 constraints linked via upper-bound parametric conditions) to be expressed and executed entirely at the egglog level, obviating prior multi-stage orchestration approaches that required round-tripping between egg and host Rust logic.

The adopted approach encodes parameterized constraints and their side conditions as egglog facts over named bound variables, then reconstructs detected constraint classes for downstream model specialization and solver API selection. The methodology is robust to cases where the domain may be empty, as existential facts are interpreted as schematic and only instantiated as concrete constraints during model lowering.

Performance Implications and Rule Premise Optimization

A crucial observation is made regarding the scaling behavior of egglog in the presence of complex, deeply nested domain set conditions (example: Deep Space Network Scheduling). When such domains are left in unsimplified nestings of higher-order stream combinators, egglog exhibits nontermination or impractical runtimes due to pre-saturation rule expansion bottlenecks. Application of the previously described ensugaring machinery to the domain set conditions—prior to equality saturation—breaks them into smaller, independent conjuncts. This refactoring yields orders-of-magnitude speedups (e.g., reducing multi-minute or nonterminating cases to a few seconds). However, the authors provide explicit experimental evidence that while further variable rebinding optimization is generally beneficial, it can sometimes be counterproductive, necessitating selective application.

Theoretical and Practical Implications, Future Work

The declarative approach described fundamentally shifts the expression and realization of symbolic analyses within industrial mathematical modeling frameworks. By using egglog as both an e-graph saturation engine and a datalog-style relational fact database, the system demonstrates the viability of large-scale, model-parameterized symbolic reasoning, with direct applications to code generation, model analysis, and automatic constraint exploitation.

The paper identifies several open avenues: (1) formal proof of the soundness of the Henkin-style constant mechanism for constraint detection (currently justified by empirical evidence and bounded domain reasoning), (2) incorporation of interval or affine arithmetic into bound analysis for richer side condition propagation, and (3) direct utilization of the egglog infrastructure as a backend for SMT-like equality reasoning in forthcoming dependent type-checkers for mathematical programs.

Conclusion

This work presents a precise, methodical application of egglog to problems in higher-order mathematical modeling, making strong technical contributions in the declarative optimization of program representations for both output rendering and symbolic analysis. The reported numerical advantages, particularly in model analysis runtime, substantiate the proposed pipeline. The integration of equality saturation, custom cost models, and rule-premise restructuring within an e-graph-based workflow provides a template for future developments in symbolic AI infrastructure for mathematical optimization and program reasoning.

Paper to Video (Beta)

No one has generated a video about this paper yet.

Whiteboard

No one has generated a whiteboard explanation for this paper yet.

Open Problems

We haven't generated a list of open problems mentioned in this paper yet.

Collections

Sign up for free to add this paper to one or more collections.

Tweets

Sign up for free to view the 3 tweets with 3 likes about this paper.