diff --git a/RELEASES.md b/RELEASES.md index 47ac9babba..50f355c8ce 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -1,6 +1,10 @@ Unreleased --------- +* In `macro ... xs:p* ...` and similar macro bindings of combinators, `xs` now has the correct type `Array Syntax` + +* Identifiers in syntax patterns now ignore macro scopes during matching. + * Improve binder names for constructor auto implicit parameters. Example, given the inductive datatype ```lean inductive Member : α → List α → Type u diff --git a/src/Init/Prelude.lean b/src/Init/Prelude.lean index b1d1fb57f9..c81497dfba 100644 --- a/src/Init/Prelude.lean +++ b/src/Init/Prelude.lean @@ -1910,19 +1910,6 @@ def getId : Syntax → Name | ident _ _ val _ => val | _ => Name.anonymous -def matchesNull (stx : Syntax) (n : Nat) : Bool := - isNodeOf stx nullKind n - -def matchesIdent (stx : Syntax) (id : Name) : Bool := - and stx.isIdent (beq stx.getId id) - -def matchesLit (stx : Syntax) (k : SyntaxNodeKind) (val : String) : Bool := - match stx with - | Syntax.node _ k' args => and (beq k k') (match args.getD 0 Syntax.missing with - | Syntax.atom _ val' => beq val val' - | _ => false) - | _ => false - def setArgs (stx : Syntax) (args : Array Syntax) : Syntax := match stx with | node info k _ => node info k args @@ -2209,6 +2196,31 @@ def defaultMaxRecDepth := 512 def maxRecDepthErrorMessage : String := "maximum recursion depth has been reached (use `set_option maxRecDepth ` to increase limit)" +namespace Syntax + +def matchesNull (stx : Syntax) (n : Nat) : Bool := + stx.isNodeOf nullKind n + +/-- + Function used for determining whether a syntax pattern `` `(id) `` is matched. + There are various conceivable notions of when two syntactic identifiers should be regarded as identical, + but semantic definitions like whether they refer to the same global name cannot be implemented without + context information (i.e. `MonadResolveName`). Thus in patterns we default to the structural solution + of comparing the identifiers' `Name` values, though we at least do so modulo macro scopes so that + identifiers that "look" the same match. This is particularly useful when dealing with identifiers that + do not actually refer to Lean bindings, e.g. in the `stx` pattern `` `(many($p)) ``. -/ +def matchesIdent (stx : Syntax) (id : Name) : Bool := + and stx.isIdent (beq stx.getId.eraseMacroScopes id.eraseMacroScopes) + +def matchesLit (stx : Syntax) (k : SyntaxNodeKind) (val : String) : Bool := + match stx with + | Syntax.node _ k' args => and (beq k k') (match args.getD 0 Syntax.missing with + | Syntax.atom _ val' => beq val val' + | _ => false) + | _ => false + +end Syntax + namespace Macro /- References -/