@@ -81,3 +81,164 @@ run_cmd Lean.Elab.Command.liftTermElabM do
8181 Lean.logInfo <| ← Lean.PrettyPrinter.ppTerm <| ← `(term| ($a)$α:subscript)
8282
8383end
84+
85+ section delab
86+
87+ open Lean PrettyPrinter.Delaborator SubExpr
88+ open Mathlib.Tactic (delabSubscript delabSuperscript)
89+
90+ private def checkSubscript {α : Type } (_ : α) := ()
91+ local syntax :arg "testsub(" noWs subscript(term) noWs ")" : term
92+ local macro_rules | `(testsub($a:subscript)) => `(checkSubscript $a)
93+
94+ private def checkSuperscript {α : Type } (_ : α) := ()
95+ local syntax :arg "testsup(" noWs superscript(term) noWs ")" : term
96+ local macro_rules | `(testsup($a:superscript)) => `(checkSuperscript $a)
97+
98+ @ [app_delab checkSubscript]
99+ private def delabCheckSubscript : Delab := withOverApp 2 do
100+ let sub ← withAppArg delabSubscript
101+ `(testsub($sub:subscript))
102+
103+ @ [app_delab checkSuperscript]
104+ private def delabCheckSuperscript : Delab := withOverApp 2 do
105+ let sup ← withAppArg delabSuperscript
106+ `(testsup($sup:superscript))
107+
108+ universe u v
109+
110+ /-- `α` can not be subscripted or superscripted. -/
111+ private def α {γ : Type u} {δ : Type v} : γ → δ → δ := fun _ ↦ id
112+ /-- `β` can be both subscripted and superscripted. -/
113+ private def β {γ : Type u} {δ : Type v} : γ → δ → δ := fun _ ↦ id
114+
115+ /-- `d` can not be subscripted, so we create an alias for `id`. -/
116+ private abbrev ID {γ : Sort u} := @id γ
117+
118+ variable (n : Nat)
119+
120+ /-- info: checkSubscript testsub(₁) : Unit -/
121+ #guard_msgs in #check checkSubscript (checkSubscript 1 )
122+ /-- info: checkSubscript testsup(¹) : Unit -/
123+ #guard_msgs in #check checkSubscript (checkSuperscript 1 )
124+ /-- info: checkSuperscript testsup(¹) : Unit -/
125+ #guard_msgs in #check checkSuperscript (checkSuperscript 1 )
126+ /-- info: checkSuperscript testsub(₁) : Unit -/
127+ #guard_msgs in #check checkSuperscript (checkSubscript 1 )
128+
129+ section subscript
130+
131+ /-- info: testsub(₁₂₃₄₅₆₇₈₉₀ ₌₌ ₁₂₃₄₅₆₇₈₉₀) : Unit -/
132+ #guard_msgs in #check testsub(₁₂₃₄₅₆₇₈₉₀ ₌₌ ₁₂₃₄₅₆₇₈₉₀)
133+ /-- info: testsub(ᵦ ₙ ₍₁ ₊ ₂ ₋ ₃ ₌ ₀₎) : Unit -/
134+ #guard_msgs in #check testsub(ᵦ ₙ ₍₁ ₊ ₂ ₋ ₃ ₌ ₀₎)
135+ /-- info: testsub(ᵦ) : Unit -/
136+ #guard_msgs in #check testsub(ᵦ)
137+ /-- info: testsub(ᵦ ₍₎) : Unit -/
138+ #guard_msgs in #check testsub(ᵦ ₍₎)
139+ /-- info: testsub(ᵦ ᵦ ᵦ ᵦ) : Unit -/
140+ #guard_msgs in #check testsub(ᵦ ᵦ ᵦ ᵦ)
141+ /-- info: testsub(ɪᴅ ɪᴅ ₃₇) : Unit -/
142+ #guard_msgs in #check testsub(ɪᴅ ɪᴅ ₃₇)
143+
144+ end subscript
145+
146+ section superscript
147+
148+ /-- info: testsup(¹²³⁴⁵⁶⁷⁸⁹⁰ ⁼⁼ ¹²³⁴⁵⁶⁷⁸⁹⁰) : Unit -/
149+ #guard_msgs in #check testsup(¹²³⁴⁵⁶⁷⁸⁹⁰ ⁼⁼ ¹²³⁴⁵⁶⁷⁸⁹⁰)
150+ /-- info: testsup(ᵝ ⁿ ⁽¹ ⁺ ² ⁻ ³ ⁼ ⁰⁾) : Unit -/
151+ #guard_msgs in #check testsup(ᵝ ⁿ ⁽¹ ⁺ ² ⁻ ³ ⁼ ⁰⁾)
152+ /-- info: testsup(ᵝ) : Unit -/
153+ #guard_msgs in #check testsup(ᵝ)
154+ /-- info: testsup(ᵝ ⁽⁾) : Unit -/
155+ #guard_msgs in #check testsup(ᵝ ⁽⁾)
156+ /-- info: testsup(ᵝ ᵝ ᵝ ᵝ) : Unit -/
157+ #guard_msgs in #check testsup(ᵝ ᵝ ᵝ ᵝ)
158+ /-- info: testsup(ⁱᵈ ⁱᵈ ³⁷) : Unit -/
159+ #guard_msgs in #check testsup(ⁱᵈ ⁱᵈ ³⁷)
160+
161+ end superscript
162+
163+ private def card {γ : Sort u} := @id γ
164+ local prefix :arg "#" => card
165+
166+ private def factorial {γ : Sort u} := @id γ
167+ local notation :10000 n "!" => factorial n
168+
169+ abbrev Nat' := Nat
170+ def Nat'.γ : Nat' → Nat' := id
171+
172+ variable (n x_x : Nat')
173+
174+ section no_subscript
175+
176+ /-- info: checkSubscript x_x : Unit -/
177+ #guard_msgs in #check checkSubscript x_x
178+ /-- info: checkSubscript (α 0 0) : Unit -/
179+ #guard_msgs in #check checkSubscript (α 0 0 )
180+ /-- info: checkSubscript (0 * 1) : Unit -/
181+ #guard_msgs in #check checkSubscript (0 * 1 )
182+ /-- info: checkSubscript (0 ^ 1) : Unit -/
183+ #guard_msgs in #check checkSubscript (0 ^ 1 )
184+ /-- info: checkSubscript [ 1 ] : Unit -/
185+ #guard_msgs in #check checkSubscript [1 ]
186+ /-- info: checkSubscript #n : Unit -/
187+ #guard_msgs in #check checkSubscript #n
188+ /-- info: checkSubscript 2! : Unit -/
189+ #guard_msgs in #check checkSubscript 2 !
190+
191+ /- The delaborator should reject dot notation. -/
192+ open Nat' (γ) in
193+ /-- info: checkSubscript n.γ : Unit -/
194+ #guard_msgs in #check testsub(ᵧ ₙ)
195+
196+ /- The delaborator should reject metavariables. -/
197+ set_option pp.mvars false in
198+ /-- info: checkSubscript ?_ : Unit -/
199+ #guard_msgs in #check checkSubscript ?_
200+
201+ /- The delaborator should reject because `n` is shadowed and `✝` can not be
202+ subscripted. -/
203+ variable {x} (hx : x = testsub(ₙ)) (n : True) in
204+ /-- info: hx : x = checkSubscript n✝ -/
205+ #guard_msgs in #check hx
206+
207+ end no_subscript
208+
209+ section no_superscript
210+
211+ /-- info: checkSuperscript x_x : Unit -/
212+ #guard_msgs in #check checkSuperscript x_x
213+ /-- info: checkSuperscript (α 0 0) : Unit -/
214+ #guard_msgs in #check checkSuperscript (α 0 0 )
215+ /-- info: checkSuperscript (0 * 1) : Unit -/
216+ #guard_msgs in #check checkSuperscript (0 * 1 )
217+ /-- info: checkSuperscript (0 ^ 1) : Unit -/
218+ #guard_msgs in #check checkSuperscript (0 ^ 1 )
219+ /-- info: checkSuperscript [ 1 ] : Unit -/
220+ #guard_msgs in #check checkSuperscript [1 ]
221+ /-- info: checkSuperscript #n : Unit -/
222+ #guard_msgs in #check checkSuperscript #n
223+ /-- info: checkSuperscript 2! : Unit -/
224+ #guard_msgs in #check checkSuperscript 2 !
225+
226+ /- The delaborator should reject dot notation. -/
227+ open Nat' (γ) in
228+ /-- info: checkSuperscript n.γ : Unit -/
229+ #guard_msgs in #check testsup(ᵞ ⁿ)
230+
231+ /- The delaborator should reject metavariables. -/
232+ set_option pp.mvars false in
233+ /-- info: checkSuperscript ?_ : Unit -/
234+ #guard_msgs in #check checkSuperscript ?_
235+
236+ /- The delaborator should reject because `n` is shadowed and `✝` can not be
237+ superscripted. -/
238+ variable {x} (hx : x = testsup(ⁿ)) (n : True) in
239+ /-- info: hx : x = checkSuperscript n✝ -/
240+ #guard_msgs in #check hx
241+
242+ end no_superscript
243+
244+ end delab
0 commit comments