Repository navigation
fix(spec): a where does not guard what a shift, window or lookup reads elsewhere - #1173
Merged
Merged
Conversation
…s elsewhere The rows a constraint's where keeps narrow the finiteness checks of what it reads. A sum, a window, a shift and a lookup read their operand at other coordinates along the axes they move. So the narrowing is widened along those axes to every row there. It stays along the other axes, where they read their own coordinate. Before, where: b > 0 with x <= shift(a / b, edge=0) built x[1] <= inf without a word. The widening reads only ctx.rows, so it applies to any narrowing.
Merging this PR will not alter performance
Comparing Footnotes
|
4 of 5 tasks
…d condition reads elsewhere A shift, an at() and a count of a condition read their operand at other coordinates, as their expression forms do. So the rows a where keeps are widened along the axes they move, the same way. Before, where: b > 0 with a case on shift(a / b > 0.1, along=t, offset=1) read the NaN of a / b at t=0 as false and built row t=1 without a word. The widening moves to context.py, so the evaluator and the conditions share it.
… keeps A guard over t and s keeps column p out of a shift and a lookup along t, so dropping the guard fails. The lookup keyed by t and s also fails when it widens along the key column it carries. A shift summed over an empty dimension and a where: t > 1 around a shift are refused, the second as mathspec's literal rule asks.
FabianHofmann
marked this pull request as ready for review
November 18, 2026 10:54
FabianHofmann
added a commit
that referenced
this pull request
Nov 18, 2026
* fix(spec): check otherwise only where no case applies A case value, and the otherwise value, was evaluated over every row and masked to its region only afterwards, so the finiteness check fired at rows another case claims (e.g. `cap * pu` where a case guards `cap == inf`). Each case value is now evaluated with the context's `rows` narrowed to its region, the mechanism that already scopes the check to a constraint's built rows, both when building and when reading the data under an unbuilt sum over an empty dimension. Fixes #1148 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01F4nx8L63c3o8u6QQvr6TGN * fix(spec): a where does not guard what a shift, window or lookup reads elsewhere The rows a constraint's where keeps narrow the finiteness checks of what it reads. A sum, a window, a shift and a lookup read their operand at other coordinates along the axes they move. So the narrowing is widened along those axes to every row there. It stays along the other axes, where they read their own coordinate. Before, where: b > 0 with x <= shift(a / b, edge=0) built x[1] <= inf without a word. The widening reads only ctx.rows, so it applies to any narrowing. * docs: name #1173 in the release note * fix(spec): skip a case that applies nowhere A case whose region holds no row is not evaluated, and its uses are consumed unread. A zero over the dimensions of the cases stands in for it, a term when the cases carry a variable. Its data is no longer checked, so a window wider than its axis there no longer raises. Tests pin that a shift or window in a case value is still refused where it reads a non-finite value, and cover the fold path. The release note is one line naming this PR. * fix(spec): a where does not guard what a shifted, looked-up or counted condition reads elsewhere A shift, an at() and a count of a condition read their operand at other coordinates, as their expression forms do. So the rows a where keeps are widened along the axes they move, the same way. Before, where: b > 0 with a case on shift(a / b > 0.1, along=t, offset=1) read the NaN of a / b at t=0 as false and built row t=1 without a word. The widening moves to context.py, so the evaluator and the conditions share it. * test(spec): pin what a where guards along the axes a reader moves and keeps A guard over t and s keeps column p out of a shift and a lookup along t, so dropping the guard fails. The lookup keyed by t and s also fails when it widens along the key column it carries. A shift summed over an empty dimension and a where: t > 1 around a shift are refused, the second as mathspec's literal rule asks. --------- Co-authored-by: Claude <noreply@anthropic.com>
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.
Closes #1171.
Changes proposed in this Pull Request
Note
The following content was generated by AI.
wherenarrows the finiteness check to the rows it keeps. Asum, a window (sum_back), ashiftand anat()lookup read their operand at other coordinates. Their operand is now checked with the narrowing widened along the axes they move (rows.any(along)), as mathspec's rule says: "a sum, a window, a shift or a lookup drops every guard from outside it".cases:when::shift(cond),at(cond)andcount(cond, over=...).where: b > 0oversstill guards a shift alongt.ctx.rows, not thewhere:itself, so a narrowing to acases:region (fix(spec): check otherwise only where no case applies #1163) widens the same way.df4b2acfromspec/prune-unbuilt, adapted to master. It addssumover a frame dimension (it leaked too) and usesdropped_dimsfor a lookup.Revision (after review):
shift,atandcountnow widen too. Before, a NaN ofa / bthey read under awheresilently read as false.linopy/spec/context.pyaswidened, soevaluator.pyandwhere.pyshare it.[t, s], so dropping the guard fails it. It gained a lookup keyed by[t, s], which fails if a lookup widens alongjoined_dims._data_underpath), and the strict rule (where: t > 1around a shift by 1 is refused).Validation and reviewer notes
On master, with
where: b > 0,b = [0, 2, 2],a = [1, 1, 1], each of these built a row withinfor dropped a term, without an error. On this branch each raisesSpecDataError: ... divide of 'a', 'b' is not finite at t=0:x <= shift(a / b, along=t, offset=1, edge=0)[0, inf, 0.5]x <= sum_back(a / b, along=t, window=2)[inf, inf, 1]x <= sum(a / b, over=t)[inf, inf, inf]x <= at(a / b, by=succ[u])(u: trelation)[0.5, inf, 0.5]shift(a / b * x, along=t, offset=1, edge=0) <= 1x[0]dropped silentlyx <= r,ra case onshift(a / b > 0.1, along=t, offset=1)a / bat t=0Tests in
test/test_spec_builder.py:test_a_where_does_not_guard_what_a_row_reads_at_another_coordinate: the five expression cases above. All fail on master.test_a_where_does_not_guard_what_a_condition_reads_at_another_coordinate:shift,atandcountof a condition in awhen:. All fail on master and on the first revision.test_a_where_does_not_guard_a_shift_summed_over_an_empty_dimension: fails on master.test_a_where_drops_every_guard_along_the_axis_a_reader_moves:where: t > 1around a shift by 1 is refused. Builds on master.test_a_where_guards_what_a_row_reads_at_its_own_coordinate:shift(a) / b,a / b + shift(a),sum(a) / bstill build, row t=0 masked.test_a_where_guards_a_reader_along_the_axes_it_does_not_move: 2-D shift and lookup, the guard onsholds.Mutants checked against
test/test_spec*.py: dropping the guard entirely, widening a lookup alongjoined_dims, and no widening in_data_undereach fail at least one test.Checks:
pytest test/test_spec*.py(580 passed, 6 skipped),ruff check .,ruff format --check .(ruff 0.16.10),mypy linopy.Reviewer notes:
where: t > 1around a shift by 1, or a divisor that is zero only in the last period withshift(..., offset=1). Master built these correctly. Exact read sets (the rows mask moved back through each reader) are possible, but more code. A test pins the strict rule, so a change is deliberate.dropped_dimsonly. A key column the call does not name is read at its own label, so it keeps its guard. The source commit widened along alljoineddims._data_under. Stacked, its default scope[ctx] * nbecomes[widened(node, ctx)] * n; the case scopes stay. A trial merge resolved this way passestest/test_spec*.py(585 passed, 6 skipped), and ashiftof a condition inside nested cases is refused there too.Checklist
AGENTS.md).doc.doc/release-notes.mdof the upcoming release is included.Generated by Claude Code