Skip to content

Commit ef780f5

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 0cd510d commit ef780f5

11 files changed

Lines changed: 68 additions & 69 deletions

File tree

‎docs/changelog.md‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -210,15 +210,18 @@ a linalg function (in [an example inspired by Siu](https://gist.github.com/sklam
210210
```python
211211
from egglog.exp.array_api import *
212212

213+
213214
@function(ruleset=array_api_ruleset, subsume=True)
214215
def linalg_norm(X: NDArrayLike, axis: TupleIntLike) -> NDArray:
215216
X = cast(NDArray, X)
216217
return NDArray(
217218
X.shape.deselect(axis),
218219
X.dtype,
219-
lambda k: ndindex(X.shape.select(axis))
220-
.foldl_value(lambda carry, i: carry + ((x := X.index(i + k)).conj() * x).real(), init=0.0)
221-
.sqrt(),
220+
lambda k: (
221+
ndindex(X.shape.select(axis))
222+
.foldl_value(lambda carry, i: carry + ((x := X.index(i + k)).conj() * x).real(), init=0.0)
223+
.sqrt()
224+
),
222225
)
223226
```
224227

@@ -372,8 +375,8 @@ rule or expression. For example:
372375
class A(Expr):
373376
def __init__(self, b: B) -> None: ...
374377

375-
class B(Expr):
376-
...
378+
379+
class B(Expr): ...
377380
```
378381

379382
### Top level commands

‎docs/explanation/2026_02_containers.md‎

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,7 @@ def get_sole_polynomial(xs: MultiSet[Value]) -> MultiSet[MultiSet[Value]]:
515515
get_sole_polynomial(MultiSet(polynomial(xss))) => xss
516516
"""
517517

518+
518519
@ruleset
519520
def to_polynomial_ruleset(
520521
n1: Value,
@@ -560,29 +561,29 @@ nested. For example, we might end up with a term like `polynomial(MultiSet(Multi
560561
with just `polynomial(xs)`. We define two additional rules to cover cases like this:
561562

562563
```python
563-
yield rule(
564-
eq(n1).to(polynomial(mss)),
565-
# For each monomial, if any of its terms is a polynomial with a single monomial, flatten
566-
# that into the monomial, otherwise keep it as is
567-
mss1 == mss.map(partial(multiset_flat_map, get_monomial)),
568-
mss != mss1, # skip if this is a no-op
569-
name="unwrap monomial",
570-
).then(
571-
union(n1).with_(polynomial(mss1)),
572-
delete(polynomial(mss)),
573-
set_(get_sole_polynomial(MultiSet(polynomial(mss1)))).to(mss1),
574-
)
575-
yield rule(
576-
eq(n1).to(polynomial(mss)),
577-
# If any of the monomials just has a single item which is a polynomial, then flatten that into the outer polynomial
578-
mss1 == multiset_flat_map(UnstableFn(get_sole_polynomial), mss),
579-
mss != mss1,
580-
name="unwrap polynomial",
581-
).then(
582-
union(n1).with_(polynomial(mss1)),
583-
delete(polynomial(mss)),
584-
set_(get_sole_polynomial(MultiSet(polynomial(mss1)))).to(mss1),
585-
)
564+
yield rule(
565+
eq(n1).to(polynomial(mss)),
566+
# For each monomial, if any of its terms is a polynomial with a single monomial, flatten
567+
# that into the monomial, otherwise keep it as is
568+
mss1 == mss.map(partial(multiset_flat_map, get_monomial)),
569+
mss != mss1, # skip if this is a no-op
570+
name="unwrap monomial",
571+
).then(
572+
union(n1).with_(polynomial(mss1)),
573+
delete(polynomial(mss)),
574+
set_(get_sole_polynomial(MultiSet(polynomial(mss1)))).to(mss1),
575+
)
576+
yield rule(
577+
eq(n1).to(polynomial(mss)),
578+
# If any of the monomials just has a single item which is a polynomial, then flatten that into the outer polynomial
579+
mss1 == multiset_flat_map(UnstableFn(get_sole_polynomial), mss),
580+
mss != mss1,
581+
name="unwrap polynomial",
582+
).then(
583+
union(n1).with_(polynomial(mss1)),
584+
delete(polynomial(mss)),
585+
set_(get_sole_polynomial(MultiSet(polynomial(mss1)))).to(mss1),
586+
)
586587
```
587588

588589
We have avoided the need to match inside of containers by instead using higher order functions to apply blockwise
@@ -643,12 +644,12 @@ def factor_ruleset(
643644
eq(n).to(polynomial(mss)),
644645
# Find factor that shows up in most monomials, at least two of them
645646
counts == MultiSet.sum_multisets(mss.map(MultiSet.reset_counts)),
646-
eq(picked_term).to(counts.pick_max()), # on ties pick an arbitrary one
647+
eq(picked_term).to(counts.pick_max()), # on ties pick an arbitrary one
647648
# Only factor out if it appears in more than one monomial
648649
counts.count(picked_term) > 1,
649650
# The factor we choose is the largest intersection between all the monomials that have that that factored term
650651
picked == mss.filter(partial(multiset_contains_swapped, picked_term)),
651-
factor == multiset_fold(MultiSet.__and__, picked.pick(), picked), # intersection
652+
factor == multiset_fold(MultiSet.__and__, picked.pick(), picked), # intersection
652653
divided == picked.map(partial(multiset_subtract_swapped, factor)),
653654
# remainder is those monomials that do not contain the factor
654655
remainder == mss.filter(partial(multiset_not_contains_swapped, picked_term)),

‎docs/explanation/optional_values.md‎

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,9 @@ Let's see what happens if we call check on something that is not true:
2828

2929
```python
3030
from egglog.bindings import *
31-
egraph = EGraph()
32-
egraph.check_fact(Fact(
33-
Call(
34-
"<",
35-
[Lit(Int(2)), Lit(Int(1))]
36-
)
3731

38-
))
32+
egraph = EGraph()
33+
egraph.check_fact(Fact(Call("<", [Lit(Int(2)), Lit(Int(1))])))
3934
```
4035

4136
We get:
@@ -48,14 +43,9 @@ And what if we call check with a non-unit value?
4843

4944
```python
5045
from egglog.bindings import *
51-
egraph = EGraph()
52-
egraph.check_fact(Fact(
53-
Call(
54-
"+",
55-
[Lit(Int(2)), Lit(Int(1))]
56-
)
5746

58-
))
47+
egraph = EGraph()
48+
egraph.check_fact(Fact(Call("+", [Lit(Int(2)), Lit(Int(1))])))
5949
```
6050

6151
Yep, it fails on us:

‎docs/reference/python-integration.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ class MyExpr(Expr):
8585
return value
8686
raise ExprValueError(self, "MyExpr")
8787

88+
8889
match MyExpr("hello"):
8990
case MyExpr(value):
9091
print(f"Matched MyExpr with value: {value}")
@@ -602,7 +603,7 @@ egraph
602603
This is equivalent to adding the rewrite rules to the e-graph directly, like this, but just more succinct:
603604

604605
```python
605-
x = var("x", Math)
606+
x = var("x", Math)
606607
egraph.register(rewrite(pi).to(math_float(3.14)))
607608
egraph.register(rewrite(square(x)).to(x * x))
608609
```

‎python/egglog/__init__.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from . import config, ipython_magic # noqa: F401
66
from .bindings import EggSmolError, StageInfo, TimeOnly, WithPlan # noqa: F401
7-
from .builtins import * # noqa: UP029
7+
from .builtins import *
88
from .conversion import *
99
from .deconstruct import *
1010
from .egraph import *

‎python/egglog/declarations.py‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
"ConstantRef",
5050
"ConstructorDecl",
5151
"Declarations",
52-
"Declarations",
5352
"DeclarationsLike",
5453
"DefaultRewriteDecl",
5554
"DelayedDeclarations",
@@ -128,7 +127,7 @@ class HasDeclarations(Protocol):
128127
def __egg_decls__(self) -> Declarations: ...
129128

130129

131-
DeclarationsLike: TypeAlias = Union[HasDeclarations, None, "Declarations"]
130+
DeclarationsLike: TypeAlias = Union[HasDeclarations, "Declarations", None]
132131

133132

134133
def upcast_declarations(declarations_like: Iterable[DeclarationsLike]) -> list[Declarations]:
@@ -659,7 +658,7 @@ def signature(self) -> FunctionSignature:
659658
)
660659

661660
@property
662-
def egg_name(self) -> None | str:
661+
def egg_name(self) -> str | None:
663662
return None
664663

665664

‎python/egglog/egraph.py‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,11 @@
6060
"BaseExpr",
6161
"BuiltinExpr",
6262
"Command",
63-
"Command",
6463
"CostModel",
6564
"EGraph",
6665
"Expr",
6766
"ExprCallable",
6867
"Fact",
69-
"Fact",
7068
"GraphvizKwargs",
7169
"GreedyDagCost",
7270
"RewriteOrRule",
@@ -1538,7 +1536,7 @@ def __call__(self, *args, **kwargs) -> Never:
15381536
def ruleset(
15391537
rule_or_generator: RewriteOrRule | RewriteOrRuleGenerator | None = None,
15401538
*rules: RewriteOrRule,
1541-
name: None | str = None,
1539+
name: str | None = None,
15421540
) -> Ruleset:
15431541
"""
15441542
Creates a ruleset with the following rules.
@@ -2086,7 +2084,7 @@ def run(ruleset: Ruleset | None = None, *until: FactLike, scheduler: BackOff | N
20862084
)
20872085

20882086

2089-
def back_off(match_limit: None | int = None, ban_length: None | int = None) -> BackOff:
2087+
def back_off(match_limit: int | None = None, ban_length: int | None = None) -> BackOff:
20902088
"""
20912089
Create a backoff scheduler configuration.
20922090

‎python/egglog/exp/any_expr.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -753,8 +753,8 @@ def any_eval(self: A) -> Any:
753753
return res
754754

755755

756-
_CURRENT_EGRAPH: None | EGraph = None
757-
_LAST_ASSERT: None | A = None
756+
_CURRENT_EGRAPH: EGraph | None = None
757+
_LAST_ASSERT: A | None = None
758758

759759

760760
@contextlib.contextmanager

‎python/egglog/exp/array_api.py‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1459,7 +1459,7 @@ def slice(cls, slice: Slice) -> MultiAxisIndexKeyItem: ...
14591459
converter(Int, MultiAxisIndexKeyItem, lambda i: MultiAxisIndexKeyItem.int(i))
14601460
converter(Slice, MultiAxisIndexKeyItem, lambda s: MultiAxisIndexKeyItem.slice(s))
14611461

1462-
MultiAxisIndexKeyItemLike: TypeAlias = MultiAxisIndexKeyItem | EllipsisType | None | IntLike | SliceLike
1462+
MultiAxisIndexKeyItemLike: TypeAlias = MultiAxisIndexKeyItem | EllipsisType | IntLike | SliceLike | None
14631463

14641464

14651465
class MultiAxisIndexKey(Expr, ruleset=array_api_ruleset):
@@ -2123,7 +2123,7 @@ def int(cls, value: Int) -> OptionalIntOrTuple: ...
21232123
def tuple(cls, value: TupleIntLike) -> OptionalIntOrTuple: ...
21242124

21252125

2126-
OptionalIntOrTupleLike: TypeAlias = OptionalIntOrTuple | None | IntLike | TupleIntLike
2126+
OptionalIntOrTupleLike: TypeAlias = OptionalIntOrTuple | IntLike | TupleIntLike | None
21272127

21282128
converter(type(None), OptionalIntOrTuple, lambda _: OptionalIntOrTuple.none)
21292129
converter(Int, OptionalIntOrTuple, lambda v: OptionalIntOrTuple.int(v))
@@ -2379,7 +2379,8 @@ def vecdot(x1: NDArrayLike, x2: NDArrayLike) -> NDArray:
23792379
x1.shape.drop_last(),
23802380
x1.dtype,
23812381
lambda idx: (
2382-
TupleInt.range(x1.shape.last())
2382+
TupleInt
2383+
.range(x1.shape.last())
23832384
.map_value(lambda i: x1.index(idx.append(i)) * x2.index((i,)))
23842385
.foldl_value(Value.__add__, Value.from_float(0))
23852386
),
@@ -2740,7 +2741,8 @@ def unravel_index(flat_index: IntLike, shape: TupleIntLike) -> TupleInt:
27402741
shape = cast("TupleInt", shape)
27412742

27422743
return (
2743-
shape.reverse()
2744+
shape
2745+
.reverse()
27442746
.foldl_tuple_int(
27452747
# Store remainder as last item in accumulator
27462748
lambda acc, dim: acc.drop_last().append((r := acc.last()) % dim).append(r // dim),
@@ -2754,7 +2756,7 @@ def unravel_index(flat_index: IntLike, shape: TupleIntLike) -> TupleInt:
27542756
array_api_combined_ruleset = array_api_ruleset
27552757
array_api_schedule = (array_api_combined_ruleset + run()).saturate()
27562758

2757-
_CURRENT_EGRAPH: None | EGraph = None
2759+
_CURRENT_EGRAPH: EGraph | None = None
27582760

27592761

27602762
@contextlib.contextmanager

‎python/egglog/runtime.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,7 @@ def to_py_signature(sig: FunctionSignature, decls: Declarations, optional_args:
649649
return Signature(parameters)
650650

651651

652-
ON_CREATE_EXPR: None | Callable[[Callable[[], TypedExprDecl]], None] = None
652+
ON_CREATE_EXPR: Callable[[Callable[[], TypedExprDecl]], None] | None = None
653653

654654

655655
@dataclass

0 commit comments

Comments
 (0)