feat: notation: unfold to prechecked quotation
diff --git a/doc/syntax.md b/doc/syntax.md index 3752323..7858e8a 100644 --- a/doc/syntax.md +++ b/doc/syntax.md
@@ -17,6 +17,7 @@ infix:50 " = " => Eq -- non-associative infixr:80 " ^ " => HPow.hPow -- right-associative prefix:100 "-" => Neg.neg +# set_option quotPrecheck false postfix:max "⁻¹" => Inv.inv ``` @@ -30,6 +31,7 @@ notation:50 lhs:51 " = " rhs:51 => Eq lhs rhs notation:80 lhs:81 " ^ " rhs:80 => HPow.hPow lhs rhs notation:100 "-" arg:100 => Neg.neg arg +# set_option quotPrecheck false notation:1024 arg:1024 "⁻¹" => Inv.inv arg -- `max` is a shorthand for precedence 1024 ``` @@ -41,6 +43,7 @@ In contrast, `infixr` reuses the notation's precedence for the right-hand side operand, so `a ^ b ^ c` *can* be parsed as `a ^ (b ^ c)`. Note that if we used `notation` directly to introduce an infix notation like ```lean +# set_option quotPrecheck false notation:65 lhs:65 " ~ " rhs:65 => wobble lhs rhs ``` where the precedences do not sufficiently determine associativity, Lean's parser will default to right associativity. @@ -49,6 +52,7 @@ As mentioned above, the `notation` command allows us to define arbitrary *mixfix* syntax freely mixing tokens and placeholders. ```lean +# set_option quotPrecheck false notation:max "(" e ")" => e notation:10 Γ " ⊢ " e " : " τ => Typing Γ e τ ```
diff --git a/src/Lean/Elab/Quotation/Precheck.lean b/src/Lean/Elab/Quotation/Precheck.lean index a43b774..072212e 100644 --- a/src/Lean/Elab/Quotation/Precheck.lean +++ b/src/Lean/Elab/Quotation/Precheck.lean
@@ -25,13 +25,22 @@ protected def withNewLocals (ls : Array Name) (x : PrecheckM α) : PrecheckM α := withReader (fun ctx => { ctx with quotLCtx := ls.foldl NameSet.insert ctx.quotLCtx }) x +register_builtin_option quotPrecheck : Bool := { + defValue := true + descr := "Enable eager name analysis on notations in order to find unbound identifiers early. +Note that type-sensitive syntax (\"elaborators\") needs special support for this kind of check, so it might need to be turned off when using such syntax." +} + unsafe def mkPrecheckAttribute : IO (KeyedDeclsAttribute Precheck) := KeyedDeclsAttribute.init { builtinName := `builtinQuotPrecheck, name := `quotPrecheck, - descr := "Register a double backtick syntax quotation precheck. + descr := "Register a double backtick syntax quotation pre-check. - [quotPrecheck k] registers a declaration of type `Lean.Elab.Term.Quotation.Precheck` for the `SyntaxNodeKind` `k`.", +[quotPrecheck k] registers a declaration of type `Lean.Elab.Term.Quotation.Precheck` for the `SyntaxNodeKind` `k`. +It should implement eager name analysis on the passed syntax by throwing an exception on unbound identifiers, +and calling `precheck` recursively on nested terms, potentially with an extended local context (`withNewLocal`). +Macros without registered precheck hook are unfolded, and identifier-less syntax is ultimately assumed to be well-formed.", valueTypeName := ``Precheck } `Lean.Elab.Term.Quotation.precheckAttribute @[builtinInit mkPrecheckAttribute] constant precheckAttribute : KeyedDeclsAttribute Precheck @@ -47,7 +56,8 @@ if let some stx' ← liftMacroM <| Macro.expandMacro? stx then precheck stx' return - throwErrorAt stx "no macro or precheck hook for syntax kind '{stx.getKind}' found{indentD stx}" + throwErrorAt stx "no macro or `[quotPrecheck]` instance for syntax kind '{stx.getKind}' found{indentD stx} +This means we cannot eagerly check your notation/quotation for unbound identifiers; you can use `set_option quotPrecheck false` to disable this check." where hasIdent stx := do for stx in stx.topDown do @@ -56,7 +66,8 @@ return false def runPrecheck (stx : Syntax) : TermElabM Unit := do - if hygiene.get (← getOptions) then + let opts ← getOptions + if quotPrecheck.get opts && hygiene.get opts then precheck stx |>.run { quotLCtx := {} } @[builtinQuotPrecheck ident] def precheckIdent : Precheck
diff --git a/src/Lean/Elab/Syntax.lean b/src/Lean/Elab/Syntax.lean index 015145a..9f40c5b 100644 --- a/src/Lean/Elab/Syntax.lean +++ b/src/Lean/Elab/Syntax.lean
@@ -485,7 +485,7 @@ let fullName := currNamespace ++ name let pat := Syntax.node fullName patArgs let stxDecl ← `($attrKind:attrKind syntax $[: $prec?]? (name := $(mkIdent name)) (priority := $(quote prio):numLit) $[$syntaxParts]* : $cat) - let macroDecl ← `(macro_rules | `($pat) => `($qrhs)) + let macroDecl ← `(macro_rules | `($pat) => ``($qrhs)) match (← mkSimpleDelab attrKind vars pat qrhs |>.run) with | some delabDecl => mkNullNode #[stxDecl, macroDecl, delabDecl] | none => mkNullNode #[stxDecl, macroDecl]
diff --git a/tests/lean/277a.lean b/tests/lean/277a.lean index b6b0a01..c02d1d4 100644 --- a/tests/lean/277a.lean +++ b/tests/lean/277a.lean
@@ -1,3 +1,4 @@ +set_option hygiene false infixl:67 " <>< " => nonexistant #eval (1 <>< 11 : UInt64)
diff --git a/tests/lean/277a.lean.expected.out b/tests/lean/277a.lean.expected.out index dfd9df9..d6624f9 100644 --- a/tests/lean/277a.lean.expected.out +++ b/tests/lean/277a.lean.expected.out
@@ -1,2 +1,2 @@ -277a.lean:3:7-3:15: error: unknown identifier 'nonexistant✝' -277a.lean:3:0-3:25: error: cannot evaluate code because it uses 'sorry' and/or contains errors +277a.lean:4:7-4:15: error: unknown identifier 'nonexistant' +277a.lean:4:0-4:25: error: cannot evaluate code because it uses 'sorry' and/or contains errors
diff --git a/tests/lean/StxQuot.lean.expected.out b/tests/lean/StxQuot.lean.expected.out index 29d14be..9985cd2 100644 --- a/tests/lean/StxQuot.lean.expected.out +++ b/tests/lean/StxQuot.lean.expected.out
@@ -50,5 +50,6 @@ "`id._@.UnhygienicMain._hyg.1" "`pure._@.UnhygienicMain._hyg.1" "(Term.fun \"fun\" (Term.basicFun [`x._@.UnhygienicMain._hyg.1] \"=>\" `x._@.UnhygienicMain._hyg.1))" +StxQuot.lean:105:22-105:23: error: unknown identifier 'y' "(Term.fun\n \"fun\"\n (Term.basicFun\n [`x._@.UnhygienicMain._hyg.1 `y._@.UnhygienicMain._hyg.1]\n \"=>\"\n (Term.app `x._@.UnhygienicMain._hyg.1 [`y._@.UnhygienicMain._hyg.1])))" "(Term.fun\n \"fun\"\n (Term.basicFun\n [(Term.anonymousCtor \"⟨\" [`x._@.UnhygienicMain._hyg.1 \",\" `y._@.UnhygienicMain._hyg.1] \"⟩\")]\n \"=>\"\n `x._@.UnhygienicMain._hyg.1))"
diff --git a/tests/lean/autobound_and_macroscopes.lean b/tests/lean/autobound_and_macroscopes.lean index c679805..6040939 100644 --- a/tests/lean/autobound_and_macroscopes.lean +++ b/tests/lean/autobound_and_macroscopes.lean
@@ -1,2 +1,2 @@ -local notation "A" => id x +macro "A" : term => `(id x) theorem test : A = A := sorry
diff --git a/tests/lean/notationPrecheck.lean b/tests/lean/notationPrecheck.lean new file mode 100644 index 0000000..5bee76f --- /dev/null +++ b/tests/lean/notationPrecheck.lean
@@ -0,0 +1,8 @@ +notation "a" => fun x => a + +syntax "b" term : term +notation "c" => b x + +set_option quotPrecheck false +notation "d" => b x +#check d
diff --git a/tests/lean/notationPrecheck.lean.expected.out b/tests/lean/notationPrecheck.lean.expected.out new file mode 100644 index 0000000..b505e20 --- /dev/null +++ b/tests/lean/notationPrecheck.lean.expected.out
@@ -0,0 +1,8 @@ +notationPrecheck.lean:1:25-1:26: error: unknown identifier 'a' +notationPrecheck.lean:1:0-1:26: error: cannot evaluate code because it uses 'sorry' and/or contains errors +notationPrecheck.lean:4:16-4:19: error: no macro or `[quotPrecheck]` instance for syntax kind 'termB_' found + b x +This means we cannot eagerly check your notation/quotation for unbound identifiers; you can use `set_option quotPrecheck false` to disable this check. +notationPrecheck.lean:4:0-4:19: error: cannot evaluate code because it uses 'sorry' and/or contains errors +notationPrecheck.lean:8:7-8:8: error: elaboration function for 'termB_' has not been implemented + b x✝