Skip to content

Commit e0a85d7

Browse files
committed
feat: delaborator for finset builder notation (#22076)
1 parent c819e08 commit e0a85d7

File tree

4 files changed

+75
-9
lines changed

4 files changed

+75
-9
lines changed

Mathlib/Algebra/BigOperators/Expect.lean

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,13 @@ scoped macro_rules (kind := bigexpect)
8383
open Lean Meta Parser.Term PrettyPrinter.Delaborator SubExpr
8484
open Batteries.ExtendedBinder
8585

86-
/-- Delaborator for `Finset.expect`. The `pp.piBinderTypes` option controls whether
86+
/-- Delaborator for `Finset.expect`. The `pp.funBinderTypes` option controls whether
8787
to show the domain type when the expect is over `Finset.univ`. -/
8888
@[scoped app_delab Finset.expect] def delabFinsetExpect : Delab :=
8989
whenPPOption getPPNotation <| withOverApp 6 <| do
9090
let #[_, _, _, _, s, f] := (← getExpr).getAppArgs | failure
9191
guard <| f.isLambda
92-
let ppDomain ← getPPOption getPPPiBinderTypes
92+
let ppDomain ← getPPOption getPPFunBinderTypes
9393
let (i, body) ← withAppArg <| withBindingBodyUnusedName fun i => do
9494
return (i, ← delab)
9595
if s.isAppOfArity ``Finset.univ 2 then

Mathlib/Algebra/BigOperators/Group/Finset/Defs.lean

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,13 +231,13 @@ end deprecated
231231
open Lean Meta Parser.Term PrettyPrinter.Delaborator SubExpr
232232
open scoped Batteries.ExtendedBinder
233233

234-
/-- Delaborator for `Finset.prod`. The `pp.piBinderTypes` option controls whether
234+
/-- Delaborator for `Finset.prod`. The `pp.funBinderTypes` option controls whether
235235
to show the domain type when the product is over `Finset.univ`. -/
236236
@[app_delab Finset.prod] def delabFinsetProd : Delab :=
237237
whenPPOption getPPNotation <| withOverApp 5 <| do
238238
let #[_, _, _, s, f] := (← getExpr).getAppArgs | failure
239239
guard <| f.isLambda
240-
let ppDomain ← getPPOption getPPPiBinderTypes
240+
let ppDomain ← getPPOption getPPFunBinderTypes
241241
let (i, body) ← withAppArg <| withBindingBodyUnusedName fun i => do
242242
return (i, ← delab)
243243
if s.isAppOfArity ``Finset.univ 2 then
@@ -252,13 +252,13 @@ to show the domain type when the product is over `Finset.univ`. -/
252252
let ss ← withNaryArg 3 <| delab
253253
`(∏ $(.mk i):ident ∈ $ss, $body)
254254

255-
/-- Delaborator for `Finset.sum`. The `pp.piBinderTypes` option controls whether
255+
/-- Delaborator for `Finset.sum`. The `pp.funBinderTypes` option controls whether
256256
to show the domain type when the sum is over `Finset.univ`. -/
257257
@[app_delab Finset.sum] def delabFinsetSum : Delab :=
258258
whenPPOption getPPNotation <| withOverApp 5 <| do
259259
let #[_, _, _, s, f] := (← getExpr).getAppArgs | failure
260260
guard <| f.isLambda
261-
let ppDomain ← getPPOption getPPPiBinderTypes
261+
let ppDomain ← getPPOption getPPFunBinderTypes
262262
let (i, body) ← withAppArg <| withBindingBodyUnusedName fun i => do
263263
return (i, ← delab)
264264
if s.isAppOfArity ``Finset.univ 2 then

Mathlib/Data/Fintype/Defs.lean

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ theorem subset_univ (s : Finset α) : s ⊆ univ := fun a _ => mem_univ a
110110
end Finset
111111

112112
namespace Mathlib.Meta
113-
open Lean Elab Term Meta Batteries.ExtendedBinder
113+
open Lean Elab Term Meta Batteries.ExtendedBinder Parser.Term PrettyPrinter.Delaborator SubExpr
114114

115115
/-- Elaborate set builder notation for `Finset`.
116116
@@ -131,8 +131,6 @@ See also
131131
`{x | p x}`, `{x : α | p x}`, `{x ∉ s | p x}`, `{x ≠ a | p x}`.
132132
* `Order.LocallyFinite.Basic` for the `Finset` builder notation elaborator handling syntax of the
133133
form `{x ≤ a | p x}`, `{x ≥ a | p x}`, `{x < a | p x}`, `{x > a | p x}`.
134-
135-
TODO: Write a delaborator
136134
-/
137135
@[term_elab setBuilder]
138136
def elabFinsetBuilderSetOf : TermElab
@@ -162,6 +160,34 @@ def elabFinsetBuilderSetOf : TermElab
162160
elabTerm (← `(Finset.filter (fun $x:ident ↦ $p) (singleton $a)ᶜ)) expectedType?
163161
| _, _ => throwUnsupportedSyntax
164162

163+
/-- Delaborator for `Finset.filter`. The `pp.funBinderTypes` option controls whether
164+
to show the domain type when the filter is over `Finset.univ`. -/
165+
@[app_delab Finset.filter] def delabFinsetFilter : Delab :=
166+
whenPPOption getPPNotation do
167+
let #[_, p, _, t] := (← getExpr).getAppArgs | failure
168+
guard p.isLambda
169+
let i ← withNaryArg 1 <| withBindingBodyUnusedName (pure ⟨·⟩)
170+
let p ← withNaryArg 1 <| withBindingBody i.getId delab
171+
if t.isAppOfArity ``Finset.univ 2 then
172+
if ← getPPOption getPPFunBinderTypes then
173+
let ty ← withNaryArg 0 delab
174+
`({$i:ident : $ty | $p})
175+
else
176+
`({$i:ident | $p})
177+
-- check if `t` is of the form `s₀ᶜ`, in which case we display `x ∉ s₀` instead
178+
else if t.isAppOfArity ``HasCompl.compl 3 then
179+
let #[_, _, s₀] := t.getAppArgs | failure
180+
-- if `s₀` is a singleton, we can even use the notation `x ≠ a`
181+
if s₀.isAppOfArity ``Singleton.singleton 4 then
182+
let t ← withNaryArg 3 <| withNaryArg 2 <| withNaryArg 3 delab
183+
`({$i:ident ≠ $t | $p})
184+
else
185+
let t ← withNaryArg 3 <| withNaryArg 2 delab
186+
`({$i:ident ∉ $t | $p})
187+
else
188+
let t ← withNaryArg 3 delab
189+
`({$i:ident ∈ $t | $p})
190+
165191
end Mathlib.Meta
166192

167193
open Finset Function
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import Mathlib.Data.Fintype.Defs
2+
3+
variable {α : Type*} [Fintype α] {p : α → Prop} {s : Finset α} {a : α}
4+
[DecidablePred p] [DecidableEq α] [Singleton α (Finset α)] [HasCompl (Finset α)]
5+
6+
/-- info: {x | p x} : Finset α -/
7+
#guard_msgs in
8+
#check ({x | p x} : Finset α)
9+
10+
/-- info: {x ∈ s | p x} : Finset α -/
11+
#guard_msgs in
12+
#check ({x ∈ s | p x})
13+
14+
/-- info: {x ≠ a | p x} : Finset α -/
15+
#guard_msgs in
16+
#check ({x ≠ a | p x} : Finset α)
17+
18+
/-- info: {x ∉ s | p x} : Finset α -/
19+
#guard_msgs in
20+
#check ({x ∉ s | p x})
21+
22+
/-- info: {x : α | p x} : Finset α -/
23+
#guard_msgs in
24+
set_option pp.funBinderTypes true in
25+
#check ({x | p x} : Finset α)
26+
27+
/-- info: {x ∈ s | p x} : Finset α -/
28+
#guard_msgs in
29+
set_option pp.funBinderTypes true in
30+
#check ({x ∈ s | p x})
31+
32+
/-- info: {x ≠ a | p x} : Finset α -/
33+
#guard_msgs in
34+
set_option pp.funBinderTypes true in
35+
#check ({x ≠ a | p x} : Finset α)
36+
37+
/-- info: {x ∉ s | p x} : Finset α -/
38+
#guard_msgs in
39+
set_option pp.funBinderTypes true in
40+
#check ({x ∉ s | p x})

0 commit comments

Comments
 (0)