Skip to content

Add a Relevant to Expected step to state errors - #495

Merged
CatarinaGamboa merged 3 commits into
mainfrom
feat/297-relevant-state-step
Nov 9, 2026
Merged

CatarinaGamboa merged 3 commits into
mainfrom
feat/297-relevant-state-step

Conversation

@CatarinaGamboa

@CatarinaGamboa CatarinaGamboa commented Nov 6, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #297

A state error's Found now ends with a step called Relevant to Expected. This step keeps only the facts that the expected state depends on. The full Found is still the step before it. This is a display change only: the VC sent to the solver, the verdict, the hints and the category are all unchanged.

When a fact is left out (established independence)

Each fact (a conjunct of a Found row) is reduced to the solver terms it constrains:

  • a ghost function applied to an object, f(x). A state stands for its state set function, so open(x) and state1(x) == state1(y) both constrain state1(x).
  • a variable whose own value the fact constrains, such as mode, #canRedo_result, or x in x == y.

A fact is kept if it shares a term with Expected, either directly or through other kept facts. All other facts are left out. Different ghost functions, or the same function over different objects, are different solver terms, so a fact that shares no term with this group is independent of Expected.

To keep this safe, an object is never split into its ghost functions when any fact constrains it as a value (x == y) or passes it to an alias or an unknown function. In that case, all of its facts stay together. Conditions are not split either: facts under the same c ? … : … share c and stay together. No step is added when every fact or no fact is relevant. The pass works on expressions and ghost declarations, not on rendered names.

Before / after (CLI message, which uses the last step)

Example Before After
E_A3_07 scanner (the issue example) startTiling(preview@53) && state2(preview@53) == state2(preview@52) && compressionSet(preview) && state1(preview) == state1(preview@53) startTiling(preview@53) && state1(preview) == state1(preview@53)
Study A1_01 SlideRecoder tilingNotExplicit(wParam) && compressionNotExplicit(wParam) compressionNotExplicit(wParam)
A3_05 ScaledImageGenerator startCompression(iwparam) && state1(iwparam) == 0 startCompression(iwparam)
ErrorTrafficLightRGB green(tl@48) && red(tl) && r(tl) == r(tl@48) && g(tl) == g(tl@48) && b(tl) == b(tl@48) red(tl)
ErrorArgumentPicksTheStateEarlier tilingOff(param@24) && uncompressed(param@24) && tilingOff(param@25) && state2(param@25) == state2(param@24) && compressed(param) && state1(param) == state1(param@25) tilingOff(param@25) && state1(param) == state1(param@25)

Expected is unchanged in every row. Across the 98 state-error cases in testSuite, 15 messages in 10 cases change. Only the Found text changes; other examples are resultset_forward_error, ErrorIteratorReturnHasNext, ErrorStateHintJoins and ErrorStateHintMultipleSets. The CLI output of the other 88 cases is byte-identical.

The six study exercises (proposal D): all report the same verdicts and lines. A1_01 gets the step, as shown above. A1_38 (open(buffered) && unmarked(buffered), where both state sets are in Expected), A1_09 (one conditional fact), and A1_56 (no Found, "could not prove it") have nothing safe to remove, so they get no step. A2_04 and A2_10 are refinement errors and are unaffected. In the A1_56 partial-fix variant (only redo() guarded), every fact sits under the shared #canRedo_result condition, so it also gets no step. Much of that noise comes from switch cases not being modelled (see the comment on #297).

Webview

The server already sends the whole history, so the extra step reaches the panel without any server change, and the back arrow returns to the full Found. However, vc-implications.ts labels the newest step "Simplified" and never shows its pass name. For the label "Relevant to Expected" to appear, vscode-liquidjava needs a one-line change: use simplification for index 0 when it is set. That change is not part of this PR.

Tests

  • TestStateErrorContext#foundEndsWithTheFactsRelevantToTheExpectedStateSet: the message, the step name, and that stepping back shows the compression state set (existing fixture).
  • VCSimplificationTest: two conservative cases where nothing may be left out: an object compared as a value, and an object passed to an unknown function. Each one fails if its rule is removed.

Full suite: 715 tests pass locally.

🤖 Generated with Claude Code

The last step of a state error's Found keeps only the facts that share a
ghost function application or a value with Expected, directly or through
other facts. Facts about other state sets, or earlier versions not linked
to the expected one, are left out. Objects compared as values, or passed
to an alias or unknown function, are never split. No step is added when
every fact or none is relevant. The VC and the verdict do not change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@CatarinaGamboa

Copy link
Copy Markdown
Collaborator Author

Codex review, round 1 (codex exec review --base origin/main): "The new filtering pass is limited to state-error diagnostics, and the targeted tests pass. I found no actionable regression in the diff." No findings, so the loop stops here.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@CatarinaGamboa CatarinaGamboa changed the title Add a Relevant to this requirement step to state errors Add a Relevant to Expected step to state errors Nov 6, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@CatarinaGamboa

Copy link
Copy Markdown
Collaborator Author

Clean-code review (clean-code-principles)

Reviewed the diff only, weighed against the standing rules (smallest PR that tests well, match surrounding style, no tidy-only refactors, lean tests).

Finding Verdict
VCRelevantFacts.java:60,64,81 - [core-dry] apply walks the nested rowFacts three times (forEach(facts -> facts.forEach(...)) twice, and a second mapToInt(List::size).sum() for the total) Fixed in f413c9e. The facts are now flattened once into allFacts, which both loops use, and the total is allFacts.size(). No behaviour change.
VCRelevantFacts.java:79 - [core-dry] symbolsOf(fact, values) is computed again for each fact in the kept filter Declined. Caching it per row needs a parallel nested list. The small cost is only paid when a state error is displayed.
VCRelevantFacts.java apply - [func-small] about 30 lines (collect, close over linked symbols, filter) Declined. The three stages are separated by blank lines, and the fixed-point loop reads clearly in place. Extracting it would only move code.
VCRelevantFacts.java:59 - [func-few-arguments] addSymbols(expected, new HashMap<>(), values) passes a throwaway map Declined. One recursive walk fills both sets. Splitting it in two would duplicate the traversal.
VCChecker.java ghostFunctionsOf and the VCRelevantFacts constructor @param describe the same contract OK. One documents the implementation and the other the parameter it is passed as.
Comments and Javadoc - [doc-why-not-what] OK. They state the soundness argument, not what the code does.
Tests - [lean tests] one integration assertion and two VCSimplificationTest cases OK. Per the PR body, each test fails if its rule is removed.

Full suite locally after the fix: 715 tests, 0 failures.

@CatarinaGamboa

Copy link
Copy Markdown
Collaborator Author

Codex review, round 2 (soundness only)

Codex was asked to find concrete counterexamples where a dropped fact changes whether Found implies Expected, or where a kept fact misleads. It reported two findings, both from synthetic harnesses that call the pass directly.

1. A kept fact can outlive the row that binds its variable (∀q. state2(q) == 0 ⇒ state1(p) == state1(q), Expected state1(p) == 1, shown as state1(p) == state1(q)). Declined. The dropped row's only fact is about state2(q), which is independent of Expected. The kept fact still mentions q, and its name and line come from the translation table, not from the row. This matches main: NeededVariables already removes a binder row whose variable another row mentions ("a variable that another row mentions is already shown there").

2. Dropping an independent contradiction (state1(p) == 0 && state2(q) == 0 && state2(q) == 1, Expected state1(p) == 1). Declined, unreachable. Both callers of throwStateRefinementError (AuxStateHandler for invocations and for field writes) throw only after checkStateSMT fails, which means Found ∧ ¬Expected is satisfiable. An unknown result throws SMTUnknownError instead. So the full Found is satisfiable, and so is the dropped group. That group shares no term with the kept facts or with Expected, so Kept ∧ ¬Expected is satisfiable iff Found ∧ ¬Expected is. That is the equivalence the pass relies on, and Codex also notes that this path cannot emit the error.

Other cases Codex checked and found sound: a zero-argument ghost (rewritten to flag(p) and kept), and multi-argument or non-variable ghost arguments (their variables become value symbols, so their facts stay together).

No code change this round.

@CatarinaGamboa

Copy link
Copy Markdown
Collaborator Author

Codex review, round 3 (soundness only): no new findings

Codex was asked to dispute the round 2 declines only with a reachable counterexample, meaning a real program whose message is wrong. It ran the verifier on ErrorArgumentPicksTheStateEarlier and ErrorStateHintNaming, read the Z3 translation (makeFunctionInvocation, supertype coercion, field access, aliases), and reported no new findings.

The argument for why it holds. The pass can only be unsound if it under-links, i.e. if one Z3 term gets two different keys. Linking too much only keeps extra facts.

  • Objects. An object is keyed by variable name, the same name its Z3 constant has. The supertype copies of a variable (storeVariablesSubtypes) keep that name.
  • Ghost functions. A ghost is keyed by GhostFunction.getName(), both for a state's set and for a ghost called directly. So open(x) and state1(x) == state1(y) get the same key state1(x).
  • Anything unresolved (a non-ghost function, an alias, a ghost with several or non-variable arguments) makes its variables value symbols. That keeps all the facts about those objects together.

Together with the satisfiability argument from round 2 (the error is only thrown when Found ∧ ¬Expected is satisfiable), a dropped group cannot change whether Found implies Expected.

Settled: no code change.

@CatarinaGamboa
CatarinaGamboa merged commit fbc5e9f into main Nov 9, 2026
1 check passed
CatarinaGamboa added a commit that referenced this pull request Nov 9, 2026
Refs #435 (first part: named sets only; carrying a set per branch stays
open).

## What
`@StateSet` takes an optional `name`:

```java
@stateset(name = "phase", value = {"start", "canEdit", "end"})
@stateset(name = "undo",  value = {"undoable", "notUndoable"})
@stateset(name = "redo",  value = {"redoable", "notRedoable"})
```

A named set's ghost function is called by that name instead of the
generated `stateN`. Messages, hints, simplification steps and the
equalities that keep unmentioned sets
(`AuxStateHandler.getMissingStates`) show `redo(um)` instead of
`state3(um)`. Specs can use the name anywhere a ghost works, for example
`to = "return ? undoable(this) : undo(this) == undo(old(this))"`. It
works in classes, interfaces and `@ExternalRefinementsFor` specs.
Unnamed sets keep `stateN` with the same numbering, so existing specs
behave the same.

Checks, all reported as `Error` (`CustomError`, the same kind as "State
names must start with lowercase"), on the name:
- not a string literal (e.g. a constant)
- not an identifier starting with a lowercase letter, or
`old`/`true`/`false`
- already the name of a state, a `@Ghost`/`@RefinementPredicate` ghost
or another set of the class (generated `stateN` included). An alias
can't clash, because alias names start with an uppercase letter.

## Before / after
Study variant A1_56 (redo guarded, undo not), with the names
`phase`/`undo`/`redo` on the spec:

Before:
```
State Refinement Error: found (#canRedo_result ? redoable(undoManager@70) : notRedoable(undoManager@70)) && state2(undoManager@70) == state2(#getUndoManager_result) && limit(undoManager@70) == limit(#getUndoManager_result) && (#canRedo_result ? undoable(undoManager) && notRedoable(undoManager) && state1(undoManager) == state1(#getUndoManager_result) && ... but expected undoable(undoManager)
```
After:
```
State Refinement Error: found (#canRedo_result ? redoable(undoManager@70) : notRedoable(undoManager@70)) && undo(undoManager@70) == undo(#getUndoManager_result) && limit(undoManager@70) == limit(#getUndoManager_result) && (#canRedo_result ? undoable(undoManager) && notRedoable(undoManager) && phase(undoManager) == phase(#getUndoManager_result) && ... but expected undoable(undoManager)
```

`ErrorStateHintMultipleSets` with its sets named `doorState` and
`lockState` (rebased on #495, which leaves out the unrelated
`unlocked(d@26)`):
```
- found closed(d@26) && locked(d) && state1(d) == state1(d@26) but expected closed(d) && unlocked(d)
+ found closed(d@26) && locked(d) && doorState(d) == doorState(d@26) but expected closed(d) && unlocked(d)
```

New errors:
```
Error: State set name 'Door' must be an identifier that starts with a lowercase letter
Error: State set name 'locked' is already the name of a state of ErrorStateSetNameOfAState
Error: State set name 'lock' is already the name of a ghost of ErrorStateSetNameOfAGhost
Error: State set name 'door' is already the name of another state set of ErrorStateSetNameOfAnotherSet
Error: State set name must be a string literal
```

## Tests (each fails on main)
- `state_hint_study_undo_manager_correct`: an external spec with named
sets. `canUndo()` keeps its undo set by name (`undo(this) ==
undo(old(this))`) when it returns false. On main the name is unknown, so
this fails.
- `ErrorStateSetNames`: invalid name and clashes with a state, a ghost
and another set.
- `TestStateHints#foundKeepsANamedStateSetByItsName`: exact message with
`doorState(d)`.

Full suite passes locally (714 tests). The 6 study exercises and the 19
study variants give byte-identical output to main without names. With
names on every multi-set exercise spec, the verdict and line stay the
same.

## API release needed
`name()` is new in `liquidjava-api`. The in-repo api is still `0.0.7`,
which is already published without `name`. Reactor builds from the root
(CI and the shaded jar) use the in-repo module. A standalone build of
the verifier, or any user project (VS Code study projects included),
resolves the published `0.0.7`, where `@StateSet(name = ...)` does not
compile. Before anyone uses names: `./release.sh api` (→ `0.0.8`, which
also bumps the verifier/example dependency), then a verifier release.
Nothing is published in this PR.

## Notes
- `old` works on the receiver, as for every ghost: `undo(old(this))`.
`old(undo(this))` (the form in the issue) is not supported for any ghost
today.
- A `to` that only mentions the set function (e.g. `undo(this) == ...`
without any of its states) does not count as mentioning the set, so the
keep equality is still added (same as for `stateN` today).
- vscode-liquidjava (not changed here): `server/.../GhostDTO` marks
states with the regex `^state\d+\(_\) == \d+$`. States of a named set
would show as "ghost" in autocomplete and the context view. Use
`ghostState.getParent() != null` instead. Set names are not offered in
autocomplete, because `ContextHistory` only carries `GhostState`s. The
state machine view (liquidjava-fsm) reads `value` of the first
`@StateSet`, so it is unaffected (fsm#8 could use names as titles).

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
CatarinaGamboa added a commit that referenced this pull request Nov 9, 2026
Part of #502 (PR 1, the language). **Depends on #507 and #508.** Until
they are merged, this diff also shows their commits; only the last
commit belongs here.

## What

A spec can write a state as a value of its set:

```java
@stateset(name = "door", value = {"closed", "opened"})
@stateset(name = "lock", value = {"unlocked", "locked"})
...
@StateRefinement(from = "door(this) == closed && lock(this) != locked", to = "door(this) == opened")
void open();
```

`set(e) == state` means `state(e)`, and `set(e) != state` means
`!state(e)`, in either order. The old shorthand stays valid, and the two
can be mixed.

## How

`StateValues.toShorthand` rewrites the new form into the shorthand
**once, where the spec is parsed and in that spec's scope**. Everything
after that is unchanged, and that includes every part that reads states
syntactically: the frame equalities, the state machine, the hints,
`DecidingArgument`, `StateErrorCategories` and #495's relevance pass.
The parse sites are:
- `@Refinement`, via `getRefinementFromAnnotation`;
- method from/to, via `createStatePredicate`;
- constructor `to`, via `setConstructorStates`;
- alias bodies, at registration, where only the alias's parameters are
variables.

Every set is already registered when specs are parsed (#508), so there
is no late resolution, and a caller's variable can never capture a state
name.

**Scope rules** (from the #502 design reviews):
- A variable in scope keeps its meaning. That covers parameters, context
variables, and `_`, `this` and `return`, which are always variables.
Specs that are valid today mean the same.
- A name that is a state of *another* set of the class is an error:
`State 'locked' is not a state of 'door' (its states are closed,
opened)`.
- Only the sets that apply to the argument's type are considered, that
is, sets declared on its class or a supertype. The type comes from
`this`, `_`, a parameter, a context variable, `old(x)`, or an alias's
declared parameter type. When the type is known, any applicable set may
have the state, as with the shorthand. When it is unknown, every set of
that name must have it, and otherwise the name stays an unknown
variable. The rewritten state is named exactly as the shorthand written
in the same place (`a.Sub.door(this) == open` becomes
`a.Sub.open(this)`), so it is resolved the same way.

**Two new checks on `@StateSet`** (⚠️ breaking for specs that have this
ambiguity today: they are now rejected even if the ambiguous state is
never used), because `open(this)` must say which set it means:
- Two sets of one class may not share a state name: `State 'open' is in
two state sets of X: state1 and state2`. That was accepted before, and
the shorthand picked one set silently.
- The #498 name check now covers generated names, so a state called
`state1` next to the unnamed set `state1` is reported.

## Tests (each fails without the change)
- `CorrectStateValues`, written only in the new form: two named sets, a
constructor `to`, from/to, `!=`, a branch that keeps a set via `old`, a
parameter named like a state, an alias used next to a local variable
named like its state, and states named `_` and `return`.
- `ErrorStateValues`: the wrong-set error, a state name shared by two
sets, and a state named like a generated set.
- `TestStateHints#queryWrittenWithItsStateSetIsOfferedAsAGuard`: a query
written in the new form is still offered as a guard, so the state
machine reads it.

The full suite passes locally. Found while testing: #509, an existing
false error for a boolean query in an ordinary class, with either form.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
CatarinaGamboa added a commit that referenced this pull request Nov 9, 2026
…#511)

Part of #502 (PR 2, messages). **Depends on #510**, which depends on
#507 and #508. Only the last commit belongs here.

## What

State errors show the states of **named** sets as values of their sets,
whichever form the spec used. `ErrorStateHintMultipleSets` (sets
`doorState` and `lockState`):

```
- found closed(d@26) && locked(d) && doorState(d) == doorState(d@26) but expected closed(d) && unlocked(d)
+ found doorState(d@26) == closed && lockState(d) == locked && doorState(d) == doorState(d@26) but expected doorState(d) == closed && lockState(d) == unlocked
```

It is now plain which set fails (only `lockState`), and the frame
equality chains on its own: `doorState(d) == doorState(d@26)` and
`doorState(d@26) == closed`.

`!open(x)` is shown as `door(x) != open`. A state keeps the shorthand
when its set has a generated name (`state2(x) == closed` reads worse
than `closed(x)`), or when sets with different names have a state of
that name. That way the set printed is never a guess.

## How

`StateValues.toSetForm` rewrites expressions for display only.
`StateRefinementError` receives it as `display`:
- **The facts to show are still chosen on the expressions as given.**
`linkedToExpected` and #495's relevance step run first. A shared state
name such as `closed`, which is a variable in the rewritten form, would
otherwise link facts about unrelated objects.
- The message, `getExpected()` and every derivation step
(`VCSimplificationResult.map`) hold the rewritten form, so the **IDE
shows it without a server change**: the server formats those expressions
as they are.

Nothing the verifier decides changes: the VC, the verdict, the hints and
the category are the same. Refinement errors (`RefinementError`) keep
the shorthand for now. They also build alias expansions and
counterexamples from the same expressions, so that would be its own
change.

`Formats.STATE_SET` names the generated `stateN`, which the display
needs to recognise.

## Tests
- `TestStateHints#foundKeepsANamedStateSetByItsName`: the message above.
It fails without the change.

The full suite passes locally.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add an expected-guided simplification step to state error derivations

1 participant