Skip to content

fix: support level antiquotations in explicit universe annotations x.{$u} - #176

Open
josaum wants to merge 4 commits into
leanprover-community:masterfrom
josaum:fix/explicit-univ-level-antiquot
Open

josaum wants to merge 4 commits into
leanprover-community:masterfrom
josaum:fix/explicit-univ-level-antiquot

Conversation

@josaum

@josaum josaum commented Nov 7, 2026

Copy link
Copy Markdown

Fixes #16.

Problem

Inside q(·), an antiquotated level variable works in some positions (Sort $u) but not in explicit universe annotations:

open Lean Qq
example (u : Level) (T : Q(Sort u)) (t : Q($T)) := q(@id.{$u} $T $t) -- unknown universe level '«$u»'
example (u : Level) (T : Q(Sort $u)) (t : Q($T)) := q(@id.{$u} $T $t) -- unknown universe level '«$u»'
example (u : Level) (T : Q(Sort u)) (t : Q($T)) := q(@id.{u} $T $t) -- works

Root cause

floatExprAntiquot' (Qq/Macro.lean) special-cases Type $l/Sort $l, routing the level through floatLevelAntiquot', but has no case for x.{u, ...} (Lean.Parser.Term.explicitUniv) nodes. The generic traversal then treats a level antiquotation $u inside .{} as an expression antiquotation, turning it into the identifier $u in level position, which level elaboration rejects with unknown universe level '«$u»'.

A first attempt to simply reuse floatLevelAntiquot' for the level list (floating let u' : Level := u, as for Type/Sort) fixed the parse error but produced Sort u vs Sort u✝ type mismatches: the floated binding becomes a fresh level parameter .param u', while antiquotations like $T refer to .param u — universe parameters compare by name, so the term-level let u' := u cannot mediate. (For Type $u/Sort $u the fresh parameter never needs to be compared with the original, which is why floating works there.)

Fix

New floatExplicitUnivLevel' used for the level list (argument 2) of explicitUniv nodes at quotation depth 0:

  • a plain identifier antiquotation $u is replaced by the bare level u, behaving exactly like writing x.{u} — the level name is linked back to the local Level variable by the existing unquoteLevelLCtx/withLevelNames machinery;
  • composite antiquotations (e.g. x.{$(u+1)}) are floated into a let binding as before (same support level as Type/Sort today);
  • the head of the annotation (which may itself be an antiquotation) is still processed as a term, and at positive nesting depth (q(q(...))) the generic traversal is unchanged, so inner quotations' antiquotations are preserved.

Tests

New QqTest/levelExplicitUnivAntiquot.lean (picked up by the QqTest.+ glob):

  • all four examples from the issue (the two failing ones now elaborate);
  • q(@id.{$u} $T $t) = q(@id.{u} $T $t) by rfl, pinning that $u and u produce the same quotation;
  • multiple explicit universe levels, including mixed (@Prod.mk.{$u, $v}, @Function.const.{$u, $v});
  • antiquotation of a level variable introduced by variable, with a #guard_msgs #check.

On unmodified master the new file fails with the issue's exact error (unknown universe level '«$u»', 9 errors); with this fix it builds cleanly.

Verification

On the pinned toolchain (leanprover/lean4:v4.36.0-rc2, master b025662):

  • lake build --wfail — passes (30 jobs, no warnings)
  • lake test — passes (exit 0; full QqTest suite, 59 jobs, including the new file)
  • Teeth check: reverting Qq/Macro.lean to master makes the new regression file fail as described above.
  • No sorry/admit introduced; the change is confined to Qq/Macro.lean (meta-only) plus the new test file.
  • Both pushed files verified byte-identical (git hash-object) to the locally built and tested sources: Qq/Macro.lean = 22335cd3ed5841ec9fddff25746c477207abe422, QqTest/levelExplicitUnivAntiquot.lean = 2af9a9318c6cde7ec001fc561062de1a15ee0b78. (Two commits because the initial push had transcription slips in unrelated lines — caught by the hash check and fully corrected in the second commit; feel free to squash.)

Branch note: the branch is based on my fork's master (859a214), whose tree is identical to upstream b025662 (verified via the identical Qq/Macro.lean blob 80dd267), so the PR diff is exactly the two files above.

Known limitation (pre-existing, unchanged): a composite level antiquotation such as q(@id.{$(u+1)} $T $t) still cannot be defeq-matched against T : Q(Sort (u+1)), since the floated binding introduces a fresh level parameter — the same limitation Type $(u+1) has today.

The file content is now byte-identical to the locally built and tested
Qq/Macro.lean (git hash-object 22335cd).
…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.levelExplicitUnivAntiquot).
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.

foo.{$u} is not supported

1 participant