Skip to content

Add egraph-unary for maximal unary context extraction#1579

Draft
pavpanchekha wants to merge 2 commits intomainfrom
unary-functions
Draft

Add egraph-unary for maximal unary context extraction#1579
pavpanchekha wants to merge 2 commits intomainfrom
unary-functions

Conversation

@pavpanchekha
Copy link
Copy Markdown
Contributor

@pavpanchekha pavpanchekha commented Apr 14, 2026

Unary expressions are special in numerics: they can be efficiently and accurately approximated by polynomials. So one thing we might want is to rewrite an expression like log(exp(a) + exp(b)) into a form like a + log(1 + exp(b - 1)), where there's a nice unary subcomponent like log(1 + exp(x)). Since this requires some rewriting, we want to do it on an e-graph.

This PR thus adds egraph-unary, which extracts non-trivial (not x or sin(x)) distinct unary expressions in an e-graph. It works is a pretty straightforward way, though I got here in a complicated way involving dominator trees:

  • Each eclass e is a unary function of e with expression x
  • Each enode f(e_1, ..., e_n) is also a unary function of e if each e_i is a unary function of e, with the obvious witness term

So you compute this via an eclass analysis where the value is a map { eclass → expr } such that if the data for e1 has { e2 → E[x] } then e1 = E[e2]. Pretty neat and pretty fast! Kinda ugly to do this on the Racket side but whatever.

@pavpanchekha pavpanchekha requested a review from obround April 19, 2026 02:36
Copy link
Copy Markdown
Contributor

@obround obround left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks very cool, I've been playing around with egraph-unary for interval-based series, and the expressions returned seem pretty useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants