Skip to content

feat(language): a case may branch on at(…, by=…) - #1012

Draft
FabianHofmann wants to merge 23 commits into
mainfrom
fix/case-exclusivity-at
Draft

FabianHofmann wants to merge 23 commits into
mainfrom
fix/case-exclusivity-at

Conversation

@FabianHofmann

@FabianHofmann FabianHofmann commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Note

The following content was generated by AI.

Two cases whose when: read a condition through the same by= are told apart by what they read, so the entry loads.

Base: the alias PR #1029, merged into this branch; GitHub's stack blocks the retarget, so the base still reads main until the stack is updated.

dimensions: { tech: { dtype: str }, unit: { dtype: str } }
relations:
  tech_of: { key: unit, values: tech }
parameters:
  kind: { dims: [tech], dtype: str }
  cap: { dims: [unit] }
variables:
  p: { dims: [unit], bounds: { lower: 0 } }
expressions:
  limit:
    dims: [unit]
    cases:
      a: { when: "at(kind == 'a', by=tech_of[tech])", expression: cap }
      b: { when: "at(kind == 'b', by=tech_of[tech])", expression: 2 * cap }
    otherwise: 0
constraints:
  c: { dims: [unit], expression: "p <= limit" }
objective: { sense: minimize, expression: sum(p) }

# before: refused
#   Named expression 'limit': cases 'a' and 'b' cannot be told apart before the data arrives: it reads a condition through 'tech_of', which only the data decides. Test this row, or precompute the read as a boolean parameter and test that.
# after: loads
Method, gate output, alternatives
  • Why sound: at names value columns and groups by the whole key, so a coordinate reads one row. Same relation + same named columns → same row → same label. Atoms under a read become subjects tagged with the chain of reads (kind through up[parent] through tech_of[tech]). Different relations, or the same parameter outside the read, stay separate subjects: over-approximation, never a hidden witness.
  • Tests:
    • TestAConditionReadThroughARelation: 5 apart, 5 still refused, incl. a nested read on both sides and a nested read vs its inner read. FLEET gains a self-map up: {key: tech, values: {parent: tech}}.
    • Fuzz test_a_read_through_a_relation_proved_apart_stays_apart_in_every_fleet: ground truth is each atom read off 2592 concrete fleets (one unit, techs t1/t2, each row of tech_of/backup_of/up present or not, kind a/b/c/absent), not the prover's _evaluate. Atoms cover nested reads, tech == 't1' under a read, is_a under a read, relation columns. 2 seeds × 2000 pairs, ~1.3 s each.
  • Guards, each deleted and the full suite run:
    • keep only the last lookup in the read chain (_every_atom, _atom) → 3 fail (fuzz ×2, a-read-inside-another-and-the-inner-read-alone); passed every test before this revision
    • a lookup ignores the relation → 8 fail
    • subjects drop the reads → 8 fail
    • no row check on a read → 3 fail (fuzz ×2, a-read-and-the-missing-row)
    • subject memo keyed by id(node) alone → 3 fail (fuzz ×2, one-condition-at-the-coordinate-and-through-a-row)
  • Independent brute-force oracle (outside the tree, masks on concrete data, random pairs): 0 unsound of 113 proved apart.
  • A read's subject is its own row: the row check moved from _rows_read into _atom, so _subject_of keeps its JoinedCondition case (dropping that one instead needs a narrower type for assert_never, and more lines).
  • Moved coverage: test_validation.py::test_a_read_through_a_relation_is_undecidable_in_a_case_when asserted the old refusal. Now two tests: the read and its negation load; the read beside c > 0 is a true overlap with a witness.
  • tests/test_pypsa_references.py builds a grid by hand: key changed to ((), id(atom)).
  • Names per review: Subject.through, Lookup.roles, _every_atom, _rows_holding, _bare; witness wording unchanged.
  • Docs: one paragraph in named.md#refused-cases.
  • gate: pixi run lint and pixi run ci pass (4210 passed, 1 skipped; docs-build strict; compile-tex 59 documents).
  • not done: at(kind, by=r[c]) == 'a'. A str parameter cannot sit in an expression, so it is refused before exclusivity, as on main. A numeric at(x, by=…) > 3 is a comparison of expressions, so still refused as undecidable. count and shift in a when: unchanged.

Generated by Claude Code

Two `when:` strings that read a condition through the same relation and
columns reach one row, so the exclusivity check compares the conditions
they read instead of refusing the pair as undecidable.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01F4nx8HNM3uZTJUHnUs1cr9
The undecidable refusal test now pins what loads and what overlaps, and
the PyPSA reference tests key their hand-built grid as the prover does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01F4nx8HNM3uZTJUHnUs1cr9
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01F4nx8HNM3uZTJUHnUs1cr9
The fleet fuzz reads each atom off concrete fleets instead of the
prover's own cells, and covers nested reads, dimension tests under a
read and condition references. The case-when test asserting two
behaviours is split in two.

@FBumann FBumann left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@FabianHofmann I would like to reuse as many primitives and meanings as possible if its genuinely the same thing.

Here are some things id like to change. You don't have to apply all of them

Note

The following content was generated by AI.

Naming: one idea, three words. Users read "through" (docs, JoinedCondition docstring, the new witness). The code adds Hop and via.

now proposed why
Subject.via through the word __str__ prints from it: "kind through up[parent] through tech_of[tech]"
Hop Lookup program.py calls this "a lookup Join"; soundness rests on one row per step. Not Read: _read() exists
Via alias drop, tuple[Lookup, ...] one more name for a short tuple
Hop.columns roles holds join.dropped, which JoinColumns calls roles; columns already names the JoinColumns on JoinedCondition
_reads _every_atom yields every atom, not only reads
_rows_under _rows_holding its docstring: "the rows that hold subject's value"
Subject._own _bare the label without the chain
  • Why not by for the field:
    • it holds a chain of by= arguments, not one; "kind by tech_of[tech]" does not read, so field and witness would differ
    • sum(…, by=…) groups many rows; the chain is sound only because each step reads one row
  • Kept: the witness wording, the test names.
  • Title: "…relation load" reads as a noun until the second pass. Refused by design on main, so feat:
    feat(language): a case may branch on at(…, by=…) (49 chars)

Generated by Claude Code

@FBumann FBumann added the area: dimensions How dimensions are stated and used label Nov 16, 2026 — with Claude
… does

Subject.via is through, Hop is Lookup with roles, the Via alias is
gone, and _reads, _rows_under and _own are _every_atom, _rows_holding
and _bare.
@FabianHofmann FabianHofmann changed the title fix(language): cases read through one relation load feat(language): a case may branch on at(…, by=…) Nov 17, 2026

Copy link
Copy Markdown
Contributor Author

Note

The following content was generated by AI.

  • Renames applied in 8e0eedd: through, Lookup, tuple[Lookup, ...], roles, _every_atom, _rows_holding, _bare. Witness wording and test names kept.
  • Title now feat(language): a case may branch on at(…, by=…); CHANGELOG line matches.

Generated by Claude Code

@FabianHofmann
FabianHofmann removed this pull request from stack #1022 November 19, 2026 08:29
`from_region: { alias: region }` has the labels, dtype and order of
`region` under a second name, so an entry can be over one set twice, as
TIMES' IRE_FLO is over two regions. The alias implies the relation
`from_region`, keyed by the alias with the value column `region`, read
`from_region[region]`, from each label to itself. It prints as the
primed index of `region` over the same set.

Ported onto main from #1020, where the identity read was a mapping of
#1015. A relation the file declares under the alias's name is refused
with the rewrite that deletes it.

(cherry picked from commit d924d66)
A reduction over a dimension primed its dummy once per enclosing use,
so inside an entry over an alias of that dimension it took the alias's
own index and printed `w_{g',g'}`. The dummy now primes past every index
another dimension prints with.

(cherry picked from commit 29973cd)
`exchanged` sums over `bus` inside a sum over its alias `from_bus`, so
the golden model reaches the line that primes a dummy past the alias.

(cherry picked from commit 84ba347)
IRE_FLO reads each region and commodity twice. With an alias for each,
the spec states it over the regions and commodities themselves, and the
workaround through an invented trade_link dimension goes.

Ported from #1020; top_ire is keyed by its five columns on main.
The relation an alias implies has a column over the alias and one over
the dimension it aliases, so it counted as a use of both, and an alias
no entry is over drew no never-an-axis advice. An implied relation now
reaches no dimension, and an alias in use reaches the dimension whose
labels it has.
Program.relations names the implied relation beside the declared ones,
and that a tool takes its rows from the labels of the alias.
`where: "from_g[g] == 'a'"` was refused as a column of a dimension,
since a side named after a dimension never reached the relation the
alias implies. A side that reads a column now reads the relation of
that name, as #1020 read its mapping.
…e-exclusivity-at

# Conflicts:
#	CHANGELOG.md
Under an entry over [from_g, g], sum(sum(w, over=g), over=from_g)
printed both dummies as g'': the inner dummy primed past every
dimension's index but not past the dummy of the sum around it. A
dummy now also skips each index an enclosing reduction or lookup
prints. The goldens do not change.
Along an alias of an unordered dimension, shift, sum_back, position,
a type 2 set and a piecewise curve said to declare the alias ordered,
and a label outside one_of said to add it to the alias's one_of. An
alias may carry neither, so both now name the dimension it aliases.
A relation declared under an alias's name is told to be deleted only
where it restates the implied relation; any other collides with the
dimension as usual.

This branch has not been deployed

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

Labels

area: dimensions How dimensions are stated and used

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants