Simplify cartesian-product inversion with a peeled row - #730
Open
eb8680 wants to merge 1 commit into
Open
Conversation
jfeser
requested changes
Jul 28, 2026
| PlusEmpty(), | ||
| PlusSingle(), | ||
| PlusAssoc(), | ||
| ReduceUnfactor(), |
Contributor
There was a problem hiding this comment.
Can ReduceUnfactor be removed in this PR?
Comment on lines
+846
to
+850
| def __iter__(self): | ||
| return iter(()) | ||
|
|
||
| def __len__(self) -> int: | ||
| return 0 |
Contributor
There was a problem hiding this comment.
These should raise, I think. I assume they're unused.
| # what ``ReduceUnfactor`` does, applied here to the body's own factors | ||
| # rather than by renormalizing the whole candidate: the shape being | ||
| # looked for is a product of factors, so only the top level matters. | ||
| if isinstance(body, Term) and _is_monoid_plus(body.op): |
Contributor
There was a problem hiding this comment.
This shouldn't apply to any Monoid.plus.
| # Absorbing their streams back into this one exposes it again. This is | ||
| # what ``ReduceUnfactor`` does, applied here to the body's own factors | ||
| # rather than by renormalizing the whole candidate: the shape being | ||
| # looked for is a product of factors, so only the top level matters. |
Contributor
There was a problem hiding this comment.
I'm not convinced that doing one step of unfactoring is sufficient. What prevents a stream from being moved more deeply into the term?
Contributor
Author
There was a problem hiding this comment.
I can just reinstate the previous unfactoring behavior, I think this part is orthogonal to the other changes in the PR.
Standing a `_PeelRow` in for the row variable puts it in the mapping position of every `row[..]`, so a subscript is recognised by what it is indexing rather than by comparing against the variable it came from. One evaluation pass peels every summand and records the plate each folds over, replacing the per-summand `row_substitute` closure and its `InvalidIndexError` control flow. Nested same-monoid reductions are absorbed inline at the top level rather than by renormalizing the whole candidate under a dedicated interpretation, so `CartesianProductNormalizeIntp` is no longer needed. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
jfeser
force-pushed
the
eb-peel-row-inversion
branch
from
July 30, 2026 17:59
c62a3cb to
a0306a8
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Rewrites the cartesian-product inversion rule
ReduceDistributeCartesianProductineffectful/ops/monoid.py. No behaviour change is intended.What changed
Peeling replaces the per-summand substitution closure.
_PeelRowis aMappingthat stands in for the row variable during oneevaluatepass. Because it occupies the mapping position of everyrow[..], a subscript is recognised by what it is indexing rather than by comparing the mapping against the variable it came from._peel_subscriptroutes_MappingTerm.__getitem__back to it (row indices are symbolic, so_MappingTerm.__getitem__would never hand a mapping a symbolic key on its own). Each subscript returns the narrowed row and records the plate variable used at the peeled position;row[p]with nothing left over returns the row's own value.This replaces
row_substituteplus itsInvalidIndexErrorcontrol flow: one pass peels every summand, and the per-summand checks that remain are just "the plate this summand's bundle binds is one of the plates the row was indexed by, and its range matches"._CannotPeelcarries the give-up signal, asInvalidIndexErrordid._PeelRowalso counts uses against peels per plate, sosurvives()can tell whether a plate is still referred to after peeling; only surviving plates need renaming onto the shared plate variable. The peeled row is bound to a freshpeeled_var = defop(stream_key)rather than reusingstream_key, since the row it names is a narrower one.The trailing
_to_bodyspecial case is gone: in the fully-peeled case the peeled row has already answered every subscript with the row's value, somonoid.reduce(combined, union_streams)is the same expression the oldhandler({_MappingTerm.__getitem__: _to_body})(evaluate)(combined)produced.CartesianProductNormalizeIntpis removed.Factormay have moved product factors into nested reductions of the same monoid, hiding the product this rule looks for. Previously the whole candidate was rebuilt under a dedicatedCartesianProductNormalizeIntpso thatReduceUnfactorcould merge the stream bundles. That is now done inline over the body's own top-level factors: the shape being looked for is a product of factors, so only the top level matters. The nested streams are absorbed intostreamswhen they do not collide, which is exactly whatReduceUnfactorwould have done.grep -rn CartesianProductNormalizeIntpover the repo (excluding.git) matched only its definition and its single use insidemonoid.py— nothing ineffectful/handlers/jax/monoid.py,tests/, ordocs/referred to it. Its removal also drops the now-unusedfrom effectful.internals.runtime import interpreterimport;defopis now imported fromeffectful.ops.syntax.Testing
No new tests:
tests/test_ops_monoid.pyandtests/test_handlers_jax_monoid.pyalready cover this rule (530 tests, all passing). A fulltests/sweep excludingtests/test_handlers_llm_*.pygives 18317 passed, 2 skipped, 2078 xfailed, 0 failed, matchingstaging-weighted.mypy,ruff checkandruff formatare clean apart from the pre-existingeffectful/handlers/jax/monoid.py:382error, which is also present onstaging-weighted.Note for review
Six
typing.cast(...)calls in the new code are not in #724 — they are runtime no-ops added purely to keepmypyclean, sinceTerm.argselements type asExpr. They are the only difference between this rule's body here and the one in #724, which is otherwise verbatim.Split out of #724 for review. Independent of the sibling
ReduceGroundCartesianProductPR, also split out of #724.🤖 Generated with Claude Code