Skip to content

knotkit/e-graph

Propagates equalities through expressions and extracts a minimum-cost equivalent expression

import {
function eGraph<Op extends string = string>(): EGraph<Op>

Create an empty congruence relation over terms with string operators and ordered children.

eGraph
} from 'knotkit/e-graph'
const
const graph: EGraph<string>
graph
=
eGraph<string>(): EGraph<string>

Create an empty congruence relation over terms with string operators and ordered children.

eGraph
()
const
const x: EClass
x
=
const graph: EGraph<string>
graph
.
EGraph<string>.add: (operator: string, children?: readonly EClass[]) => EClass

Intern an operator applied to its ordered children. One class lookup per child.

@throws ― {EGraphInvalidInputError} FOREIGN_CLASS for a child from another graph.

add
('x')
const
const plusZero: EClass
plusZero
=
const graph: EGraph<string>
graph
.
EGraph<string>.add: (operator: string, children?: readonly EClass[]) => EClass

Intern an operator applied to its ordered children. One class lookup per child.

@throws ― {EGraphInvalidInputError} FOREIGN_CLASS for a child from another graph.

add
('+', [
const x: EClass
x
,
const graph: EGraph<string>
graph
.
EGraph<string>.add: (operator: string, children?: readonly EClass[]) => EClass

Intern an operator applied to its ordered children. One class lookup per child.

@throws ― {EGraphInvalidInputError} FOREIGN_CLASS for a child from another graph.

add
('0')])
const
const left: EClass
left
=
const graph: EGraph<string>
graph
.
EGraph<string>.add: (operator: string, children?: readonly EClass[]) => EClass

Intern an operator applied to its ordered children. One class lookup per child.

@throws ― {EGraphInvalidInputError} FOREIGN_CLASS for a child from another graph.

add
('sin', [
const x: EClass
x
])
const
const right: EClass
right
=
const graph: EGraph<string>
graph
.
EGraph<string>.add: (operator: string, children?: readonly EClass[]) => EClass

Intern an operator applied to its ordered children. One class lookup per child.

@throws ― {EGraphInvalidInputError} FOREIGN_CLASS for a child from another graph.

add
('sin', [
const plusZero: EClass
plusZero
])
const graph: EGraph<string>
graph
.
EGraph<string>.equivalent: (left: EClass, right: EClass) => boolean

Check equivalence under all equalities supplied so far.

@throws ― {EGraphInvalidInputError} FOREIGN_CLASS for an unknown handle.

equivalent
(
const left: EClass
left
,
const right: EClass
right
) // false
const graph: EGraph<string>
graph
.
EGraph<string>.unite: (left: EClass, right: EClass) => boolean

Merge two classes and propagate congruence to a fixed point. Returns whether the partition changed.

@throws ― {EGraphInvalidInputError} FOREIGN_CLASS for an unknown handle.

unite
(
const x: EClass
x
,
const plusZero: EClass
plusZero
) // the caller supplies x = x + 0
const graph: EGraph<string>
graph
.
EGraph<string>.equivalent: (left: EClass, right: EClass) => boolean

Check equivalence under all equalities supplied so far.

@throws ― {EGraphInvalidInputError} FOREIGN_CLASS for an unknown handle.

equivalent
(
const left: EClass
left
,
const right: EClass
right
) // true, by congruence
e-graph.ts
const product = graph.add('×', [graph.add('2'), x])
const sum = graph.add('+', [x, x])
graph.unite(product, sum)
// A printer counts occurrences of x.
graph.extract(product, (op) => Number(op === 'x')).operator // '×'
// A code generator charges five units for multiplication and one for every other operator.
graph.extract(product, (op) => (op === '×' ? 5 : 1)).operator // '+'
  • share repeated subexpressions even when their operands were built from different equivalent terms
  • recognize that replacing an operand with an equal operand preserves an expression
  • select the cheapest equivalent expression under a caller's operator prices
FactoryChoose whenCost
eGraph
kind hash-consed
the caller supplies term equalities and needs an immediate equivalence query or a minimum-cost finite expressionan effective merge rescans stored terms until the congruence relation stabilizes

Performance and benchmarks for this module

{"v":1,"box":[38.4774,94.1597,943.1333,437.3555],"x":{"s":"linear","lim":[-0.27,6.27],"inv":false,"g":0,"t":"Price of one ×","tk":[[0,"0"],[1,"1"],[2,"2"],[3,"3"],[4,"4"],[5,"5"],[6,"6"]]},"y":{"s":"linear","lim":[1.73,8.27],"inv":false,"g":0,"tk":[[2,"2"],[3,"3"],[4,"4"],[5,"5"],[6,"6"],[7,"7"],[8,"8"]]},"ti":"Extraction switches from 2 × x to x + x once × costs more than 3","a":[{"k":"line","c":"#fffff8","s":"x","d":[0,2,1,3,2,4,3,5,4,6,5,7,6,8],"p":[75.83,423.19,214.15,370.71,352.48,318.23,490.81,265.76,629.13,213.28,767.46,160.8,905.79,108.33]},{"k":"line","c":"#5b6b7c","s":"x","d":[0,2,1,3,2,4,3,5,4,6,5,7,6,8],"p":[75.83,423.19,214.15,370.71,352.48,318.23,490.81,265.76,629.13,213.28,767.46,160.8,905.79,108.33]},{"k":"line","c":"#fffff8","s":"x","d":[0,5,1,5,2,5,3,5,4,5,5,5,6,5],"p":[75.83,265.76,214.15,265.76,352.48,265.76,490.81,265.76,629.13,265.76,767.46,265.76,905.79,265.76]},{"k":"line","c":"#c4501b","s":"x","d":[0,5,1,5,2,5,3,5,4,5,5,5,6,5],"p":[75.83,265.76,214.15,265.76,352.48,265.76,490.81,265.76,629.13,265.76,767.46,265.76,905.79,265.76]},{"k":"line","c":"#fffff8","s":"x","d":[0,2,1,3,2,4,3,5,4,5,5,5,6,5],"p":[75.83,423.19,214.15,370.71,352.48,318.23,490.81,265.76,629.13,265.76,767.46,265.76,905.79,265.76]},{"k":"line","c":"#1d5fa6","s":"x","d":[0,2,1,3,2,4,3,5,4,5,5,5,6,5],"p":[75.83,423.19,214.15,370.71,352.48,318.23,490.81,265.76,629.13,265.76,767.46,265.76,905.79,265.76]},{"k":"segments","c":"#8c8c84"},{"k":"segments","c":"#8c8c84"},{"k":"annotation","c":"#2b2b28"},{"k":"annotation","c":"#5c5c57"},{"k":"annotation","c":"#5b6b7c"},{"k":"annotation","c":"#c4501b"},{"k":"annotation","c":"#1d5fa6"}],"lg":[]} 0 1 2 3 4 5 6 Price of one × 2 3 4 5 6 7 8 tie at 5 Total cost of each form of twice x (x costs 2, + costs 1) 2 × x x + x Extracted Extraction switches from 2 × x to x + x once × costs more than 3
{"v":1,"box":[38.4774,122.3921,495.1333,437.3555],"x":{"s":"linear","lim":[-0.27,6.27],"inv":false,"g":0,"t":"Price of one ×","tk":[[0,"0"],[1,"1"],[2,"2"],[3,"3"],[4,"4"],[5,"5"],[6,"6"]]},"y":{"s":"linear","lim":[1.73,8.27],"inv":false,"g":0,"tk":[[2,"2"],[3,"3"],[4,"4"],[5,"5"],[6,"6"],[7,"7"],[8,"8"]]},"ti":"Extraction switches from 2 × x to x + x\nonce × costs more than 3","a":[{"k":"line","c":"#fffff8","s":"x","d":[0,2,1,3,2,4,3,5,4,6,5,7,6,8],"p":[57.33,424.35,127.16,376.19,196.98,328.03,266.81,279.87,336.63,231.71,406.46,183.55,476.28,135.4]},{"k":"line","c":"#5b6b7c","s":"x","d":[0,2,1,3,2,4,3,5,4,6,5,7,6,8],"p":[57.33,424.35,127.16,376.19,196.98,328.03,266.81,279.87,336.63,231.71,406.46,183.55,476.28,135.4]},{"k":"line","c":"#fffff8","s":"x","d":[0,5,1,5,2,5,3,5,4,5,5,5,6,5],"p":[57.33,279.87,127.16,279.87,196.98,279.87,266.81,279.87,336.63,279.87,406.46,279.87,476.28,279.87]},{"k":"line","c":"#c4501b","s":"x","d":[0,5,1,5,2,5,3,5,4,5,5,5,6,5],"p":[57.33,279.87,127.16,279.87,196.98,279.87,266.81,279.87,336.63,279.87,406.46,279.87,476.28,279.87]},{"k":"line","c":"#fffff8","s":"x","d":[0,2,1,3,2,4,3,5,4,5,5,5,6,5],"p":[57.33,424.35,127.16,376.19,196.98,328.03,266.81,279.87,336.63,279.87,406.46,279.87,476.28,279.87]},{"k":"line","c":"#1d5fa6","s":"x","d":[0,2,1,3,2,4,3,5,4,5,5,5,6,5],"p":[57.33,424.35,127.16,376.19,196.98,328.03,266.81,279.87,336.63,279.87,406.46,279.87,476.28,279.87]},{"k":"segments","c":"#8c8c84"},{"k":"segments","c":"#8c8c84"},{"k":"annotation","c":"#2b2b28"},{"k":"annotation","c":"#5c5c57"},{"k":"annotation","c":"#5b6b7c"},{"k":"annotation","c":"#c4501b"},{"k":"annotation","c":"#1d5fa6"}],"lg":[]} 0 1 2 3 4 5 6 Price of one × 2 3 4 5 6 7 8 tie at 5 Total cost of each form of twice x (x costs 2, + costs 1) 2 × x x + x Extracted Extraction switches from 2 × x to x + x once × costs more than 3
  • Declaring x + 0 equal to x also equates all 6 nested sin pairs.
  • Multiplication price 1 selects × (cost 3). Price 6 selects + (cost 5).
  • Prices count each occurrence. Here x costs 2, addition costs 1, and literals cost 0.

Hover or focus a chart for exact values. Drawn by demo.ts against knotkit/e-graph.

ExportReturnsDoes
eGraphEGraph<Op>Interns ordered terms and eagerly closes each merge under congruence.

Cost per member. Open a row for every operation.

eGraph 5 members
MemberCost
sizeO(1)
addO(k * alpha(n)) amortized for k children
uniteO(n * (n + m) * alpha(n)) per effective merge, O(alpha(n)) otherwise
equivalentO(alpha(n)) amortized
extractO((n + m) * alpha(n) + n log(n + 1)), excluding the price callback
InputTypeGet one from
local price per operator occurrence(operator: Op) => numberthe caller

Every error this module throws is EGraphInvalidInputError, with a code from a closed, exported list. Branch on the code; the message is for people.

FOREIGN_CLASS INVALID_COST COST_OVERFLOW
If you need to…Use instead
merge groups of opaque valuesknotkit/disjoint-set
discover substitutions by pattern matching and repeatedly applying rewrite rulesegglog

Measured by the suite in packages/e-graph/__tests__/bench. The numbers come from one machine, so run the suite on yours before deciding on speed alone.

Prompt · knotkit/e-graph
Use knotkit/e-graph for this task. If Knotkit is missing, follow https://knotkit.jkomyno.dev/getting-started.md#install-from-source. Import from 'knotkit/e-graph'.

Exports:
- `eGraph` → `EGraph<Op>`: Interns ordered terms and eagerly closes each merge under congruence.

Pick the layout by these rules:
- `eGraph` (kind 'hash-consed') when the caller supplies term equalities and needs an immediate equivalence query or a minimum-cost finite expression

Inputs and their sources:
- local price per operator occurrence: `(operator: Op) => number` from `the caller`

Use something else when:
- merge groups of opaque values → knotkit/disjoint-set
- discover substitutions by pattern matching and repeatedly applying rewrite rules → egglog

Errors are EGraphInvalidInputError with a `code` property. Branch on `error.code`, never on the message.

Full page: https://knotkit.jkomyno.dev/modules/e-graph.md
Types: node_modules/knotkit/e-graph/ and https://knotkit.jkomyno.dev/reference/knotkit/e-graph/