JijModeling 2: Declarative Egglog Optimization
- JijModeling 2 is a mathematical modeller that uses a simply typed λ‐calculus augmented with stream operations to represent higher-order expressions.
- The system applies an egglog ensugaring pass to reconstruct conventional mathematical notation from nested stream operations, improving readability.
- It employs cost-guided equality saturation and declarative Datalog-style rules to optimize constraint detection, notably for SOS1 constraints.
Searching arXiv for the specified paper and closely related JijModeling/egglog work. JijModeling 2 is a mathematical modeller whose internal representation is based on simply typed -calculus and whose optimization pipeline uses egglog for two distinct purposes: improving output for higher-order mathematical expressions and implementing declarative constraint detection for structures such as SOS1 constraints (Ishii, 18 May 2026). In this setting, Python comprehensions are desugared into stream operations such as , , and , while egglog is used both to reconstruct conventional mathematical notation by “ensugaring” these higher-order terms and to express multi-step detection logic directly through Datalog-style rules. The resulting system is presented as an industrial application of egglog that emphasizes the practical importance of optimizing the premises of egglog rules and of propagating constraints by means of ideas drawn from mathematical logic (Ishii, 18 May 2026).
1. Core representation and symbolic architecture
JijModeling 2’s core abstract syntax tree is a small simply-typed -calculus extended with primitives for arithmetic, arrays, dictionaries, sets, and stream operations. Its type grammar is given by
$\tau ::= A \mid \tau_1 \to \tau_2 \quad\text{(base types %%%%6%%%% include %%%%7%%%%).}$
Terms are generated by the usual constructors,
where ranges over set or stream expressions (Ishii, 18 May 2026).
In egglog, this AST is encoded through datatypes including FVar, BVar, Lam, App, Let, Map, FlatMap, and Filter. Bound variables use a “locally-named” representation in which each bound variable carries an outer-scope offset and a globally unique nonce. This representation is directly relevant to both pretty-printing and constraint detection, because higher-order constructs and reductions must preserve binding structure while still supporting rewrite-based optimization.
A closely related architectural principle already appeared in earlier JijModeling work: symbolic representation is separated from input data, so the rewrite-based detector operates on a small symbolic tree and remains independent of numerical arrays until model instantiation (Ishii et al., 2 Jun 2025). This suggests that JijModeling 2 inherits a broader design objective from earlier versions: keep symbolic reasoning compact while deferring data-dependent expansion.
2. Ensugaring higher-order terms into mathematical notation
A central motivation for JijModeling 2 is that direct emission of higher-order stream terms yields unnatural mathematical notation. In user code, a generator expression such as
3
is desugared at parse time into nested stream operations: JijModeling 2 then applies an egglog “ensugaring” pass to reconstruct a conventional comprehension form (Ishii, 18 May 2026).
The key rewrite rules are presented as comprehension-building transformations:
- Comp-Map:
0
- Comp-FlatMap:
1
- Comp-Filter:
2
Together these rules build a comprehension AST with let-clauses for intermediate bindings. The target notation is the conventional nested summation form
3
The significance of this transformation is not merely cosmetic. The paper explicitly treats higher-order mathematical notation as an optimization target in its own right: declarative rewriting is used to recover human-readable mathematical structure from a lower-level stream encoding (Ishii, 18 May 2026). A plausible implication is that readability is being handled with the same formal machinery ordinarily reserved for algebraic normalization or solver preprocessing.
3. Equality saturation and cost-guided extraction
Naïve ensugaring can still produce many fresh variables and nested let expressions. JijModeling 2 therefore uses egglog’s e-graph rewriting together with a custom cost model to minimize temporary variable rebinding. The cost assignment is:
- cost(let-binding) 4
- cost of a bound variable 5 6
- cost of any other node 7
In 8 form,
9
Saturation is performed under three classes of rewrites: basic structural rewrites such as commuting nested lets, collapsing identical 0 via side-condition facts, and inlining trivial bindings at the end. The minimum-cost representative is then extracted from the saturated e-graph (Ishii, 18 May 2026).
This choice of objective is narrowly targeted. Rather than optimizing algebraic complexity or proof length, the extraction process minimizes artifacts of desugaring itself: temporary bindings and deep variable offsets. The outcome is described as a human-readable comprehension with the fewest temporaries. For small models, printing is reported to occur almost instantly, while a 50-variable, 30-constraint instance takes approximately 1 seconds in the 2 pass (Ishii, 18 May 2026).
An important conceptual point follows from the same design. The extraction criterion is not a generic syntactic preference but a domain-specific measure of mathematical presentability. This suggests a broader role for equality saturation in modeling systems: not only proving equivalence classes, but selecting representatives optimized for expert-facing notation.
4. Declarative constraint detection in egglog
JijModeling 2 also uses egglog as a Datalog-style engine for detecting higher-level constraints such as SOS1 and network structures without external orchestration code (Ishii, 18 May 2026). The paper’s illustrative example concerns SOS1 detection in two stages.
The first stage is a binary SOS1 witness over 3: 4
The second stage is a family of linking upper bounds: 5
Egglog rules are then written in a direct Datalog-like form. Rule 1 marks a unary sum bounded by 6 over binary variables as sos1:
4
Rule 2 lifts detection from the binary witness 7 to the real variable family 8:
5
To implement quantified “for all 9”, the system introduces a fresh Henkin-like constant 0 for each rule instance, registers its side conditions as unconditional facts in the e-graph database, and allows egglog’s bottom-up engine to derive (sos1 … x[i_0]). The internal facts include
1
After deriving (sos1 Conds x[i_0]), the system reinterprets that result as a schematic SOS1 constraint over the possibly empty domain 2 (Ishii, 18 May 2026).
The main technical significance is that multi-stage detections run entirely inside egglog in a single saturation pass because side conditions are themselves facts and each pattern is a single Datalog rule. This directly addresses a limitation noted in the earlier e-graph-based system, where composite SOS constraints required two passes of pattern matching and the roadmap explicitly proposed migration to egglog so that such composite detection rules could be expressed as Horn clauses (Ishii et al., 2 Jun 2025).
5. Performance characteristics and the role of premise optimization
The reported measurements distinguish two phases: 3 ensugaring with optimization for small models, and SOS1 detection on a pathological Deep-Space-Network benchmark with seven nested comprehensions in the domain-set condition (Ishii, 18 May 2026).
| Benchmark | w/o ensugaring | with ensugaring |
|---|---|---|
| Small model (50×30) | 5.0 s (LaTeX pass) | 5.0 s |
| DSN SOS1 detection | 4 s | 3.0 s |
For smaller 5 workloads, the paper also reports:
- 10 vars, 5 constraints: 6 s
- 50 vars, 30 constraints: 7 s
The most notable result concerns the pathological detection case. A naïve encoding as a single membership over a huge nested stream led to non-termination or more than 8 minutes, whereas ensugaring the membership with the same comprehension rules reduced execution to 9 seconds (Ishii, 18 May 2026). The paper explicitly frames this as evidence that optimizing the premises of egglog rules is essential for practical performance in egglog programs.
This is a significant clarification of where the performance gains arise. The improvement is not presented as a generic speed-up from adopting egglog alone; rather, it depends on preprocessing the domain-set condition into a more favorable syntactic form before saturation. A common misconception would be to treat equality saturation as sufficient by itself. The reported benchmark indicates the opposite: premise form can dominate whether detection takes seconds, minutes, or fails to terminate.
6. Relationship to earlier e-graph work and remaining limitations
JijModeling 2 is best understood as a continuation and reformulation of earlier e-graph-based constraint detection in JijModeling. The prior case study represented each constraint as a separate e-graph, saturated it with a fixed set of approximately 0 rewriting rules, ran constant folding and simple type inference on every e-node, and matched templates for one-hot and SOS1 constraints modulo algebraic congruence (Ishii et al., 2 Jun 2025). That system also distilled three heuristic criteria for rewriting-system design: bidirectionalize asymmetric rules, guard reverse type-erasing rules with side conditions, and favor reductive shortcuts even if logically redundant (Ishii et al., 2 Jun 2025).
The earlier implementation introduced the egg_recursive utility library, a Rust crate atop egg that provides a recursive AST define-and-derive macro and an IntoLanguageChildren trait, allowing native Rust construction and matching of terms rather than handwritten S-expressions (Ishii et al., 2 Jun 2025). This remains relevant context because JijModeling 2 replaces part of the earlier orchestration burden with egglog’s declarative rule language rather than external Rust coordination.
Several limitations identified in the earlier work also clarify the significance of JijModeling 2. Composite SOS constraints previously required multiple passes; bound variables in reductions were handled via an ad-hoc naming layer rather than proper 1 theory; and binary Add/Mul nodes forced heavy use of associativity and commutativity rules, slowing saturation on large sums (Ishii et al., 2 Jun 2025). JijModeling 2 directly addresses the first issue by expressing composite detection logic in Datalog-style rules and partially addresses the binding issue through the locally-named representation and the cost-aware treatment of bound variables (Ishii, 18 May 2026).
At the same time, the available evidence does not claim that all earlier limitations are fully resolved. The paper demonstrates a practical industrial application, improved higher-order pretty-printing, and declarative constraint detection, but it does not state that a full 2-equivalence or HOAS-based treatment has been completed. A plausible implication is that JijModeling 2 should be seen as a substantial architectural advance in declarative preprocessing, while deeper binding-theoretic generalization remains an open direction already anticipated by the earlier roadmap (Ishii et al., 2 Jun 2025).