Repository navigation
Conversation
…al context A local declaration whose type is not `Quoted _`/`QuotedDefEq ..`/`Level` and has no `ToExpr` instance used to be silently dropped from a quotation's unquoted local context. Later declarations may still refer to it in their types or let-values, and dropping it left those references dangling, producing `unknown free variable` errors (issue leanprover-community#8) — e.g. an unannotated shadowing `let args := a :: args` in a `do` block, whose inferred type is still a metavariable while a quotation is being elaborated. Keep such declarations in the unquoted context unchanged; they simply cannot be used in antiquotations. `Level`/`QuotedLevelDefEq` declarations (handled by `unquoteLevelLCtx`) and auxiliary declarations (which require `auxDeclToFullName` registration, issue leanprover-community#105) keep being skipped. Fixes leanprover-community#8
…gnature The previous commit accidentally changed `unquoteLevel (e.getArg! 1)` to `unquoteExpr (e.getArg! 1)` in `unquoteLevelList` and mangled the result type of `floatExprAntiquot'`; restore both to the exact master text.
…`do`-block `let`s) The MWE from the issue (a shadowing `let args := a :: args` whose inferred type is still a metavariable during quotation elaboration) failed with `unknown free variable` before the fix; a chain of dependent unannotated `let`s feeding a quotation is covered as well.
Author
|
Small correction to the branch note above: because the PR diff is computed against the merge-base of my fork's |
…th master Upstream master bumped the toolchain past this branch (v4.36.0-rc2 -> v4.36.0 -> v4.37.0-rc1 in leanprover-community#179/leanprover-community#180), conflicting with the rc1->rc2 bump this branch carried from the fork's merge-base. Setting the branch's toolchain to master's makes the merge clean; the PR diff relative to the merge result is unchanged. Verified locally on leanprover/lean4:v4.37.0-rc1 (upstream master ad0c9f8 + this branch, toolchain conflict resolved to master's version): `lake build --wfail` passes (31 jobs, no warnings) and `lake test` passes (exit 0, incl. QqTest.doLetShadowing).
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.
Fixes #8.
Problem
The issue's MWE fails with
unknown free variable '_uniq.419'at the$Fantiquotation, and the presence of a shadowing, unannotatedletin adoblock is what triggers it:(Still reproducible on current
master/v4.36.0-rc2;QQ.checkfrom the 2023 report no longer exists and is not needed to reproduce.)Root cause
unquoteLCtx(Qq/Macro.lean) copies the ambient local context into the quotation's unquoted context. The fallback branch for declarations that are notQuoted _/QuotedDefEq ../Level-typed tries to auto-quote viaToExpr:— and silently drops the declaration otherwise.
The catch: the inferred type of an unannotated
do-blockletis still an unassigned metavariable while a subsequentQ( )/q( )ascription is being elaborated (annotating the let, as in the issue'sthis_works₂, assigns it eagerly — which is why that variant worked).getLevel ?mreturns a fresh level metavariable rather than.succ _, so the declaration — here the shadowingargs— is dropped. But the next let's value (args.length) refers to it, so the unquoted context ends up with a dangling free variable, which surfaces later as theunknown free variableerror at an unrelated antiquotation. (InstrumentingunquoteLCtxshows exactly the dropped declaration's fvar being reported.)Fix
A declaration that cannot be auto-quoted (no
ToExprinstance, or a still-unassigned metavariable type) is now kept in the unquoted context unchanged instead of being dropped. It cannot be used in antiquotations (noexprBackSubstentry), but later declarations that refer to it in their types or let-values stay well-formed. Two categories keep being skipped deliberately:Level/QuotedLevelDefEqdeclarations, which are handled byunquoteLevelLCtx;.auxDecl), which would needauxDeclToFullNameregistration — that's misc/examples.lean currently PANICs #105 / fix: preserve auxDeclToFullName when unquoting the local context #168's domain, and this PR intentionally does not overlap with it (whichever of the two lands second will need a trivial merge in thiselsebranch).A side effect worth noting: class-typed hypotheses that previously vanished from the unquoted context (e.g. a local
[Decidable p]) are now kept, so they are available to instance resolution while the quotation body is elaborated — matching the ambient context more faithfully.Tests
New
QqTest/doLetShadowing.lean(picked up by theQqTest.+glob):shadowedUnannotatedDoLet), plus arun_cmdthat evaluates it and checks the resultingExpris exactly@TypeFun.ofCurried 1 Nat.succ(in core's type-correctOfNat.ofNatnumeral representation);chainedUnannotatedDoLets: a chain of dependent unannotatedlets feeding a quotation, with arun_cmdchecking the quoted value.Both fail on unmodified
master(withunknown free variable '_fvar.N'andUnknown identifier «$m», respectively) and pass with this fix.Verification
On the pinned toolchain (
leanprover/lean4:v4.36.0-rc2, masterb025662):masterfirst; annotated-let and inlined variants from the issue confirmed working there, as reported.lake build --wfail— passes (30 jobs, no warnings).lake test— passes (exit 0; fullQqTestsuite, 59 jobs, including the new file).Qq/Macro.leanmakes the new regression file fail with the errors above.sorry/admitintroduced; the change is meta-only inQq/Macro.leanplus the new test file.git hash-object) to the locally built and tested sources:Qq/Macro.lean=06c0bf75868e27e53079e873867bd3328f3a5cbc,QqTest/doLetShadowing.lean=526112846e7900b7e023eeca3c841b1b214fc8d9. (Three commits because the file upload API is per-file and the first push had two transcription slips in unrelated lines, caught by the hash check and fully corrected in the second; feel free to squash.)Branch note: the branch is based on my fork's master (
859a214), whose tree is identical to upstreamb025662(verified via theQq/Macro.leanbase blob80dd267), so the PR diff is exactly the two files above.