Fix SMT encoding for polymorphic Sequence preconditions (#1201)#1205
Open
PROgram52bc wants to merge 2 commits into
Open
Fix SMT encoding for polymorphic Sequence preconditions (#1201)#1205PROgram52bc wants to merge 2 commits into
PROgram52bc wants to merge 2 commits into
Conversation
PR #1100 introduced bounds preconditions on `Sequence.select`/`update`/ `take`/`drop`. Their precondition expressions carry the type variable `%a` (e.g. `Sequence.length(s : Sequence %a)`), and `PrecondElim` runs before type checking — so `%a` survives into the generated obligation and the SMT encoder fails with `Unimplemented encoding for type var`. Two fixes: - `Lambda.Preconditions.collectWFObligations` now applies a call-site type substitution to the substituted precondition. The substitution is derived from the actuals' types (via `LFunc.opTypeSubst` / argument unification), so a call like `Sequence.select(s, 0)` with `s : Sequence bv32` produces an obligation with `Sequence bv32` rather than `Sequence %a`. - `Core.SMTEncoder.LMonoTy.toSMTType` treats unresolved `.ftvar` encodings as fresh uninterpreted sorts instead of erroring out. This covers the residual cases where no concrete type is reachable (e.g. inside a polymorphic function body like `Seq_lib_insert<T>`, or when every argument is itself polymorphic). The solver then reports `unknown` instead of the verifier crashing. Tests: - `PreconditionsTests`: two new cases pinning the new substitution behaviour for an annotated `.op` and for the argument-type fallback. - `seq_slicing`: `seqOutOfBoundsSeed` `#guard_msgs` now reflects the bounds-check obligation (now `unknown`, no encoder error). - `sha256_compact_indexed`: `#guard_msgs` updated to include the new bounds obligations (`unknown` for the unproven ones, `pass` where contracts already discharge them).
Contributor
Author
|
Seems like a similar fix is needed for main, but the problem is surfaced only on main2 (by tests added in PR #1075). |
MikaelMayer
reviewed
May 21, 2026
Contributor
MikaelMayer
left a comment
There was a problem hiding this comment.
🤖🔍 Clean fix for the polymorphic precondition encoding issue. The two-pronged approach (resolve at the precondition level + graceful fallback in the encoder) is sound. One design concern about code duplication.
Address review feedback on #1205: the docstring now explicitly names `LFunc.computeTypeSubst` in `Factory.lean`, explains why the priority is reversed (pre- vs. post-typecheck `.op` reliability), and notes why the two helpers cannot be merged behind a flag without breaking the `Semantics.lean` proofs that depend on `computeTypeSubst`'s shape.
MikaelMayer
reviewed
May 22, 2026
Contributor
MikaelMayer
left a comment
There was a problem hiding this comment.
🤖🔍 All previous comments appear addressed — reviewer sign-off still needed.
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.
Summary
PrecondElimno longer leaves%ain obligations from polymorphicSequence.*preconditions, and the SMT encoder no longer fails on residual unresolved type variables.Preconditions.lean: derives a call-site type substitution from the actuals' types (with the.opannotation as fallback) and applies it to the substituted precondition expression.SMTEncoder.lean: encodes a leftover.ftvaras a fresh uninterpreted sort instead of returningUnimplemented encoding for type var. Verification reportsunknownfor those cases (sound, just weaker).Why this branch
The two failing tests (
sha256_compact_indexed.lean,seq_slicing'sseqOutOfBoundsSeed) were added in this branch's Boole language-extensions work (#1075) and are what surfaced the bug — main's existing Sequence tests use Core'sSequence.empty<T>()syntax which annotates the.opwith a concrete type and masks the bug. A backport PR againstmainwill follow with the same source-file fixes.Why two changes
Most call sites (e.g.
Sequence.select(state_out, 0)withstate_out : Sequence bv32) get a concrete substitution from arg types and produce well-typed obligations. The encoder fallback covers the remaining cases the issue calls out: calls inside a polymorphic function body (Seq_lib_insert<T>) and calls whose every argument is itself polymorphic (Sequence.select(Sequence.empty_int, 0)).Test plan
lake build StrataTest— all 614 targets pass.StrataTest/DL/Lambda/PreconditionsTests.lean— added two cases pinning the new behaviour: annotated.oppath and argument-type fallback.StrataTest/Languages/Boole/FeatureRequests/seq_slicing.lean—seqOutOfBoundsSeed#guard_msgsupdated; the SMT encoding error is gone, replaced byunknown.StrataTest/Languages/Boole/FeatureRequests/sha256_compact_indexed.lean—#guard_msgsupdated to include the new bounds-check obligations (unknownwhere the procedure has no length precondition,passwhere it does).lake test— only pre-existing failure is theion-javajar fixture, unrelated.