Skip to content

Return MatchIndeterminate (not MatchFailed) for mixed-determinacy matches in Eval and Implies modes#4152

Draft
ehildenb wants to merge 13 commits into
masterfrom
match-indeterminate-bug
Draft

Return MatchIndeterminate (not MatchFailed) for mixed-determinacy matches in Eval and Implies modes#4152
ehildenb wants to merge 13 commits into
masterfrom
match-indeterminate-bug

Conversation

@ehildenb

@ehildenb ehildenb commented Jun 8, 2026

Copy link
Copy Markdown
Member

The booster matcher's job is to return the truth — terms either definitely cannot match (MatchFailed) or cannot be decided without further simplification (MatchIndeterminate) — and leave it to the caller to decide what to do with an indeterminate verdict. In Eval and Implies modes the matcher previously short-circuited to a decisive MatchFailed in cases where the two terms could in fact simplify to something equivalent, so the deferral never reached the caller.

Two shapes were affected: a pattern variable rebinding to two terms that are not both constructor-like (e.g. a domain value and a function application), and a FunctionApplication pattern matched against a concrete structured subject (DomainValue/Injection/KMap/KList/KSet). Both now route through addIndeterminate, matching what Rewrite mode already did.

This matters most for function-equation evaluation: handleFunctionEquation routes FailedMatch _ -> continue but IndeterminateMatch{} -> abort. The old decisive MatchFailed caused the evaluator to silently skip a higher-priority equation and commit to a lower-priority catch-all, violating the priority contract function equations rely on. For Implies, the indeterminate verdict lets the existing simplify-LHS / simplify-RHS retry ladder in Pattern.Implies attempt the discharge and report a residual non-match as indeterminate rather than a decisive valid:false. Simplification behaviour is unchanged — handleSimplificationEquation routes both verdicts to continue.

Changes:

  • Match.bindVariable: the mixed-determinacy rebind case now returns addIndeterminate for all match modes instead of MatchFailed VariableConflict for everything but Rewrite.
  • match1: dropped the five Eval-specific FunctionApplication vs structured-subject lines so they fall through to the shared addIndeterminate catch-all already used by Rewrite/Implies.
  • Added unit tests pinning the post-fix contract: matcher-level cases in MatchEval and MatchImplies, plus a paired function/simplification soundness-regression test in ApplyEquations (test_soundnessGap) that shows a high-priority indeterminate match correctly aborts function-equation evaluation rather than falling through to a lower-priority rule.

Validation:

  • Full booster unit-test suite passes (731 tests). The new tests were authored to fail against the pre-fix matcher and turn green only with the Match.hs changes applied.

ehildenb and others added 6 commits June 8, 2026 19:52
…le-rebind with mixed-determinacy subject

When the matcher (in Implies mode) sees a pattern variable bound first to a
domain value and then to a function application (or vice versa), it currently
returns a decisive MatchFailed VariableConflict, even though the function
application could simplify into the domain value (or anything equivalent).
The pyk team's recover-mode sweep depends on a sound MatchIndeterminate
verdict here so the implies handler's existing simplify-LHS / simplify-RHS
retry ladder can attempt the discharge, and a residual non-match can be
reported with indeterminate:true rather than as a decisive valid:false.

Pin both orderings of the rebind. Tests are expected to fail until
Match.bindVariable in Implies mode mirrors Rewrite mode's addIndeterminate.

(cherry picked from commit 1700d2d0f5dd89836c86a92f057275ac15756a64)
… mode through addIndeterminate on mixed-determinacy rebind

In Match.bindVariable, when a variable is bound a second time and the two
terms are not both constructor-like, the matcher previously failed with a
decisive VariableConflict in both Eval and Implies modes. That is sound for
Eval (the equation evaluator skips to the next priority on MatchFailed) but
unsound for Implies: the comment two lines up — "the term in the binding
could be equivalent (not necessarily syntactically equal)" — captures
exactly why such a decision should be deferred. Routing this case through
addIndeterminate lets the existing simplify-LHS / simplify-RHS retry ladder
in Pattern.Implies attempt to discharge the equivalence and, on residual
failure, mark the implication indeterminate so recover-mode pyk can
escalate to a kore-enabled retry.

Eval keeps the failWith path pending a separate soundness review of the
function-equation evaluator, which currently treats MatchFailed as "skip and
try next priority" while it treats MatchIndeterminate as "abort". The
comment on the carve-out documents this distinction.

Also flip the pre-existing "Matching the same variable in a constructor"
test in MatchImplies from MatchFailed to the new MatchIndeterminate
expectation, mirroring MatchRewrite's analogous case.

(cherry picked from commit 262d49858688eced23a309c7be83489c0a258a09)
…sts for Eval matcher soundness gap

In Eval mode the matcher today returns a decisive MatchFailed when:
  - a pattern variable rebinds to two terms that aren't both
    constructor-like (Match.hs:825), and
  - a function-application pattern is matched against a concrete
    structured subject (Injection/KMap/KList/KSet, match1 lines 289,
    291, 293, 295).

The "truth" verdict for both shapes is MatchIndeterminate: the function
side could simplify into something equivalent to the constructor-like
side, and the matcher cannot decide that without further work. Because
handleFunctionEquation (ApplyEquations.hs) routes FailedMatch _ to
continue but IndeterminateMatch{} to abort, the current decisive verdict
silently skips a higher-priority equation and commits to a lower-priority
catch-all, violating the priority contract function equations rely on.

This adds six matcher-level tests (MatchEval.hs) and a paired
function/simplification soundness-regression test (ApplyEquations.hs)
that pin the post-fix contract. All seven new tests are expected to fail
until Match.bindVariable and the five Eval-specific match1 lines mirror
the catch-all behaviour. The simplification companion already passes
today and serves to pin that simplifications are unaffected by the
proposed fix.

(cherry picked from commit a61e40fb842f002b1d315b5785b4a61610b332b0)
…through addIndeterminate on mixed-determinacy match

The matcher's job is to return the truth — terms either definitely
cannot match (MatchFailed) or cannot be decided without simplification
(MatchIndeterminate) — and to let the caller decide what to do. Eval
mode previously short-circuited in two places where the terms involved
could in fact simplify to be equivalent:

  - Match.bindVariable rebind where the two terms are not both
    constructor-like (e.g. a domain value and a function application):
    previously failWith $ VariableConflict; now addIndeterminate, like
    Rewrite and Implies.

  - Five match1 lines that pair a FunctionApplication pattern with a
    concrete structured subject (DomainValue, Injection, KMap, KList,
    KSet): previously failWith $ DifferentSymbols in Eval mode; the
    Eval-specific lines are now dropped so the same catch-all that
    handles Rewrite/Implies (addIndeterminate) handles Eval too.

The downstream contract — handleFunctionEquation routes
@IndeterminateMatch{} -> abort@ while @FailedMatch _ -> continue@ — now
correctly stops function-equation iteration at an indeterminate match
instead of silently skipping a higher-priority equation and committing
to a lower-priority catch-all. Function-equation priorities are
"semantically binding" (per the comment on handleFunctionEquation), so
preserving priority order under the matcher's deferred verdict is the
sound thing to do.

handleSimplificationEquation routes both verdicts to @continue@, so
simplification behaviour is unchanged. The simplification companion in
test_soundnessGap pins this.

Also flip the two pre-existing MatchEval tests whose expectations
become stale ("function and something else" and "Matching two
constructor argument to be the same (failing)") from MatchFailed to
the new MatchIndeterminate verdict.

(cherry picked from commit b004b6f5270a57ff620877bbdd783a369c924a24)
The mixed-determinacy rebind comment had grown to a verbose paragraph.
Restore master's concise wording, keeping only the one new fact: all
modes now defer (a decisive MatchFailed would skip higher-priority
equations) rather than re-listing every caller's discharge mechanism.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Comment thread booster/library/Booster/Pattern/Match.hs Outdated
ehildenb and others added 7 commits June 11, 2026 20:34
…eword test docs to post-fix framing

The test-first commits documented the soundness gap as present ("currently
failing", "under the proposed fix"), and the fix commits never updated the
wording, leaving group names claiming tests fail while they pass. Reword to
describe the pinned contract, mentioning the pre-fix behaviour as history.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
…ws into a catch-all

All non-special-case rows for a FunctionApplication pattern resolve to
addIndeterminate in every mode, so replace the per-subject-constructor rows
(DomainValue/Injection/KMap/KList/KSet in all modes, ConsApplication and
FunctionApplication outside Eval, Var outside Rewrite) with a single trailing
catch-all. The mode-specific rows (AndTerm handling, Eval symbol-application
descent, Rewrite subject-variable match) stay explicit above it. No behaviour
change; addresses review feedback on #4152.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
…s-builtin-collection match indeterminate both ways in Eval

match1 was asymmetric for Injection paired with a builtin collection (KMap/KList/KSet) under Eval: the
collection-pattern/Injection-subject direction returned addIndeterminate while the commuted
Injection-pattern/collection-subject direction returned a decisive failWith DifferentSymbols. A spurious
MatchFailed silently skips a higher-priority function equation, so the sound verdict is indeterminate in
both directions. Add the Eval indeterminate carve-out to the Injection rows; Rewrite/Implies stay decisive.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
…: defer matchVar sort mismatch when the subject can narrow

A pattern variable matched against a subject whose static sort is not a
subsort of the variable's sort decisively failed with DifferentSorts. That is
only sound when the subject's sort is exact: a function application may
evaluate to a term of a narrower sort (matchInj already defers on exactly this
ground), and a subject variable may be instantiated with one. Defer as
indeterminate when the subject is a FunctionApplication or Var and the two
sorts share a subsort; sort-disjoint pairs and rigid subjects keep the
decisive failure. New helper sortsOverlap intersects the reflexive-transitive
subsort closures from the SortTable.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
…hen a child on the wider-sorted side can narrow

The inj-vs-inj fall-through decisively failed with DifferentSorts whenever the
sources differed and the special cases (subject function child, pattern
variable child) did not apply. That is only sound for rigid children: a
subject variable child of the wider sort may be instantiated in the pattern's
source sort, and a pattern function child of the wider sort may evaluate into
the subject's. Restructure the post-subsort-check dispatch as a single case
over both children, deferring those two shapes and keeping the decisive
failure for rigid children at incompatible sorts.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
…tch-alls, drop dead MatchType params

Pure refactor, no behaviour change. The match1 table shrinks from ~108 rows to
46 by: hoisting the per-block \and-subject rows into two generic rows after
the \and-pattern block; closing every pattern block with a catch-all (the
bulk indeterminate or decisive-fail outcome) so only special rows are
explicit; collapsing the Var-pattern block into a single matchVar dispatch;
merging the Rewrite/Implies DomainValue-vs-Var rows; and deleting the Eval
ConsApplication/FunctionApplication cross-kind descents through
matchSymbolAplications, which always resolved to addIndeterminate because
constructor and function symbol names never coincide. A header comment states
the block order and the rigidity principle governing decisive vs indeterminate
outcomes. bindVariable and matchInj no longer inspect their MatchType argument
(all modes defer identically since the mixed-determinacy fix), so the dead
parameters are removed.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
…e generic addIndeterminate fall-through

Pure refactor, no behaviour change. Instead of per-pattern blocks each closed
by their own catch-all, the table now ends in one generic rule resolving every
unhandled pair to addIndeterminate — the always-sound outcome — and every row
above it must justify something stronger: \and decomposition, variable
binding via matchVar, same-category descent, or decisive failure. The decisive
cross-category rule is a single guarded row over a new isRigidCategory
predicate (domain values, injections, collections, constructor applications),
making the rigidity principle executable rather than commentary. Explicit
addIndeterminate rows remain only where they must shadow a more generic
stronger row: the Eval \and quirks and the Eval injection-vs-collection
carve-out (now two isCollection-guarded rows covering both directions). The
subjectVariableMatch marker now uniformly covers all non-Eval
rigid-or-function patterns against a variable subject (outcome unchanged:
addIndeterminate). 46 rows become 18.

Co-Authored-By: Claude Fable 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.

1 participant