Skip to content

Commit 452129a

Browse files
committed
feat: have notation3 use elaborator when generating matchers, add support for pi/lambda (#6833)
`notation3` was generating matchers directly from syntax, which included a half-baked implementation of a term elaborator. This switches to elaborating the term and then generating matchers from the elaborated term. This 1. is more robust and consistent, since it uses the main elaborator and one can make use of other notations 2. has the nice side effect of adding term info to expansions in the `notation3` command 3. can unfortunately generate matchers that are more restrictive than before since they also match against elaborated features such as implicit arguments. We now also generate matchers for expansions that have pi types and lambda expressions.
1 parent 57e4e03 commit 452129a

File tree

14 files changed

+287
-128
lines changed

14 files changed

+287
-128
lines changed

Mathlib.lean

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2198,6 +2198,7 @@ import Mathlib.Init.ZeroOne
21982198
import Mathlib.Lean.CoreM
21992199
import Mathlib.Lean.Data.NameMap
22002200
import Mathlib.Lean.Elab.Tactic.Basic
2201+
import Mathlib.Lean.Elab.Term
22012202
import Mathlib.Lean.EnvExtension
22022203
import Mathlib.Lean.Exception
22032204
import Mathlib.Lean.Expr
@@ -2215,6 +2216,7 @@ import Mathlib.Lean.Meta.CongrTheorems
22152216
import Mathlib.Lean.Meta.DiscrTree
22162217
import Mathlib.Lean.Meta.Simp
22172218
import Mathlib.Lean.Name
2219+
import Mathlib.Lean.PrettyPrinter.Delaborator
22182220
import Mathlib.Lean.SMap
22192221
import Mathlib.Lean.System.IO
22202222
import Mathlib.Lean.Thunk

Mathlib/Algebra/Lie/Weights/Basic.lean

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ open scoped BigOperators TensorProduct
5555
section notation_weight_space_of
5656

5757
/-- Until we define `LieModule.weightSpaceOf`, it is useful to have some notation as follows: -/
58-
local notation3 (prettyPrint := false) "𝕎("M"," χ"," x")" =>
59-
(toEndomorphism R L M x).maximalGeneralizedEigenspace χ
58+
local notation3 "𝕎("M", " χ", " x")" => (toEndomorphism R L M x).maximalGeneralizedEigenspace χ
6059

6160
/-- See also `bourbaki1975b` Chapter VII §1.1, Proposition 2 (ii). -/
6261
protected theorem weight_vector_multiplication (M₁ M₂ M₃ : Type*)

Mathlib/Algebra/Order/Nonneg/Module.lean

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ variable {𝕜 𝕜' E : Type*}
2121

2222
variable [OrderedSemiring 𝕜]
2323

24-
-- TODO: remove `prettyPrint := false` once #6833 is merged
25-
local notation3 (prettyPrint := false) "𝕜≥0" => {c : 𝕜 // 0 ≤ c}
24+
local notation3 "𝕜≥0" => {c : 𝕜 // 0 ≤ c}
2625

2726
namespace Nonneg
2827

Mathlib/AlgebraicGeometry/Restrict.lean

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,11 @@ variable (X : Scheme)
3737

3838
/-- `f ⁻¹ᵁ U` is notation for `(Opens.map f.1.base).obj U`,
3939
the preimage of an open set `U` under `f`. -/
40-
notation3:90 f:91 "⁻¹ᵁ " U:90 => Prefunctor.obj
41-
(Functor.toPrefunctor <| Opens.map (PresheafedSpace.Hom.base (LocallyRingedSpace.Hom.val f))) U
40+
notation3:90 f:91 "⁻¹ᵁ " U:90 => (Opens.map (f : LocallyRingedSpace.Hom _ _).val.base).obj U
4241

4342
/-- `X ∣_ᵤ U` is notation for `X.restrict U.openEmbedding`, the restriction of `X` to an open set
4443
`U` of `X`. -/
45-
notation3:60 X:60 " ∣_ᵤ " U:61 => (Scheme.restrict X (Opens.openEmbedding U))
46-
47-
attribute [nolint docBlame] «term_⁻¹ᵁ_».delab «term_∣_ᵤ_».delab
44+
notation3:60 X:60 " ∣_ᵤ " U:61 => Scheme.restrict X (U : Opens X).openEmbedding
4845

4946
/-- The restriction of a scheme to an open subset. -/
5047
abbrev Scheme.ιOpens {X : Scheme} (U : Opens X.carrier) : X ∣_ᵤ U ⟶ X := X.ofRestrict _

Mathlib/Combinatorics/SimpleGraph/Regularity/Bound.lean

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,9 @@ open SzemerediRegularity
6565
variable {α : Type*} [DecidableEq α] [Fintype α] {P : Finpartition (univ : Finset α)}
6666
{u : Finset α} {ε : ℝ}
6767

68-
local notation3 (prettyPrint := false)
69-
"m" => (card α / stepBound P.parts.card : ℕ)
68+
local notation3 "m" => (card α / stepBound P.parts.card : ℕ)
7069

71-
local notation3 (prettyPrint := false)
72-
"a" => (card α / P.parts.card - m * 4 ^ P.parts.card : ℕ)
70+
local notation3 "a" => (card α / P.parts.card - m * 4 ^ P.parts.card : ℕ)
7371

7472
namespace SzemerediRegularity.Positivity
7573

Mathlib/Combinatorics/SimpleGraph/Regularity/Chunk.lean

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ namespace SzemerediRegularity
4848
variable {α : Type*} [Fintype α] {P : Finpartition (univ : Finset α)} (hP : P.IsEquipartition)
4949
(G : SimpleGraph α) (ε : ℝ) {U : Finset α} (hU : U ∈ P.parts) (V : Finset α)
5050

51-
local notation3 (prettyPrint := false)
52-
"m" => (card α / stepBound P.parts.card : ℕ)
51+
local notation3 "m" => (card α / stepBound P.parts.card : ℕ)
5352

5453
/-!
5554
### Definitions

Mathlib/Combinatorics/SimpleGraph/Regularity/Increment.lean

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ open scoped BigOperators Classical SzemerediRegularity.Positivity
4545
variable {α : Type*} [Fintype α] {P : Finpartition (univ : Finset α)} (hP : P.IsEquipartition)
4646
(G : SimpleGraph α) (ε : ℝ)
4747

48-
local notation3 (prettyPrint := false)
49-
"m" => (card α / stepBound P.parts.card : ℕ)
48+
local notation3 "m" => (card α / stepBound P.parts.card : ℕ)
5049

5150
namespace SzemerediRegularity
5251

Mathlib/Data/PSigma/Order.lean

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ namespace PSigma
3737

3838
/-- The notation `Σₗ' i, α i` refers to a sigma type which is locally equipped with the
3939
lexicographic order.-/
40-
notation3 "Σₗ' "(...)", "r:(scoped p => _root_.Lex (PSigma p)) => r
40+
-- TODO: make `Lex` be `Sort u -> Sort u` so we can remove `.{_+1, _+1}`
41+
notation3 "Σₗ' "(...)", "r:(scoped p => _root_.Lex (PSigma.{_+1, _+1} p)) => r
4142

4243
namespace Lex
4344

Mathlib/Lean/Elab/Term.lean

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/-
2+
Copyright (c) 2023 Kyle Miller. All rights reserved.
3+
Released under Apache 2.0 license as described in the file LICENSE.
4+
Authors: Kyle Miller
5+
-/
6+
import Lean.Elab
7+
8+
/-!
9+
# Additions to `Lean.Elab.Term`
10+
-/
11+
12+
namespace Lean.Elab.Term
13+
14+
/-- Fully elaborates the term `patt`, allowing typeclass inference failure,
15+
but while setting `errToSorry` to false.
16+
Typeclass failures result in plain metavariables.
17+
Instantiates all assigned metavariables. -/
18+
def elabPattern (patt : Term) (expectedType? : Option Expr) : TermElabM Expr := do
19+
withTheReader Term.Context ({ · with ignoreTCFailures := true, errToSorry := false }) <|
20+
withSynthesizeLight do
21+
let t ← elabTerm patt expectedType?
22+
synthesizeSyntheticMVars (mayPostpone := false) (ignoreStuckTC := true)
23+
instantiateMVars t
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/-
2+
Copyright (c) 2023 Kyle Miller. All rights reserved.
3+
Released under Apache 2.0 license as described in the file LICENSE.
4+
Authors: Kyle Miller
5+
-/
6+
import Lean.PrettyPrinter.Delaborator.Basic
7+
8+
/-!
9+
# Additions to the delaborator
10+
-/
11+
12+
namespace Lean.PrettyPrinter.Delaborator
13+
14+
open Lean.Meta Lean.SubExpr SubExpr
15+
16+
namespace SubExpr
17+
18+
variable {α : Type} [Inhabited α]
19+
variable {m : TypeType} [Monad m]
20+
21+
section Descend
22+
23+
variable [MonadReaderOf SubExpr m] [MonadWithReaderOf SubExpr m]
24+
variable [MonadLiftT MetaM m] [MonadControlT MetaM m]
25+
variable [MonadLiftT IO m]
26+
27+
/-- Assuming the current expression is a lambda or pi,
28+
descend into the body using the given name `n` for the username of the fvar.
29+
Provides `x` with the fresh fvar for the bound variable.
30+
See also `Lean.PrettyPrinter.Delaborator.SubExpr.withBindingBody`. -/
31+
def withBindingBody' (n : Name) (x : Expr → m α) : m α := do
32+
let e ← getExpr
33+
Meta.withLocalDecl n e.binderInfo e.bindingDomain! fun fvar =>
34+
descend (e.bindingBody!.instantiate1 fvar) 1 (x fvar)
35+
36+
end Descend
37+
38+
end SubExpr
39+
40+
/-- Assuming the current expression in a lambda or pi,
41+
descend into the body using an unused name generated from the binder's name.
42+
Provides `d` with both `Syntax` for the bound name as an identifier
43+
as well as the fresh fvar for the bound variable.
44+
See also `Lean.PrettyPrinter.Delaborator.withBindingBodyUnusedName`. -/
45+
def withBindingBodyUnusedName' {α} (d : Syntax → Expr → DelabM α) : DelabM α := do
46+
let n ← getUnusedName (← getExpr).bindingName! (← getExpr).bindingBody!
47+
let stxN ← annotateCurPos (mkIdent n)
48+
withBindingBody' n $ d stxN

0 commit comments

Comments
 (0)